简体   繁体   English

Graphql react-apollo内省片段匹配器

[英]Graphql react-apollo IntrospectionFragmentMatcher

I'm using GitHub Graphql API and I wrote following code with react-apollo but when I paginate after many requests I get following errors on the console. 我正在使用GitHub Graphql API,并使用react-apollo编写了以下代码,但是当我在许多请求后分页时,控制台上出现以下错误。

You are using the simple (heuristic) fragment matcher, but your queries contain union or interface types. 您正在使用简单的(启发式)片段匹配器,但是您的查询包含联合或接口类型。 Apollo Client will not be able to accurately map fragments. Apollo Client将无法准确映射片段。 To make this error go away, use the IntrospectionFragmentMatcher as described in the docs: https://www.apollographql.com/docs/react/recipes/fragment-matching.html 要消除此错误,请按照docs中所述使用IntrospectionFragmentMatcherhttps : //www.apollographql.com/docs/react/recipes/fragment-matching.html

.

WARNING: heuristic fragment matching going on! 警告:启发式片段匹配正在进行中!

.

Missing field name in { "__typename": "Organization" } {“ __typename”:“ Organization”}中缺少字段名称

Missing field avatarUrl in { "__typename": "Organization" } {“ __typename”:“ Organization”}中缺少字段avatarUrl

Missing field repositories in { "__typename": "Organization" } {“ __typename”:“ Organization”}中缺少字段存储库

and I wrote the following codes: 我写了以下代码:

gql`
  query($username: String!, $nextPage: String) {
    search(query: $username, type: USER, first: 100, after: $nextPage) {
      pageInfo {
        hasNextPage
        endCursor
      }
      edges {
        node {
          ... on User {
            name
            avatarUrl
            repositories {
              totalCount
            }
          }
        }
      }
    }
  }
`;


handleSubmit = ({ username }) => {
    const { client } = this.props;
    this.setState({
      loading: true,
      searchTerm: username,
    });
    client
      .query({
        query: SEARCH_USER,
        variables: {
          username
        }
      })
      .then(({ data }) => {
        this.setState({
          loading: false,
          searchList: data.search.edges,
          pagination: data.search.pageInfo,
        });
      })
      .catch(err => {
        console.warn(err);
      });
  };

Because Apollo has not enough information about your GraphQL Schema you need to provide it somehow. 由于Apollo没有足够的有关GraphQL模式的信息,因此需要以某种方式提供它。 Apollo has a well written documentation on that topic. 阿波罗(Apollo)有关于该主题的书面文档

It describes to use a script to introspect your GraphQL Server in order to get that missing information about Unions and Interfaces. 它描述了使用脚本对您的GraphQL Server进行内部检查,以获取有关联合和接口的缺失信息。

To make the process even easier I wrote a plugin for GraphQL Code Generator that automates everything. 为了使过程更加轻松,我为GraphQL代码生成器编写了一个插件,该插件可以自动执行所有操作。 There's a chapter called "Fragment Matcher" that I recommend to read. 我建议阅读一章“片段匹配器”

Either first, the manual solution or the second should fix your problem :) 首先,手动解决方案或第二个应该解决您的问题:)

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

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