question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[gatsby-source-contentful] - Contentful Reference fields only allow to query for fragments/types that are in use instead of all possible.

See original GitHub issue

Description

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: c1 (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 image

  • If I try to query any other content type I get a GraphQL Error on build/develop image 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:

image (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:closed
  • Created 3 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
EdPike365commented, Feb 12, 2021

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

1reaction
lnilyacommented, Aug 4, 2020

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

References | Contentful Help Center
In Contentful, content is structured using references. References are links between types of content that allow you to reuse content over and over...
Read more >
Content Delivery API | Contentful
The Content Delivery API (CDA), is a read-only API for delivering content from Contentful to apps, websites and other media.
Read more >
Filter API results with relational queries - Contentful
It is important to understand that search on reference fields only works with entries. It cannot search assets the same way, and the...
Read more >
Linked entries and assets in Contentful
Linked assets and entries in Contentful are a mystery no more! Let's explore how to work with links in the REST and GraphQP...
Read more >
GraphQL Content API - Contentful
Disclaimer: GraphQL Content API is available on all spaces for customers on ... This field is always present and allows you to query...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found