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

阿波罗链路状态在调用缓存时发出投诉。写入数据

  •  1
  • Ron  · 技术社区  · 8 年前

    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。

    1 回复  |  直到 8 年前
        1
  •  3
  •   sonlexqt    8 年前

    我也有同样的问题。从中引用代码后 the todos example 我发现你必须这样包装你的物体 { data: { // your actual data } } . 因此,在您的情况下,可能是:

    cache.writeData({
      data: {
        modal: {
          name,
          open: true,
          __typename: 'Modal',
        },
      },
    });