relay icon indicating copy to clipboard operation
relay copied to clipboard

Refine generated type for assignable fragments on abstract types

Open Malien opened this issue 2 years ago • 3 comments

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.

Malien avatar Jan 28 '24 11:01 Malien

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 [email protected]. Thanks!

facebook-github-bot avatar Jan 28 '24 11:01 facebook-github-bot

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

facebook-github-bot avatar Jan 28 '24 12:01 facebook-github-bot

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

Malien avatar Jan 28 '24 12:01 Malien