【发布时间】:2019-07-26 04:24:36
【问题描述】:
我正在使用 apollo-boost 构建一个应用程序,其中应该已经包含一些用于使用 graphql 进行反应应用程序的包。
当我想将数据片段存储到我的 Apollo 缓存中时,我遇到了一个错误。
ApolloClient.tsx
import ApolloClient from 'apollo-boost'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { secretToken } from './secretToken'
const cache = new InMemoryCache({
})
const client = new ApolloClient({
uri: 'https://someUrlIremovedForTheQuestion.com',
request: async operation => {
operation.setContext({
headers: {
Authorization: secretToken
}
})
},
cache
})
client.writeData({
data: {
names: [
{
firstName: 'Mario'
},
{
firstName: 'Luigi'
}
]
}
})
export default client
现在我正在尝试访问我在同一个 apollo 配置中创建的这些名称:
MyComponent.tsx
const result = client.readQuery({ query: gql`
query namesCached {
names @client
}`
})
console.log(result)
这总是陷入这个错误:
Missing selection set for object of type undefined returned for query field names
如果我正在处理单个数据,例如 name: 'Mario',它可以完美地检索该数据,但对于更深层次的对象或数组,它总是会因此错误而失败。
我也在使用 Typescript
知道可能是什么吗?堆栈溢出向我展示了几个无效的答案,而 GitHub 也没有为我提供解决方案......
编辑 因此,这是我的真实示例,以使其更接近我的实际问题:
<Query
query={fetchdata}
onCompleted={({ repository }) => client.writeData({
data: {
issues: repository.issues.edges
}
})
}
>
基本上我正在做一个查询,一旦完成我想将它存储在缓存中,因为我多次检查它以比较和过滤一些结果。
我尝试了repository,但仍然出现同样的错误
要检索数据,我正在执行与上述相同的操作,但使用 issues 而不是 names
【问题讨论】:
标签: reactjs graphql react-apollo apollo-client