gatsby-plugin-schema-snapshot Issue with Contentful LongText fields

Created on 20 Nov 2019  路  9Comments  路  Source: gatsbyjs/gatsby

Description

I recently tried using gatsby-plugin-schema-snapshot to create an explicit snapshot of the schema for a project that uses gatsby-source-contentful. This works as expected with the exception of LongText fields in Contentful. Per the documentation LongText fields should actually be an object rather than a string.

Generated type using gatsby-plugin-schema-snapshot for a field named body which is a LongText field in Contentful.

type contentfulPostBodyTextNode implements Node @dontInfer {
  body: String
}

This causes the following part of the query to fail if the actual content doesn't exist:

 body {
  childMarkdownRemark {
    timeToRead
    html
    excerpt(pruneLength: 320)
  }
}

Steps to reproduce

  1. Select a project that uses at least one LongText field in Contentful
  2. Install gatsby-plugin-schema-snapshot and add it to the gatsby-config.js
  3. Run gatsby build and view the snapshot file created
  4. See how the LongText field is defined as a string rather than an object
not stale GraphQL bug

Most helpful comment

@ryanwiemer We should address this in the core but in the meantime, you could use @childOf directive manually for all types producing a warning like the one above.

Following worked for me with your repro:

// in site's gatsby-node.js:
exports.createSchemaCustomization = ({ actions }) => {
  const typeDefs = `
    type contentfulPostBodyTextNode implements Node
    @childOf(types: ["ContentfulPost"]) {
      id: ID!
    }
    type contentfulPageMetaDescriptionTextNode implements Node
    @childOf(types: ["ContentfulPage"]) {
      id: ID!
    }
    type contentfulPageBodyTextNode implements Node
    @childOf(types: ["ContentfulPage"]) {
      id: ID!
    }
    type contentfulPostMetaDescriptionTextNode implements Node
    @childOf(types: ["ContentfulPost"]) {
      id: ID!
    }
    type MarkdownRemark implements Node
    @childOf(types: [
      "contentfulPostBodyTextNode",
      "contentfulPostMetaDescriptionTextNode",
      "contentfulPageBodyTextNode",
      "contentfulPageMetaDescriptionTextNode"
    ]) {
      id: ID!
    }
  `
  actions.createTypes(typeDefs)
}

All 9 comments

cc @vladar @stefanprobst

Thanks, I'll look into this! If you check the field type for contentfulPostBodyTextNode.body in the graphiql ide (without gatsby-plugin-schema-snapshot) - is it defined as String?

@stefanprobst yes, contentfulPostBodyTextNode.body is defined as String without gatsby-plguin-schema-snapshot.

contentfulPostBodyTextNode

@stefanprobst is there any other useful info I can provide for this particular ticket?

Can you make a minimum reproduction? Then I can take a look. It is hard to help without actual code at hand.

@vladar

Sure, no problem. Apologies for the multi-step process but it is necessary since this involves Contentful. Let me know if this is helpful or if you need anything else.

Reproducible Test Case

  1. Clone this starter: git clone https://github.com/ryanwiemer/gatsby-starter-gcn
  2. Use the snapshot branch which has all dependencies updated and gatsby-plugin-schema-snapshot already set up.
  3. Sign up and create a new empty Contentful space
  4. Run setup script: yarn setup to create the relevant content model and dummy content
  5. Confirm everything works using yarn develop
  6. Log into Contentful and delete or archive the dummy content.
  7. Run yarn develop and you should receive an error:
    ```
    Cannot query field "childMarkdownRemark" on type "contentfulPostBodyTextNode".

GraphQL request:30:9
29 | body {
30 | childMarkdownRemark {
| ^
31 | timeToRead
````

OK, I think I know what is going on here. Our printDefinitions won't print @childOf directive. As a result, convenience child fields (like childMarkdownRemark in this case) are not included in schema snapshots and still depend on the presence of data.

I do see related warnings in the console:

warn The type `contentfulPostBodyTextNode` does not explicitly define the field
`childMarkdownRemark`.
On types with the `@dontInfer` directive, or with the `infer` extension set to `false`,
automatically adding fields for children types is deprecated.

@ryanwiemer We should address this in the core but in the meantime, you could use @childOf directive manually for all types producing a warning like the one above.

Following worked for me with your repro:

// in site's gatsby-node.js:
exports.createSchemaCustomization = ({ actions }) => {
  const typeDefs = `
    type contentfulPostBodyTextNode implements Node
    @childOf(types: ["ContentfulPost"]) {
      id: ID!
    }
    type contentfulPageMetaDescriptionTextNode implements Node
    @childOf(types: ["ContentfulPage"]) {
      id: ID!
    }
    type contentfulPageBodyTextNode implements Node
    @childOf(types: ["ContentfulPage"]) {
      id: ID!
    }
    type contentfulPostMetaDescriptionTextNode implements Node
    @childOf(types: ["ContentfulPost"]) {
      id: ID!
    }
    type MarkdownRemark implements Node
    @childOf(types: [
      "contentfulPostBodyTextNode",
      "contentfulPostMetaDescriptionTextNode",
      "contentfulPageBodyTextNode",
      "contentfulPageMetaDescriptionTextNode"
    ]) {
      id: ID!
    }
  `
  actions.createTypes(typeDefs)
}

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
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 for being a part of the Gatsby community! 馃挭馃挏

Was this page helpful?
0 / 5 - 0 ratings