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

Is __typename in idFields required when writing and reading fragments? #350

Open
kvenn opened this issue May 11, 2024 · 3 comments
Open
Labels

Comments

@kvenn
Copy link

kvenn commented May 11, 2024

I see the docs mention the use of write and read like so:

  client.writeFragment$PersonSummary(
    idFields: {'__typename': 'Person', 'id': '1'},
    data: person.copyWith(name: 'Kurt'),
  );

But from this change, the Fragment already knows its own typename : #247

Is it possible to generate a shorthand so I can just call it like so:

  client.writeFragment$PersonSummary(
    idFields: { 'id': '1'},
    data: person.copyWith(name: 'Kurt'),
  );

Or even better yet (since all of our idFields are the same for all our fragments):

  client.writeFragment$PersonSummary(
	id: '1'
    data: person.copyWith(name: 'Kurt'),
  );
Copy link

👋 @kvenn
Thank you for raising an issue. I will investigate the issue and get back to you as soon as possible.
Please make sure you have provided enough context.

This library is created and maintained by me, @budde377. Please consider supporting my work and ensure our survival by donating here.

@budde377
Copy link
Contributor

Good idea, thanks for raising it. I will investigate and see if its possible :)

@kvenn
Copy link
Author

kvenn commented Jun 22, 2024

Incredible thank you!!

We've been using wrappers in our code which kind of exemplify our ideal API. The read (in our case) could only need a string ID and the write only needs the object (since it will have the ID on it). I know this makes the function a little less flexible, but maybe it's a building block on top of the default?

  Fragment$User? readUser(String id)
  void writeUser(Fragment$User user) 

Implementation

extension UserCache on GraphQLClient {
  Fragment$User? readUser(String id) =>
      readFragment$User(
        idFields: {
          '__typename': 'User',
          'id': id,
        },
      );

  void writeUser(Fragment$User user) =>
      writeFragment$User(
        idFields: {
          '__typename': 'User',
          'id': user.id,
        },
        data: user,
      );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants