javascriptgraphqlapolloreact-apollographql-tag

Using Variables with react-apollo Query


I've attached a query to my React Native component like so:

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; in this case I used "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 $. This method of passing variables to queries is repeated across multiple sources. What am I doing wrong here?


Solution

  • 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