Same boat here – can anyone from Contentful chime in?
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.
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.
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).
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
Running into the same problem, we are generating types based on the schema and queries but having everything nullable is quite annoying
New to contentful and currently have the same issue.
Found this also when googling for a solution -
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?
Any updates on this and getting the production schema?
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?
+1 to all of this.
while i understand there are perceived issues with preview and with the introduction of required fields to an existing content type (see @jl1’s share), having a TypeScript schema that doesn’t fit our application’s needs (i.e. an accurate reflection of required vs. optional fields) requires clumsy, crufty workarounds and chips away at the benefit of using GraphQL in our app.
Imagine having to check for an item you know is in the array. Not to mention all of the properties…
Unless I can find a solution, I’m likely going to have to abort my introduction of CF’s GraphQL API into our app.
Is there support for Graphql schemas that accurately reflect the production schema?
This is a deal breaker for my team because developer experience is a high priority. It’s incredibly annoying, frustrating, and time consuming to use Contentful with Typescript as it requires a ton of extra checks.
We also tried Contentful with the REST API and it’s even worse DX.