Contentful Plugin: Should output complete schema regardless of whether content is available #29874
-
DescriptionWhen content types are created but are not yet populated they do not include any of their fields in the GraphQL schema, this means that we cannot write queries against those fields which will work when the data is available. This issue would arise if the content was available but subsequently unpublished or deleted. The content model can be represented in the schema by using the schema customisation API rather than using inference. Partially discussed here: #1517 Note: I appreciate this is mentioned in the plugin's readme but this is a serious drawback of the plugin. A similar fix was put in place on the Kentico Kontent source plugin last year: Steps to reproduce
Expected resultThe schema should include the full model rather than a "skeleton" structure with only the default fields. Actual resultThe type exists but does not include the full model, only the base Contentful fields. Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 18 comments 1 reply
-
The code that needs to be updated should begin here: The types are being generated but the fields are not explicitly defined. The following breakdown of field types in Contentful can be used to determine the appropriate GraphQL type. https://www.contentful.com/developers/docs/concepts/data-model/ Reference field types should be either This would allow the underlying data to be queried using inline fragments. |
Beta Was this translation helpful? Give feedback.
-
Sounds like it's the same as #12816 ? |
Beta Was this translation helpful? Give feedback.
-
@LekoArts thanks for finding this, I was searching for open issues but couldn't find any. Happy to see some progress has been made on this but it is very disheartening to see that PR has been open for over 6 months. 😫 |
Beta Was this translation helpful? Give feedback.
-
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. Thanks for being a part of the Gatsby community! 💪💜 |
Beta Was this translation helpful? Give feedback.
-
Definitely not stale, or shouldn't be... |
Beta Was this translation helpful? Give feedback.
-
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. Thanks for being a part of the Gatsby community! 💪💜 |
Beta Was this translation helpful? Give feedback.
-
Nope, not stale. |
Beta Was this translation helpful? Give feedback.
-
Why this issue is closed? @LekoArts It is a bug that should be fixed. |
Beta Was this translation helpful? Give feedback.
-
Hello, whether a bug or a feature request, this is one update that I'd really like to see. |
Beta Was this translation helpful? Give feedback.
-
I agree that this feature would be super helpful to see, especially as other CMS have started providing it like strapi. Would be super helpful to see this feature. |
Beta Was this translation helpful? Give feedback.
-
What's the status of this? Optional fields, or even multi-relational fields become dangerous with the current state of this. We just a had a client remove a section from their page, only to have the whole site break because no pages contained that particular section anymore and the GraphQL query shape changed on us. |
Beta Was this translation helpful? Give feedback.
-
As I'm updating to the latest
I agree with the general comments here that having the schema built regardless of entries existing would be super nice. But if that is too difficult and/or isn't coming super soon, what is the correct way to make your own schema for something Contentful is going to provide if you don't want GraphQL to fail the build when no entry exists for a specific type? |
Beta Was this translation helpful? Give feedback.
-
This, among other reasons, is why I avoid Contentful, and definitely avoid using it with Gatsby. It just isn't fit for purpose in a reasonably complex project. I've moved to using Next.js primarily for this kind of project, or using Kontent by Kentico as a CMS if Gatsby is being used. Much happier with the workflows for both of those. Not intended as a knock on Gatsby or Contentful but this type of issue being open for several years doesn't fill me with confidence. Open source contributions are great, but both Gatsby and Contentful have significant funding and this should have been resolved by Gatsby or Contentful developers by now. |
Beta Was this translation helpful? Give feedback.
-
This feature is still a WIP, you can follow the progress here: https://github.com/gatsbyjs/gatsby/projects/24 |
Beta Was this translation helpful? Give feedback.
-
I know this doesn't fully resolve things, but part of the difficulty I've had was knowing the types which are generated by contentful; gatsby-plugin-schema-snapshot can be helpful in showing every part of the schema, and you can use createSchemaCustomization to ensure stability in your schema. Components that have multiple types within them, which are usually accessed by a fragment in graphql - those actually reference a union of many different components. Adjusting that union to account for other types will result in them appearing in your schema - even if no instance of that type is published in contentful. Of course, nothing will appear when you run your query, but the bright side is, the entire site won't crash; it just queries empty objects. It's a workaround, I know. I agree that gatsby should offer a better solution for ensuring certain parts of the schema are constant (at least they should match the structure of the content models, not an assumption of a schema based on current data - I assume it has to do with creating as "bare minimum" of a schema as possible (and all those types really add up), but it would be better for devs to control those aspects). Creating dummy content (as is often suggested) is a terrible fix, one that not only adds more work, but can easily, and accidently, be undone - breaking the schema and the site. Here's where I found this workaround - credit to user schpet: https://stackoverflow.com/questions/61587167/customizing-gatsby-contentful-schema Hope this helps :) Let me know if you encounter any issues - I just found this today, so there may be gaps that I haven't found. |
Beta Was this translation helpful? Give feedback.
-
This is honestly such a BS bug. |
Beta Was this translation helpful? Give feedback.
-
It's hard to believe this bug exists in the first place. And that it hasn't been fixed in over a year is unbelievable. Optional fields are part of every content model and database since the beginning of time. How could they not be? To not address them or say "well just create a record with all the values" is bizarre. |
Beta Was this translation helpful? Give feedback.
-
Hi there, I have the same issue with MongoDB connector (type generation). If the field is never used in the database, the graphQL query using this field is failing the build ... (see the log extract below) [I'm using Mongo Realm to create the schema in typescript (also using the generated graphQL API in Mongo Atlas) but I didn't find an easy way of having a smooth experience with all this...]
|
Beta Was this translation helpful? Give feedback.
This feature is still a WIP, you can follow the progress here: https://github.com/gatsbyjs/gatsby/projects/24
(with #30855)