-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[source-contentful] handling undefined values #1517
Comments
I noticed this is also the case using [source-wordpress] |
From the stack trace it looks like the error is in your gatsby-node.js file. So the query is succeeding. Correct? |
I have this query in my
If I have both the url and name fields defined in contentful, everything works fine. If one of the fields is empty however, then |
That's true, the query will fail if the field does not exists in the node's GraphQL schema. Do you expect it to work like a reference to an undefined Javascript's Object property would ? It yes, then I don't think that's Gatsby related, the answer lays into the deep understanding of GraphQL's Core. |
Good point @sebastienfi . Your comment made me dig a little. |
It seems to me that this is the answer. It is not in the response because Contentful seems to ommit fields which has either null or empty value, so Gatsby and GraphQL both doesn't know it exists. |
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 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! |
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. |
@MarcCoet i'm trying to take a stab at this now, let me know if you've already made progress though. |
Awesome @dan-weaver . No unfortunately August is just a long rush for me. No time to investigate. =/ |
And BTW, if you have any thought about #1703 . I'd love to read it. |
👋 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. |
@Khaledgarbaya thanks. I've been procrastinating.. Will let you know if I have any questions! thanks for the examples |
Hey @dan-weaver , any news from your side? |
@MarcCoet hey sorry for the lack of update. I tried for a bit but probably need some guidance. Also was having trouble with gatsby's development workflow.. Mosty just nuisances though in that regard. My biggest question at this point is where should this be done Options as far as I can tell:
I think I'm lacking some understanding of how gatsby constructs its GraphQL types. |
Maybe @KyleAMathews could give you some clues? |
Yeah that'd be great. I know this probably isn't huge priority, but it would be a good learning experience.. I have some other plugins I'd like to write in the future. Hopefully my question above isn't too vague. |
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. |
Trying to use contentful as a front-end for my wife's blog. I also can't rely on her to always use every field defined in the content schema. Would be great if there was an intuitive solution for this. |
The best way to handle these cases for possibly undefined fields is, as Kyle mentioned, to use the setFieldsOnGraphQLNodeType api to explicitly define the fields so they exist whether or not they are empty |
Thanks folks. Just FYI @MarcCoet I'm going to set aside some time to dive into this again today / tomorrow, but don't let me be your blocker if it's urgent. And let me know if you've already got something. |
If I am not wrong you can here check the fields of the entry and the fields in the contentType, if one the of the fields does not exist you set it to null or something similar. |
@Khaledgarbaya I had been going down that route, but to no avail. See my "option 2 above". Someone please correct me if I'm wrong, because I'd love to know. But from looking at the code, it seems Gatsby would not have enough information about the field type if it's simply given a null value there. As far as I could tell the fields were just being dropped when i did that. |
@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. |
@MarcCoet That's Great news, feel free to mention me in the PR and I'll try to help there |
@MarcCoet Still no progress about his issue? |
Hey @Unforgiven-wanda the PR has been left hanging because we need to implement some tests and I don't know anything about tests. |
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 :( |
@stefanoverna would love to see you using Gatsby for every project! @pieh is working on a fix in #3344 — perhaps you could chat with him to see if you could help out? He's got a branch he's working on that you could look at. |
Is @pieh's schema solution being looked at as the path forward vs. altering gatsby-source-contentful to generate schemas from the Contentful's content model? @MarcCoet is there any way to use your untested plugin in the meantime? This is a showstopper for us, so would love to have a solution even if it's a little shaky 🙂 |
@SeanRoberts complete solution would allow gatsby-source-contentful to use content model to generate schema - my "solution" was just proof of concept that it can feasibly be done and actually work. I decided to just use schema definition language there but it could be expanded to add api for plugins to define schema types programatically. |
Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help! |
For anyone else stumbling across this it's still open/tracked here: #3344 |
Hey Folks, https://medium.com/@Zepro/contentful-reference-fields-with-gatsby-js-graphql-9f14ed90bdf9 |
@Khaledgarbaya this works great for optional content types but we quickly run into the issue again if one of those types have optional fields that are undefined |
This is still an issue |
For me its an issue as well. If i have 300 posts and i dont choose a category (or other kind of field) at least in 1, i will have the issue. |
You should try the gatsby-plugin-schema-snapshot. I personally still have issues with Contentful but we are definitely heading in the right direction. |
I'm still having issue with graphql schema when I run query against non-existing data(Editor might have unpublish the data from Contentful). Any workaround for it???? |
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
ornull
but now the request to Contentful seems to fail.The text was updated successfully, but these errors were encountered: