0

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

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

验证类型:年龄@empId

人员详细信息:人员详细信息类型

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

值是从这里传递的:

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

1 回答 1

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'
        }
      });
于 2019-03-05T20:05:05.380 回答