zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.23k stars 613 forks source link

[BUG] writeFragment on interface fields causes an incorrect change in __typename #1381

Open ghost opened 10 months ago

ghost commented 10 months ago

Describe the issue Using writeFragment with a fragment on interface fields causes the__typename of the concrete type implementing the interface to become: __typename: InterfaceName.

To Reproduce (MUST BE PROVIDED)

Let's suppose we have the following DSL:

interface Post {
  title: String!
  createdAt: String!
}

type LinkPost implements Post {
  title: String!
  createdAt: String!
  linkUrl: String!
}

type TextPost implements Post {
  title: String!
  createdAt: String!
  bodyText: String!
}

Now we provide the following dataIdFromObject function to the graphql cache:

dataIdFromObject: (object) {
    return "${object['id']}";
},

Now we want to update the "title" field for an entity in the cache and we go with the following:

const postFragment = '''
    fragment fields on Post {
        title
    }
''';

gqlClient.cache.writeFragment(
    Fragment(
      document: gql(postFragment),
    ).asRequest(
      idFields: {
        'id': 'c8e82094',
      },
    ),
    data: {'title': 'New Title'},
  );

Before calling writeFragment, the cache entry in question looks like this:

c8e82094: {__typename: TextPost, id: c8e82094-6093-4df5-be2b-c1dc853d5071, title: 'Title'}

When calling writeFragment, the cache is updated with with wrong typename(and. the correct new value for "title"):

c8e82094: {__typename: Post, id: c8e82094-6093-4df5-be2b-c1dc853d5071, title: 'New Title'}

Here, we see that __typename is incorrect and uses the name of the interface.

Expected behavior The cache should preserve the cache entry of the concrete type (TextPost which already exists in the cache) based on its id, and only update the entry's field title

c8e82094: {__typename: TextPost, id: c8e82094-6093-4df5-be2b-c1dc853d5071, title: 'New Title'}

device / execution context iOs simulator on Mac.