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

Refine generated type for assignable fragments on abstract types #4603

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Malien
Copy link

@Malien Malien commented Jan 28, 2024

Closes #4560

This is quality-of-life change to the type generation for the assignable fragments (aka. typesafe updaters). For the cases where the assignable fragment is defined on the abstract type, and is spread on a concrete implementation of that type. Issue #4560 has a more true-to-life example. But the minimal case is:

type A {}
type B {}
union C = A | B

type Query {
  a: A
}

fragment Assignable_c on C @assignable {
  __typename
}

fragment Foo on Query {
  a {
    ...Assignable_c
  }
}

Same applies to the interfaces as well.

Previously, the generated type for fragment Foo would include __isAssignable_c?: "A" (since the assignable fragment is defined on an abstract type). In some sense, this type is misleading. __isAssignable_c can never be undefined, since A always implements (is member of) abstract type C. This would require writing a gnarly validator. The validator turns out to be redundant in the end, and is just there to satisfy the type system.

This is a small patch that checks if the abstract type spread is done on the concrete subtype of the supertype (ie. fragment of union C is spread its member A). If the check is successful, the optionality of __isAssignable_c?: "A" is removed. This is a safe assumption, since the only way for A not to be a subtype of C:

  • is to it remove A as a member of union C (if C is a union)
  • or to remove conformance of A to the interface C (if C is an interface)

Both of which are breaking schema changes anyhow.

@facebook-github-bot
Copy link
Contributor

Hi @Malien!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@Malien
Copy link
Author

Malien commented Jan 28, 2024

Also added a test to make sure the intended behaviour for this typename optionality, explained here, is not broken by me or anyone else.

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

Successfully merging this pull request may close these issues.

Type-safe updaters: non-assignability of unions
2 participants