【问题标题】:Getting `Store error: the application attempted to write an object with no provided typename but the store already contains an object with typename`获取`存储错误:应用程序试图写入一个没有提供类型名的对象,但存储已经包含一个具有类型名的对象`
【发布时间】:2019-07-24 16:18:27
【问题描述】:

我在更新时遇到存储错误,而且我是 apollo 和 graphQL 的新手,所以无法理解我做错了什么。 我正在尝试更新一个人的名字和姓氏。 这个对象是嵌套的,我将 typeName 作为包装对象传递。 如果有人理解这一点并可以提出建议,请告诉我

这就是它在阿波罗中的样子:

validationType:age@empId

persondetails:personDetailsType

第一个:“abc”
empId: 10002
年龄:“21”
最后:“定义”

从这里传递值:

 <Button content="Save"
              onClick={(_, {value}) => {
                this.props.mutation({
                  variables: {
                    persondetails: {
                      first: this.state.first,
                      last: this.state.last,
                      empId: empId,
                      age: age
                    },
                      age,
                      empId,
                  }
                });
              }}
            />

我的更新查询如下所示:

updatePersonDetails: (
      _,
      {persondetails, age, empId},
      {cache}
    ) => {
      cache.writeFragment({
        id: `validationType:${age}!${empId}`,
        fragment: gql`
          fragment updatePersonDetails on validationType {
            persondetails {
              first
              last
              age
              empId
              __typename

            }
            __typename
          }
        `,
        data: {
          persondetails: persondetails,
          age,
          empId,
          __typename: 'validationType'
        }
      });

更新时出现以下错误:

[Network error]: Error: Error writing result to store for query:
 {
  ...updatePersonDetails
}

fragment persondetails on validationType {
  persondetails {
   first
   last
   age
   empId
    __typename
  }
  __typename
}

Store error: the application attempted to write an object with no provided typename but the store already contains an object with typename of personDetailsType for the object of id $validationType!21.1002. The selectionSet that was trying to be written is:
persondetails {
 first
 last
 age
 empId
  __typename
}

【问题讨论】:

  • 听起来data 内部的persondetails 缺少__typename。很难确定,因为您的代码没有显示updatePersonDetails 函数是如何被调用的
  • 它肯定与 typename 有关,但我在数据字段中确实有一个 __typename,这使得它难以理解。在按钮标签中, onClick --> 它调用 props.mutation 进而调用 update 方法。下面的代码更清晰:export default MutationHoC({ Component: PersonDetailsEditComponent, mutation: updatePersonDetails });
  • 我可以看到数据反映在缓存中,但我猜在检索或执行更多操作时它会失败并抛出此错误。
  • __typename 是 Apollo 自动附加的元字段,以便轻松 normalize query results in the cache。当使用writeDatawriteFragment 时,data 内的每个对象都应该有适当的__typename 字段。如果它丢失了,你会在从缓存中获取内容时遇到问题。
  • 所以我的问题是:您在updatePersonDetails 函数中使用的persondetails 对象是否包含__typename。如果不是,那么这就是可能导致错误的原因。但是,再一次,如果不查看更多代码,很难确定这一点。

标签: reactjs graphql apollo apollo-cache-inmemory


【解决方案1】:

在传递更新时,我在解析器中以错误的方式声明了我的对象。而且我在该对象中缺少 typeName 。 正确的语法是:

updatePersonDetails: (
      _,
      {persondetails, age, empId},
      {cache}
    ) => {
      cache.writeFragment({
        id: `validationType:${age}!${empId}`,
        fragment: gql`
          fragment updatePersonDetails on validationType {
            persondetails {
              first
              last
              age
              empId
              __typename

            }
            __typename
          }
        `,
        data: {
          persondetails: {
           ...persondetails,
           __typename: 'persondetailsType'
}
          __typename: 'validationType'
        }
      });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2015-08-31
    相关资源
    最近更新 更多