[gatsby-source-contentful] - Contentful Reference fields only allow to query for fragments/types that are in use instead of all possible.
See original GitHub issueDescription
A contentful reference field that can take a number of content-types will only allow me to query for the content types that are actually linked in the content. So if the content only links to 1 or 2 contentypes out of the 6 available, I can only query these 2. And worse I can only compile for querying these two. Any query addressing the possible links to other content types in the future cannot be queried. Making the reference field fairly problematic.
My workaround so far is to create “dummy contents”, each one linking to a different content type so I can include all of these types in my queries. But this is obviously very fragile and not really elegant.
Is there a way to pass some schema to gatsby-source-contentful, so that it knows beforehand that what I expect link to be is a union of the different content types as specified? Or can it pull the schema directly from contentful?
Steps to reproduce
-
I have a Reference field in Contentful, that allows to take any of the ContentTypes I defined: (The restriction doesn’t really matter, it is just to show, that these are all contentypes that work with the link, since they have a slug-property defined in our model).
-
I have 2 Contents that use this reference field one links to a Product another one to a ProductCategory. I can query those and ONLY those 2, not all of the above
-
If I try to query any other content type I get a GraphQL Error on build/develop Here I try to query on the ContentType HomePage.
We've encountered an error: Objects are not valid as a React child (found: GraphQLDocumentError: GraphQLDocumentError: Fragment cannot be spread here as objects of type "ContentfulProductContentfulProductCategoryUnion" can never be of type
"ContentfulHomePage".). If you meant to render a collection of children, use an array instead.
Expected result
I should be able to query for all possible content types.
After all Contentful does provide the types the Link-Field can have in the JSON-output for the Contenttype:
(not all types selected here, but the list is visible under “validations”)
Actual result
The graphQL error above.
Environment
OS: macOS 10.15.4
CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 14.5.0 - /usr/local/bin/node
npm: 6.14.5 - /usr/local/bin/npm
Languages:
Python: 2.7.16 - /usr/bin/python
Browsers:
Chrome: 84.0.4147.89
Firefox: 73.0.1
Safari: 13.1
npmPackages:
gatsby: ^2.24.2 => 2.24.2
gatsby-background-image: ^1.1.1 => 1.1.1
gatsby-env-variables: ^2.0.0 => 2.0.0
gatsby-image: ^2.4.9 => 2.4.13
gatsby-plugin-graphql-codegen: ^2.7.1 => 2.7.1
gatsby-plugin-manifest: ^2.4.14 => 2.4.18
gatsby-plugin-offline: ^3.2.18 => 3.2.18
gatsby-plugin-react-helmet: ^3.3.6 => 3.3.10
gatsby-plugin-sass: ^2.3.12 => 2.3.12
gatsby-plugin-sharp: ^2.6.21 => 2.6.22
gatsby-source-contentful: ^2.3.33 => 2.3.33
gatsby-source-filesystem: ^2.3.14 => 2.3.19
gatsby-transformer-remark: ^2.8.25 => 2.8.25
gatsby-transformer-sharp: ^2.5.7 => 2.5.11
npmGlobalPackages:
gatsby-cli: 2.12.60
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (2 by maintainers)
Top GitHub Comments
Since this is still open, here is a link to the version of this problem in gatsby-source-contentful v 4.6 ish. Hope this helps anyone coming along this problem in more recent times. https://edpike365.medium.com/gatsby-contentful-rich-text-migrate-to-gatsby-source-contentful-version-4-in-early-2021-321904587470
Yes this finally totally resolved my issue Thank you so much.
I had to add the array, since for some types I had multiple references.
link: [HeroSlideLink] @link(from: “link___NODE”)
Thanks a lot!