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

Removing __typename from monomorphic fragments #3774

Closed
AnkitPatanaik opened this issue Jan 7, 2022 · 7 comments
Closed

Removing __typename from monomorphic fragments #3774

AnkitPatanaik opened this issue Jan 7, 2022 · 7 comments
Assignees
Labels
✔️ Fixed in SNAPSHOTs The fix has been merged and is available in SNAPSHOTs, and will be available in the next release ✨ Type: Feature

Comments

@AnkitPatanaik
Copy link

AnkitPatanaik commented Jan 7, 2022

Use case
I have an existing project that already uses GraphQL in production and am currently in the process of adding Apollo 3.0.0 to our project. Part of the complexity is that for any given query, we rely on many dozens of fragments which currently don’t explicitly query for __typename.

Describe the solution you'd like

  • For us to most easily integrate Apollo throughout our project it would be necessary to not add __typename to monomorphic types. I believe @martinbonnin mentioned possible solutions here in !3672. Specifically:

\ mid-term: (apollo-side) optimize and do not add __typename for monomorphic fields

  • Do you have a timeline for when you would be able to address this or have any recommended work-arounds to ease integration?

Let me know if there's anything I can add or provide that would be helpful!

  • I created this new Issue to make it easier for discussion/visibility to avoid piling onto !3672 which is closed
  • This is also related to !1458
@martinbonnin
Copy link
Contributor

Thanks for giving this problem the issue it deserves :)!

I'll take a look this week. Early draft there: #3777

@AnkitPatanaik
Copy link
Author

Hey @martinbonnin! Thanks for sharing your draft 😄 I was wondering if you had any updates on a timeline for it's merge

@martinbonnin
Copy link
Contributor

Hi @AnkitPatanaik. Very sorry for the delay on this. I've been having a few personal issues + this issue is not an easy one so it takes time to dive into it but it's still ongoing.

@martinbonnin
Copy link
Contributor

Small update: we had to ship 3.1.0 without this because this issue is still ongoing and it's a substantial change that I don't want to rush in. I dumped my current thoughts in Typename.md. Sorry for the delay but this is still ongoing.

@martinbonnin
Copy link
Contributor

martinbonnin commented Mar 21, 2022

Apologies again for the huge delay there that is due to a bunch of unplanned life events...

The good news is that this PR finally landed on main. It introduces a addTypename option to customise how __typename is handled:

apollo {
  /**
   * Add '__typename' for abstract fields, i.e. fields that are of union or interface type
   *
   * `"ifAbstract"` is a good and simple value that will work most of the times.
   *
   * Note: It also adds '__typename' on fragment definitions that satisfy the same property because fragments
   * could be read from the cache and we don't have a containing field in that case.
   **/
  addTypename.set("ifAbstract")

  /**
   * Add '__typename' for polymorphic fields, i.e. fields that contains a subfragment
   * (inline or named) whose type condition isn't a super type of the field type.
   * If a field is monomorphic, no '__typename' will be added.
   *
   * `"ifPolymorphic"` adds the bare minimum number of `__typename`. If you're using the cache it can lead
   * to some extra cache misses because of a missing `__typename` so test before you opt-in
   * 
   * Note: It also adds '__typename' on fragment definitions that satisfy the same property because fragments
   * could be read from the cache and we don't have a containing field in that case.
   **/
  addTypename.set("ifPolymorphic")


  /**
   * Add '__typename' for every selection set that contains fragments (inline or named) (Default)
   *
   * `"ifFragments"` is adding a lot more '__typename' than the above "ifPolymorphic" and "isAbstract" and will be removed in
   * a future version. If you require '__typename' explicitely, you should add it to your queries.
   **/
  addTypename.set("ifFragments")
}

If you're not using the normalized cache, "isPolymorphic" is certainly a good value. If you are, "ifAbstract" is safer (and also considerably simpler). You can read more about it in the design doc.

There's still a default "ifFragments" value that is kept for backward compatibility. The current plan is to change the default to "ifAbstract somewhere down the road, depending on feedbacks.

Let us know what you think!

@martinbonnin martinbonnin added the ✔️ Fixed in SNAPSHOTs The fix has been merged and is available in SNAPSHOTs, and will be available in the next release label Mar 21, 2022
@AnkitPatanaik
Copy link
Author

Thanks for fixing this @martinbonnin. This is great!

@AnkitPatanaik
Copy link
Author

AnkitPatanaik commented Mar 23, 2022

I'll go ahead and close this too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✔️ Fixed in SNAPSHOTs The fix has been merged and is available in SNAPSHOTs, and will be available in the next release ✨ Type: Feature
Projects
None yet
Development

No branches or pull requests

2 participants