代码之家  ›  专栏  ›  技术社区  ›  acmoune

apollo链接状态:如何编写查询解析器?

  •  2
  • acmoune  · 技术社区  · 7 年前

    const stateLink = withClientState({
      cache,
      resolvers,
      defaults: {
        quote: {
          __typename: 'Quote',
          name: '',
          phoneNumber: '',
          email: '',
          items: []
        }
      }
    })
    

    所以我的缓存不应该是空的。现在我的 resolvers 地图如下所示:

    resolvers = {
      Mutation: { ... },
      Query: {
        quote: (parent, args, { cache }) => {
          const query = gql`query getQuote {
            quote @client {
              name phoneNumber email items
            }
          }`
    
          const { quote } = cache.readQuery({ query, variables: {} })
          return ({ ...quote })
        }
      }
    }
    

    datasource 解析器 quote 对此,我提出另一个问题 引用

    我想我应该得到 引用 引用

    Can't find field **quote** on object (ROOT_QUERY) undefined
    

    请帮忙

    1 回复  |  直到 7 年前
        1
  •  1
  •   Florian    7 年前

    只是想贴出同样的问题-幸运的是,我刚刚发现了。

      protected resolvers = {
    Query: {
      getProdConfig: (parent, args, { cache, getCacheKey }) => {
        const id = getCacheKey({ __typename: 'ProdConfig', id: args.id });
        const fragment = gql`fragment prodConfig on ProdConfig {
          id,
          apiKey,
          backupUrl,
          serverUrl,
          cache,
          valid
        }`;
        const data = cache.readFragment({ fragment, id })
        return ({ ...data });
      }
    }
    

    阿波罗的召唤是:

        let query = this.$apollo.query(`
      query prodConfig($id: Int!) {
        getProdConfig(id: $id) @client {
          apiKey,
          backupUrl,
          serverUrl,
          cache,
          valid
        }
      }`,
      { id: 0 }
    );