Relay-compiler-language-typescript: All (including required) GraphQL fields are optional when using inline fragments

Created on 17 Jun 2020  路  3Comments  路  Source: relay-tools/relay-compiler-language-typescript

fragment MyComponent_foo on Foo {
  ... on Bar {
    id
  }
}

Even if Bar has id set as required in the GraphQL schema, all props, including these GQL required fields, will be optional in the generated interface.

Most helpful comment

This is expected behavior - as when the returned type is not of type Bar (but some other Foo) you only get an empty object back.

All 3 comments

This is expected behavior - as when the returned type is not of type Bar (but some other Foo) you only get an empty object back.

@kastermester thanks for the answer. I still find it a bit awkward when required fields from the graphql schema is suddenly all turned into optional props in the generated types and you have to have undefined checks for each of the "required" fields in your components.

I wonder why all the props needs to be possibly undefined, and not just foo const foo = useFragment(graphql`MyComponent_foo on Foo { ... }`, props.foo); - but not the fields inside.

But if this is the intended behavior, thanks for clarifying.

why not a fragment on Bar?

Was this page helpful?
0 / 5 - 0 ratings