【问题标题】:Query component returning null for data BUT network shows a response查询组件为数据返回 null 但网络显示响应
【发布时间】:2019-07-03 03:04:43
【问题描述】:

数据为空,但是,我在 /grahpql 和网络中都看到了响应。

查询是什么:准系统:

query GetProductById($product_id: Int!) {
  getProductById(product_id: $product_id) {
    id
    product_id
    name
    product_type
    create_time
    update_time
    sizes
    click_url
    price
    discount_price
    coupon_code
    sale_date
    sku
    colors
    in_stock

    regions {
       ...domestic
       ...international
    }
  }
}

编辑:更新:缩小到这个:::

    regions {
     ...domestic
     ...international
    }

我认为这是问题所在.. 不是吗?

import { IntrospectionFragmentMatcher } from 'apollo-cache-inmemory';

const fragmentMatcher = new IntrospectionFragmentMatcher({
  introspectionQueryResultData: {
    __schema: {
      types: [
        {
          kind: 'INTERFACE',
          name: 'Regions',
          possibleTypes: [
            {
              name: 'domestic'
            },
            {
              name: 'international'
            }
          ]
        }
      ]
    }
  }
});
 export default fragmentMatcher;




  import fragmentMatcher from './RegionFragmentMatcher';

  const cache = new InMemoryCache();
  const apolloClient = new ApolloClient({
    fragmentMatcher,
    cache,
    link: new HttpLink({
      uri: `${config.url}/graphql`,
      credentials: 'include'
    }),
     resolvers: Resolvers(cache),
     addTypename: true,
     dataIdFromObject,
     introspection: true
    });

    export default apolloClient;

【问题讨论】:

  • 查询内容是什么?
  • 更新以包含查询
  • 好的,我开始配对一些查询键,在某个时候,它起作用了。所以,似乎有什么东西在掩盖它。但是为什么在一种情况下它可以工作,而另一种情况却不能......?很奇怪。所以,我删除了大约一半的查询键...我将它放回去并一一进行..
  • 请看我的最新评论,或许你能帮到我。
  • 您是否将fragmentMatcher 变量导出为默认导出?我没有看到您粘贴的代码中显示的内容。

标签: graphql react-apollo


【解决方案1】:

FragmentMatcher 应该传递给 InMemoryCache 构造函数,而不是 ApolloClient 构造函数:

const cache = new InMemoryCache({ fragmentMatcher });

const client = new ApolloClient({
  cache,
  link: new HttpLink(),
});

【讨论】:

  • 宾果游戏!!!多谢兄弟!!就是这样..我不知道我是怎么错过的。我只是认为这涉及到更多的事情……哈哈。再次,谢谢!!!!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 2017-03-23
  • 2022-12-05
  • 2021-09-16
  • 2020-09-02
  • 1970-01-01
相关资源
最近更新 更多