我是apollo-link-state的新手,我被困了一段时间。我要解决的问题很简单,只需将模态信息保存到本地状态,但是,当我尝试保存时,它会抱怨:
ApolloError.js?f55daa5:36 Uncaught (in promise) Error: Network error: Cannot read property 'selections' of null
at new ApolloError (ApolloError.js?f55daa5:36)
at Object.error (QueryManager.js?8f5ff47:115)
at SubscriptionObserver.error (zen-observable.js?8f5ff47:176)
at <anonymous>
我的源代码,如下所示:
{ // resolvers and defaults
resolvers: {
Mutation: {
openModal: (_, args, { cache }) => {
const {name} = args
cache.writeData({
modal: {
name,
open: true,
__typename: 'Modal',
},
})
return null
},
closeModal: (_, __, {cache}) => {
cache.writeData({modal: {open: false}})
}
}
},
defaults: {
modal: {
__typename: 'Modal',
open: false,
name: ''
}
}
}
graphql(
gql`
mutation openModal ($name: String!) {
openModal (name: $name) @client
}
`
)(
({mutate}) => {
return (
<div>
<button onClick={() => mutate({ variables: { name: 'test' }})} >Open</button>
</div>
)
}
)
单击按钮时,会出现错误消息。有什么想法吗?
顺便说一句,我在next.js中使用。
答案 0 :(得分:3)
我遇到了同样的问题。在引用the todos example中的代码后,我发现你必须像这样{ data: { // your actual data } }
包装你的对象。所以在你的情况下,它可能是:
cache.writeData({
data: {
modal: {
name,
open: true,
__typename: 'Modal',
},
},
});