optional property vs. nullability? #8612
Unanswered
mdecurtins
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've been working with GraphQL for a few years and we've recently started integrating
graphql-code-generator
into our React projects at my suggestion. I'm digging a little bit more into how types are generated (also using the newtyped-document-node
for my Apollo hooks), and I'm a little bit confused about how codegen treats optionality vs nullability.Forgive me if this is a silly question; I've done a bit of googling but haven't yet found an explanation.
In GraphQL, we might have a type definition like this:
Which codegen would turn into:
Here's my question: why is it
bar?
and notbar
? In other words, why does the generated type think thatbar
is an optional property onFoo
, when the GraphQL schema merely declared thatbar
might be null?Aren't these 2 different things? In Typescript,
prop?
indicates thatprop
might not exist on an object that is assignable to the type, but the GraphQL SDL by definition is saying, "I guarantee these fields will exist." After all, if a resolver doesn't return the fields specified by the type, GraphQL should error, because the resolver didn't fulfill the contract promised by the schema.Can anyone explain why codegen does this? It looks like there's an
avoidOptionals
config for codegen (which I will definitely use), but I'm curious why the default isfalse
when it results in types that seemingly confuse the optionality of a field with the potential for that field to be null.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions