datocms / gatsby-source-datocms

Official GatsbyJS source plugin to pull content from DatoCMS
MIT License
140 stars 50 forks source link

Graphql throwing error when adding structuredtext field to block after it was created #152

Closed Hless closed 3 years ago

Hless commented 3 years ago

I have an optional structured text field in my block model. I added this field after already having created various blocks of this type in the DatoCMS editor. When querying the field with GraphQL I get the following error when querying blocks that were not changed since adding the field:

TypeError: Cannot read property 'document' of null

  - structuredText.js:90 resolveForSimpleField
    [project-web]/[gatsby-source-datocms]/hooks/sourceNodes/createTypes/item/fields/structuredText.js:90:51

  - index.js:89 resolver
    [project-web]/[gatsby-source-datocms]/hooks/sourceNodes/createTypes/item/index.js:89:20

  - resolvers.ts:569 resolver
    [project-web]/[gatsby]/src/schema/resolvers.ts:569:20

  - resolvers.ts:569 resolver
    [project-web]/[gatsby]/src/schema/resolvers.ts:569:20

  - resolvers.ts:569 wrappedTracingResolver
    [project-web]/[gatsby]/src/schema/resolvers.ts:569:20

Query:

query MyQuery {
  allDatoCmsMyBlock {
    nodes {
      myStructuredTextField { // This throws
        value
      }
      myOtherField
    }
  }
}

Packages:

  "gatsby": "3.1.2",
  "gatsby-source-datocms": "2.6.8",
Hless commented 3 years ago

If I understand correctly this issue occurs because the field is null on the CMS side, when I do the following actions in DatoCMS the build error goes away:

  1. Add whitespace
  2. Press save
  3. Remove the whitespace
  4. Press save

This is a bit of a tedious operation if you have 20 pages with 3 translations each for example. In my opinion the query should return null/undefined, or { value: null } for the field, instead of throwing an error.

stefanoverna commented 3 years ago

Hey @Hless, sorry to hear that, could you please give us the ID of a DatoCMS record with the wrong value for the Structured Text field? I'd like to inspect it myself and see exactly how we can fix them. Thanks!

stefanoverna commented 3 years ago

Nevermind, I was able to reproduce it myself! This should be fixed in v2.6.10!

Hless commented 3 years ago

Great stuff! Thanks for the quick reactions