Why do required fields appear as nullable in the GraphQL graph?

If I set a field as required and create content for it I would expect that it would appear as non-nullable in the graph and yet it doesn’t. Any idea why that would be?

8 Likes

We’re dealing with the same issue. Basically we’d like “required” on Contentful to equate to non-nullable in terms of the graphql schema, but that’s not how it works.

We are using gatsby-source-contentful, and the issue is on the Contentful side here… Context: https://twitter.com/KrComet/status/1310734920080211969

2 Likes

Same boat here – can anyone from Contentful chime in?

1 Like

Hi,

The reason why all fields (even those that are required) are nullable is that the preview content is served through the same schema, and in preview mode, there are no validations (a required fields might be empty).

1 Like

Is it possible, that you return the production schema only?
Maybe with a header parameter “production=true”?
This would be a massive improvement!

Edit: As an alternative it could help, to generate and make it possible to download a production schema file.

1 Like

2 separate schemas would be a great solution. Without this it’s nearly impossible to properly use typescript with Contentful!

1 Like

An inaccurate schema, providing inaccurate types really undermines a lot of the point of using typescript. We can’t accurately type anything downstream because every single value is nullable. Being able to provide an accurate schema to production would be a massive improvement. Without it we’re having to put a lot of time into finding hacky typescript workarounds with explanatory comments as to why the problem doesn’t seem to make sense.

2 Likes

Having a separate production schema could be useful in some scenarios, but wouldn’t work great for codebases that build against both the prod and preview APIs for different environments. Would it be reasonable to support optionally enabling validation in the preview API? Unpublished content would still show up, it would just have to be in a valid state first which seems like an acceptable tradeoff for those who want to opt-in to greater safety.

Any news on this? This is pretty annoying since practically all types are generated like possible undefined and you have to null check every single value.

Any workarounds? I am using GraphQL Codegen

Same as Victor… Would love an update

Any updates on this?

Just want to chime in here. Being able to have required fields be non-null in graphql is absolutely essential to being able to use typescript correctly with this API. I would much rather have my preview queries fail if required fields are missing than have to deal with fields falsely marked as nullable (this is the correct behavior anyway imo, since a preview of my page should act the same as a production request, where the fields are required).

2 Likes

I, too, am running into this issue and would also prefer to have my preview queries fail if fields that I marked as required in Contentul are missing. Thank you :slight_smile:

1 Like

Running into the same problem, we are generating types based on the schema and queries but having everything nullable is quite annoying :slight_smile:

1 Like

New to contentful and currently have the same issue.
Found this also when googling for a solution -

Hello,
I’m also new to Contentful and I also ran into this issue quite fast. All those optional fields make it quite difficult to work with the models on the client side.

I see DatoCMS have an option the retrieve the “non preview” schema where the required field are respected.

Is there something similar in Contentful?

cheers

Any updates on this and getting the production schema?

1 Like

Same issue.
Moreover I dont see a point of having an array of nullable items, even with preview limitations can’t think up a reason why [Article]! can’t be [Article!]!

Would be very helpfull if you sort out this issue. Thank you.

So far, I’ve found Contetful to be really good. However, this is quite a damper to check everything for nullable states. 2 schemas are in my opinion a solution, my development schema can differ during development anyway. However, I don’t see the reason why required should not also be automatically requiered in the preview api. The way it is currently solved Contentful is hardly usable with the apollo client and typescript. Is there any solution in the near future @contentful_team ? Otherwise I would have to look for an alternative.

same issue here… any new from contentful?