Contentful logo

Contentful Community

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?

2 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

Same boat here – can anyone from Contentful chime in?

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).

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.

4 Likes

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

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.

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.