[gatsby-source-contentful] Explicitly typing RichText references field results in it always being null
See original GitHub issueDescription
I know that there’s currently a limitation with the source-contentful plugin where the generated schema only represents content that actually exists.
I would prefer to explicitly type the schema, rather than rely on dummy content, but I’m now stuck on trying to figure out how to type RichText fields, specifically the references field.
I’ve used gatsby-plugin-schema-export to get the schema gatsby generates, so I can base it on that, and come up with this (not proper syntax, edited for brevity):
union RichTextReferenceTypes = ContentfulAsset | ContentfulFoo | ContentfulBar
type ContentfulAsset implements Node {
...fields
}
type ContentfulFooBody {
raw: String
references: [RichTextReferenceTypes]
}
type ContentfulBarBody {
raw: String
references: [RichTextReferenceTypes]
}
type ContentfulFoo implements Node{
body: ContentfulFooBody!
}
type ContentfulBar implements Node {
body: ContentfulBarBody!
}
This kinda works, in that GraphiQL correctly shows the ContentfulAsset, ContentfulFoo, and ContentfulBar types as possible elements of the references field.
The problem is that the references field is now always null when I query.
This is especially strange, because looking at the structure of everything in GraphiQL, it looks identical to the structure that the source plugin created by itself from dummy content that contained all these reference types, and as far as I can tell, I’ve recreated the schema exactly as the exported schema is.
The only difference I can see is that the gatsby-inferred schema creates a union type that’s called ContentfulAssetContentfulFooContentfulBarUnion (which is why I’m trying to explicitly replace this, so the name isn’t based on content types). Besides that, if, for example, I only had 1 ContentfulFoo entry that only linked to a ContentfulAsset, the ContentfulFooBody reference field would be typed just as [ContentfulAsset] (which is the known limitation I’m trying to work around).
I’ve even tried just using ContentfulAssetContentfulFooContentfulBarUnion as the type for the references fields (which would make the explicit type exactly the same as the inferred type from what I can tell), and still just get null, even when querying content types that have full dummy data and were working perfectly when the type was inferred.
Steps to reproduce
- Setup contentful space with a few content types that have rich text fields.
- Create an entry of each of those types, for one of them, add references to all content types (embed or hyperlink), for the other, embed only an asset.
- Explicitly type the schema as above
- Try to query references field of any of the rich text fields
If needed, I can try to set up a repro in a fresh repo off the contentful blog starter.
Expected result
Can query with something like
allContentfulFoo {
raw
references {
... on ContentfulAsset {
...AssetFields
}
... on ContentfulFoo {
...FooFields
}
... on ContentfulBar {
...BarFields
}
}
}
and references will be an array of whatever ContentfulAsset, ContentfulFoo, and ContentfulBar references exist in the source content.
Actual result
References is null, regardless of source content.
Environment
Note: I’m using Chrome, not sure why it only finds Edge, but I don’t imagine the browser makes any difference here as the issue is with GraphQL
System: OS: Windows 10 10.0.18363 CPU: (8) x64 Intel® Core™ i5-8250U CPU @ 1.60GHz Binaries: Node: 10.13.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD Browsers: Edge: 44.18362.449.0 npmPackages: gatsby: ^2.30.1 => 2.30.1 gatsby-background-image: ^1.3.1 => 1.3.1 gatsby-image: ^2.9.0 => 2.9.0 gatsby-plugin-graphql-codegen: ^2.7.1 => 2.7.1 gatsby-plugin-manifest: ^2.10.0 => 2.10.0 gatsby-plugin-material-ui: ^2.1.10 => 2.1.10 gatsby-plugin-offline: ^3.8.0 => 3.8.0 gatsby-plugin-react-helmet: ^3.8.0 => 3.8.0 gatsby-plugin-schema-export: ^1.1.2 => 1.1.2 gatsby-plugin-sharp: ^2.12.0 => 2.12.0 gatsby-plugin-typescript: ^2.10.0 => 2.10.0 gatsby-source-contentful: ^4.4.1 => 4.4.1 gatsby-source-filesystem: ^2.9.0 => 2.9.0 gatsby-transformer-remark: ^2.14.0 => 2.14.0 gatsby-transformer-sharp: ^2.10.0 => 2.10.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (2 by maintainers)
Top GitHub Comments
Hey again!
It’s been 60 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it. Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m
HUMAN_EMOTION_SORRY
. Please feel free to comment on this issue or create a new one if you need anything else. As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!Thanks again for being part of the Gatsby community! 💪💜
Did anyone figure out what was going on with this? I’m having this issue as well