简体   繁体   English

在React-Apollo查询中使用变量

[英]Using Variables with react-apollo Query

I've attached a query to my React Native component like so: 我已经将查询附加到我的React Native组件上,如下所示:

let getNearbyStoriesQuery = gql`{
  getStoriesNearbyByGeoHash(geoHash: "8k"){
      id
  }
}`;

export default graphql(getNearbyStoriesQuery,
  {
    props: (props) => {
      debugger;
      let storiesNearby = props.data.getStoriesNearbyByGeoHash.map((story) => {
        return {
          id: story.id,
        }
      });
      return {storiesNearby};
    },
    variables: {
      geoHash: mockGeoHash
    }
  })(Home);  // Home is the React Native Component

I'm able to retrieve data from using this technique as long as I hardcode a value in for geoHash in the query; 只要我在查询中为geoHash硬编码一个值,就可以使用这种技术来检索数据; in this case I used "8k". 在这种情况下,我使用“ 8k”。 However, when I attempt to modify the query so that I can puss in a variable like so: 但是,当我尝试修改查询以便可以像这样输入变量时:

let getNearbyStoriesQuery = gql`{
      getStoriesNearbyByGeoHash($geoHash: String!){
          id
      }
    }`;

I get an error saying Expected Name, found $ . 我收到一个错误消息,说Expected Name, found $ This method of passing variables to queries is repeated across multiple sources. 在多个源之间重复使用这种将变量传递给查询的方法。 What am I doing wrong here? 我在这里做错了什么?

Should be: 应该:

query GetStoriesNearbyByGeoHash($geoHash: String!) {
    getStoriesNearbyByGeoHash(geoHash: $geoHash){
         id
    }
}

Have a look on how to use variables in graphql: http://graphql.org/learn/queries/#variables 看看如何在graphql中使用变量: http ://graphql.org/learn/queries/#variables

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM