apollo-link-state:如何编写查询解析器?



我使用默认值创建状态链接,如下所示:

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 })
}
}
}

resolversdatasource是缓存对吗?所以我必须以某种方式查询缓存。但这不起作用,我想这是因为我正在尝试响应quote查询,为此我正在进行另一个quote查询。

我认为我应该在不查询quote的情况下获取quote数据,但是如何?

我收到此错误:

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

请帮忙

只是想发布同样的问题 - 幸运的是刚刚想通了。 readQuery-Methode 只允许您从根进行查询。因此,您应该使用 readFragment,因为它允许您访问缓存中的任何规范化字段,只要您获得它的 id(如下所示:GraphQlTypeName:0 通常由字段构造:id 和 __typename (。然后,查询解析程序应如下所示:

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 }
);

最新更新