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] Explicitly typing RichText references field results in it always being null

See original GitHub issue

Description

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

  1. Setup contentful space with a few content types that have rich text fields.
  2. 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.
  3. Explicitly type the schema as above
  4. 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:closed
  • Created 3 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
github-actions[bot]commented, Jul 18, 2021

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! 💪💜

0reactions
DarthFloopycommented, Aug 12, 2022

Did anyone figure out what was going on with this? I’m having this issue as well

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Rich Text with the Contentful Source plugin
How to render and apply Rich Text fields with Gatsby and Contentful | Guide to querying Rich Text content with GraphQL.
Read more >
Gatsby: Inject a field in a graphql query before executing it ...
In essence, I want to develop a gatsby-plugin that modifies all contentful-related graphQL queries to always insert the contentful_id so the ...
Read more >
gatsby-source-sanity
This will help the plugin in knowing which types and fields exists, so you can query for them even without them being present...
Read more >
Gatsby Contentful Rich Text Changes | Medium - Ed Pike
The gatsby-source-contentful plugin Rich Text changed from having one “json” field to a “raw” and “references” array.
Read more >
Gatsby Changelog | 5.3.0
Now, when a code update or data update is made for the <Header> component, only the HTML for the Slice will be updated,...
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