How to make fields optional for graphql api?

Kind of analogous to the way hide_fields is used, is there a way I can allow a field to be nil for the Graphql API when it is not allowed to be nil as an attribute?
6 Replies
ZachDaniel
ZachDaniel2y ago
well, if its not allowed to be nil for the attribute, what should happen if you let the graphql supply it? or rather, if you let the graphql not supply it then you'd have a nil value in a non nil field right?
shyan
shyanOP2y ago
I am automatically generating types for my typescript react app using graphql-codegen based on the graphql schema. If I don't query for that particular field, my result is mismatched with the type it corresponds to, because the type wants to make sure the field is on the result. I guess what I really want to do is figure out a way for the graphql schema that's automatically generated to say, hey, it's ok not to include this field. Hopefully that makes more sense? Apologies, probably not doing the best job of expressing what I want.
ZachDaniel
ZachDaniel2y ago
I think that might be an issue with your codegen setup TBH A field is allowed to be nil if it’s not included in the request
shyan
shyanOP2y ago
Yeah, I think you're right. Apologies, I went barking up the wrong tree hah
ZachDaniel
ZachDaniel2y ago
No worries 😄 Good to have in the support channel for any others encountering a similar issue
shyan
shyanOP2y ago
For posterity: I ended up using the specific result type of my query, rather than the generic type of the resulting resource, which was churned out by graphql-codgen. The optionality of the field I had in mind was supported by the specific result type, thereby solving my problem.

Did you find this page helpful?