Apollo-client: writeFragment: Missing field __typename

Created on 24 May 2017  路  4Comments  路  Source: apollographql/apollo-client

I'm trying to use writeFragment but running into the following issues:

https://d3vv6lp55qjaqc.cloudfront.net/items/2f1l0M0s3x2c3u270W0a/Screen%20Shot%202017-05-24%20at%2014.28.35.png?X-CloudApp-Visitor-Id=4c115ae96a1293116cf3f7a8f20e2c24&v=827959ce

My fragment does have __typename though:

client.writeFragment({
        id: job._id,
        fragment: gql`
          fragment JobsStatusUpdate on Job {
            __typename
            status
          }
        `,
        data: {
          status: 2
        },
      });

And I also have addTypename: true in my ApolloClient options. Any idea what's going on?

Most helpful comment

Can this be added to the docs example?

https://www.apollographql.com/docs/react/advanced/caching.html#writequery-and-writefragment

All 4 comments

The data you're writing is missing __typename. Just change it to this:

client.writeFragment({
        id: job._id,
        fragment: gql`
          fragment JobsStatusUpdate on Job {
            __typename
            status
          }
        `,
        data: {
          status: 2,
          __typename: 'Job',
        },
      });

Oh of course. For some reason I didn't realize you had to specify __typename in the data, that seems obvious in retrospect. Thanks!

Can this be added to the docs example?

https://www.apollographql.com/docs/react/advanced/caching.html#writequery-and-writefragment

In case somebody comes here and has nested fragments, make sure you have the corresponding nested __typename fields inside the data field.

Was this page helpful?
0 / 5 - 0 ratings