gatsby: [source-contentful] handling undefined values
Would be nice to have a way to query an entry field regardless of it being defined in contentful.
I mean if a field exists in Contentful but is not required (and empty) I would expect getting undefined or null but now the request to Contentful seems to fail.
TypeError: Cannot read property 'contentfulSettings' of undefined
at graphql.then.result (/home/marc/code/TOILE/toile.io/gatsby-node.js:120:24)
at tryCatcher (/home/marc/code/TOILE/toile.io/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/marc/code/TOILE/toile.io/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/marc/code/TOILE/toile.io/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromiseCtx (/home/marc/code/TOILE/toile.io/node_modules/bluebird/js/release/promise.js:606:10)
at Async._drainQueue (/home/marc/code/TOILE/toile.io/node_modules/bluebird/js/release/async.js:138:12)
at Async._drainQueues (/home/marc/code/TOILE/toile.io/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/home/marc/code/TOILE/toile.io/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:800:20)
at tryOnImmediate (timers.js:762:5)
at processImmediate [as _immediateCallback] (timers.js:733:5)
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 3
- Comments: 40 (32 by maintainers)
My point exactly. So what do you think about inferring the graphQL schema from the content model?
My argument is that Contentful is a CMS. It is meant to be used by editors. If a field is not required in contentful the editor will reasonably understand that the field can be empty. But it cannot as the graphQL query will fail in that case! It seems like a real problem to me.
Is there maybe a way to tell Contentful to provide explicit responses for each field, even undefined ones @Khaledgarbaya ?
@KyleAMathews is it the same issue you were mentionning here #1264?
This is still an issue
This single issue is preventing our agency to extensively use Gatsby on every new project… I would be happy to help proposing a PR but I’m not sure how to proceed 😦
Yeah Gatsby builds the schema from the data it has. The plugin does use the content schema to identify markdown fields. Would be happy to take a PR that also uses the content model to identify fields that don’t have data yet.
This just bit me as well. Is #1264 addressing this? Not sure you guys are understanding what @MarcCoet is saying? forgive me if I’m wrong.
But, it seems that if a field is specified in a query that doesn’t happen to exist in any of the contenful records returned at that time by the api, then the graphql schema is not able to service that field, because it doesn’t know it exists?
So, I’m guessing the schema is currently being build from existing records. Is that not correct? Could the schema be built instead from the content type descriptions themselves: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/content-types/content-model . I was having trouble deciphering exactly what was going on. Would love to help out with this!
Hey Folks, I haven’t tested this but this might be a p[otential Clean workaround that does not require creating dummy content.
https://medium.com/@Zepro/contentful-reference-fields-with-gatsby-js-graphql-9f14ed90bdf9
@MarcCoet That’s Great news, feel free to mention me in the PR and I’ll try to help there
@dan-weaver just to let you know asap that I think I’ve got a working solution… I hope I didn’t miss something huge because I made it quick and dirty. Basically I went Khaled’s way and set a value directly when entryNodes are built. I will post a PR as soon as possible.
So probably the best way to “fill in” missing fields in the schema would be @iw-dweaver’s (1) option to use setFieldsOnGraphQLNodeType. You’d look at each generated schema and compare the schema against the Contentful model. If there’s any missing fields, you add that field with the correct type.
Good point @sebastienfi . Your comment made me dig a little. I did not dug into the plugin code (graphQL is not my bff yet) but from my observations I am guessing we infer node’s GraphQL schema from the Contentful entries directly, right? As Contentful does not return empty fields in its response it is then absent from the schema. What if we inferred the schema from the Contentful content model instead so that we are sure that every field present in the content model is callable in Gatsby and if a specific field value is absent from the response we define it as
nullorundefinedor whatever goes well with graphQL. What am I missing?Hey @Unforgiven-wanda the PR has been left hanging because we need to implement some tests and I don’t know anything about tests. My suggestion was to add problematic cases to the test space but got no answer back. I am running the PR code in a local plugin and it is all good for me (at least for what it was supposed to solve). No progress on images and unused content types (e.g. blogPost content type exists but no blog post is created) though. I think #2518 is mentioning the same kind of issue for images
👋 hey there, Sorry I was off for two weeks, I totally agree on having the schema defined based on the content type and it should be fairly easy to that. we have a labs project that allows you to query the data stored in Contentful using graphql, A schema and value resolvers are automatically generated out of an existing space. Please check the source code because there is a lot of useful stuff there. I will be glad to answer any question related to Contentful, but unfortunately I don’t have time to make a PR for that