Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ghost opened this issue Sep 24, 2023 · 0 comments

Comments

@ghost
Copy link

ghost commented Sep 24, 2023

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.

@ghost ghost changed the title writeFragment on interface fields causes an incorrect change in __typename [BUG] writeFragment on interface fields causes an incorrect change in __typename Sep 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants