【问题标题】:Using Variables with react-apollo Query使用带有 react-apollo 查询的变量
【发布时间】:2018-09-25 13:13:11
【问题描述】:

我已将查询附加到我的 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

只要我在查询中为 geoHash 硬编码一个值,我就可以使用这种技术检索数据;在这种情况下,我使用了“8k”。但是,当我尝试修改查询以便可以像这样插入变量时:

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

我收到一条错误消息 Expected Name, found $。这种将变量传递给查询的方法在多个源中重复。我在这里做错了什么?

【问题讨论】:

    标签: javascript graphql apollo react-apollo graphql-tag


    【解决方案1】:

    应该是:

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

    看看如何在graphql中使用变量:http://graphql.org/learn/queries/#variables

    【讨论】:

    • 谢谢!这让我发疯了几个小时。
    猜你喜欢
    • 2021-04-13
    • 2019-01-02
    • 2017-08-15
    • 2019-01-28
    • 2018-01-27
    • 2019-08-14
    • 2018-11-06
    • 2018-10-30
    • 2019-01-26
    相关资源
    最近更新 更多