Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get type name from ThunkComposer #33957

Closed
2 tasks done
marcomiduri opened this issue Nov 12, 2021 · 14 comments · Fixed by #34089
Closed
2 tasks done

Cannot get type name from ThunkComposer #33957

marcomiduri opened this issue Nov 12, 2021 · 14 comments · Fixed by #34089
Labels
status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting. type: bug An issue or pull request relating to a bug in Gatsby

Comments

@marcomiduri
Copy link

marcomiduri commented Nov 12, 2021

Preliminary Checks

Description

I have enabled server-side rendering and DSG for our shop product pages. Page fetches the product review information in real-time using server-side rendering. After I enable server-side rendering, I am getting this error on build process.

Error: Cannot get type name from ThunkComposer({ _thunk: [function ], _typeName: "ContentBlock" })

This ContentBlock is a custom schema interface that I created using the createSchemaCustomization API.

FYI, it's working fine on development mode.

Reproduction Link

Minimal Reproduction Repository

Steps to Reproduce

  1. Run gatsby build

Expected Result

Build should be done without any problems

Actual Result

3:19:48 PM: error /opt/build/repo/.cache/query-engine/index.js:130612
3:19:48 PM:       throw new Error(`Cannot get type name from ${(0,_misc__WEBPACK_IMPORTED_MODULE_2__.inspect)(type)}`);
3:19:48 PM:             ^
3:19:48 PM: error Error: Cannot get type name from ThunkComposer({ _thunk: [function ], _typeName: "ContentBlock" })
3:19:48 PM:     at getComposeTypeName (/opt/build/repo/.cache/query-engine/index.js:130612:13)
3:19:48 PM:     at ObjectTypeComposer.hasInterface (/opt/build/repo/.cache/query-engine/index.js:126738:93)
3:19:48 PM:     at ObjectTypeComposer.addInterface (/opt/build/repo/.cache/query-engine/index.js:126743:15)
3:19:48 PM:     at /opt/build/repo/.cache/query-engine/index.js:206241:56
3:19:48 PM:     at Array.forEach (<anonymous>)
3:19:48 PM:     at mergeTypes (/opt/build/repo/.cache/query-engine/index.js:206241:26)
3:19:48 PM:     at /opt/build/repo/.cache/query-engine/index.js:207100:7
3:19:48 PM:     at Array.forEach (<anonymous>)
3:19:48 PM:     at parseTypes (/opt/build/repo/.cache/query-engine/index.js:207086:19)
3:19:48 PM:     at /opt/build/repo/.cache/query-engine/index.js:206133:23
3:19:48 PM:     at Array.forEach (<anonymous>)
3:19:48 PM:     at addTypes (/opt/build/repo/.cache/query-engine/index.js:206120:9)
3:19:48 PM:     at updateSchemaComposer (/opt/build/repo/.cache/query-engine/index.js:205982:9)
3:19:48 PM:     at buildSchema (/opt/build/repo/.cache/query-engine/index.js:205925:9)
3:19:48 PM:     at build (/opt/build/repo/.cache/query-engine/index.js:24422:18)
3:19:48 PM:     at GraphQLEngine._doGetRunner (/opt/build/repo/.cache/query-engine/index.js:448573:5)

Environment

System:
  OS: Windows 11
Binaries
  Node: v16.3.0
  NPM: v7.15.1
Gatsby: v4.1.4

Config Flags

No response

@marcomiduri marcomiduri added the type: bug An issue or pull request relating to a bug in Gatsby label Nov 12, 2021
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Nov 12, 2021
@graysonhicks graysonhicks added gatsby 4 status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting. and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Nov 12, 2021
@LekoArts
Copy link
Contributor

Hi!

Sorry to hear you're running into an issue. To help us best begin debugging the underlying cause, it is incredibly helpful if you're able to create a minimal reproduction. This is a simplified example of the issue that makes it clear and obvious what the issue is and how we can begin to debug it.

If you're up for it, we'd very much appreciate if you could provide a minimal reproduction and we'll be able to take another look.

Thanks for using Gatsby! 💜

@marcomiduri
Copy link
Author

marcomiduri commented Nov 16, 2021

@LekoArts Thanks for checking this issue. I just attached the minimal reproduction. Do I need to add the .env file as well?

https://github.com/marcomiduri/gatsby-issue-reproduction

@graysonhicks
Copy link
Contributor

@marcomiduri Yes, if the .env file is needed to reproduce, please add it. Thanks!

@jroehl
Copy link

jroehl commented Nov 18, 2021

Hi @graysonhicks - I have a similar issue:

I am using gatsby-source-contentful and need to fix a type on an existing (automatically) created type by letting it implement an interface:

interface SubPage {
  contentful_id: String!
  node_locale: String!
  slug: String
  subPages: [SubPage]
  dynamicSubPageKeys: [String]
}

type ContentfulPage implements SubPage {
  subPages: [SubPage]
}

type ContentfulPureContent implements SubPage {
  subPages: [SubPage]
}

Everything works fine with gatsby develop.
During gatsby build it fails at Validating Rendering Engines with the error
Error: Cannot get type name from ThunkComposer({ _thunk: [function ], _typeName: "SubPage" }).

Apparently, it has something to do with adding an interface - or even fixing a type on an existing (inferred) type.

It would be great if you could look into it ...
Thanks for all the great work!

@marcomiduri
Copy link
Author

@graysonhicks I have committed the .env file with test sanity project access. You should be able to replicate the error on your side. https://github.com/marcomiduri/gatsby-issue-reproduction/blob/master/.env

@marcomiduri
Copy link
Author

Hi, @LekoArts @graysonhicks Just wanted to check if this issue is still progressing

@graysonhicks
Copy link
Contributor

I haven't had a chance to retest, but I wonder if this is related to using fs to load the schema in the schema customization step.

@LekoArts
Copy link
Contributor

LekoArts commented Nov 22, 2021

You can't do this with engines as it doesn't support bundling these files:

https://github.com/marcomiduri/gatsby-issue-reproduction/blob/50bbe785b4abcd0e92f82a4e16f0b1ee3b255295/gatsby-node.js#L17-L20

You'll need to put the definition either in a JS file and import it or directly put it into gatsby-node.js

(So this is not a bug but a limitation)

@marcomiduri
Copy link
Author

marcomiduri commented Nov 22, 2021

@LekoArts @graysonhicks Thanks for your immediate response. I just removed schema.graphql file and moved the graphql code into gatsby-node.js. Please check my last commit. But it's the same...

@LekoArts
Copy link
Contributor

LekoArts commented Nov 23, 2021

Referencing those issues:
#20969 (comment)
#29100 (comment)


I'm not really understanding why you're redefining the types there as it already correctly creates a union:

image

But anyways, if you want to have a custom union, you need to define it this way:

exports.createSchemaCustomization = ({ actions, schema }) => {
  const { createTypes } = actions

  const typeDefs = `    
    type SanityBifold {
      id: ID!
      _type: String!
      _key: String!
    }
    
    type SanityCallToAction {
      id: ID!
      _type: String!
      _key: String!
    }
    
    type SanityPage implements Node {
      contentBlocks: [ContentBlocks]
    }
  `

  const ContentBlocks = schema.buildUnionType({
    name: `ContentBlocks`,
    types: [`SanityBifold`, `SanityCallToAction`],
    resolveType: (value, info) => {
      if (value._type === `bifold`) {
        return `SanityBifold`
      } else {
        return `SanityCallToAction`
      }
    }
  })

  createTypes(typeDefs)
  createTypes([ContentBlocks])
}

Then it'll also work:

image

Successfully built:

image


So I think the validation is doing it's job here and catching a user error, not an error in Gatsby itself. So @jroehl I'm suspecting your schema definitions are also incorrect.

@marcomiduri
Copy link
Author

marcomiduri commented Nov 23, 2021

@LekoArts Thank you very much for this code snippet. But I have to define ContentBlock interface for this project. I have pushed a new commit to my reproduction repository. Can you please review that? I followed this for creating interface but I am still getting the same error. I think this comes from my lack of graphql knowledge. Anyway would be appreciated if you can help me with this.

My last commit URL: marcomiduri/gatsby-issue-reproduction@a6a6081
Comment URL: #20969 (comment)

Thank you

@LekoArts
Copy link
Contributor

LekoArts commented Nov 24, 2021

@marcomiduri

But I have to define ContentBlock interface for this project.

Why do you have to do that?


So in gatsby develop this works:

exports.createSchemaCustomization = ({ actions, schema }) => {
  const { createTypes } = actions

  const typeDefs = `
    type SanityBifold implements ContentBlock {
      _type: String!
      _key: String!
    }
    
    type SanityCallToAction implements ContentBlock {
      _type: String!
      _key: String!
    }

    type SanityPage implements Node {
      contentBlocks: [ContentBlocks]
    }
  `

  const contentBlockInterface = schema.buildInterfaceType({
    name: `ContentBlock`,
    fields: {
      _type: `String!`,
      _key: `String!`,
    },
    resolveType: value => value._type,
  })

  const contentBlocksUnion = schema.buildUnionType({
    name: `ContentBlocks`,
    types: [`SanityBifold`, `SanityCallToAction`],
    resolveType: (value) => {
      if (value._type === `bifold`) {
        return `SanityBifold`
      } else {
        return `SanityCallToAction`
      }
    }
  })

  createTypes([contentBlockInterface, typeDefs, contentBlocksUnion])
}

image

I'm not sure why you need ContentBlock to be an Interface and what you want to do with it. Depending on how you want to use it your configuration still might be wrong.

@marcomiduri
Copy link
Author

marcomiduri commented Nov 24, 2021

@LekoArts I have defined a fragment called ContentBlockBase and used it on all block fragment like below:

  fragment ContentBlockBase on ContentBlock {
    _type
    _key
  }
  fragment Blocks on ContentBlocks {
    ...ContentBlockBase
  }

So we don't need to write _type, _key fields everytime. Hope it makes sense!

I have run it using gatsby develop and it worked but gatsby build is erroring the Error: Cannot get type name from ThunkComposer({ _thunk: [function ], _typeName: "ContentBlock" }) again.

@pieh
Copy link
Contributor

pieh commented Nov 26, 2021

I have minimal repro for error like this to happen during regular schema building in https://github.com/pieh/i33957-mini/blob/c8a1c27fcd8a6400fc2585daded1387c116e5847/gatsby-node.js

Not yet sure where the problem is, but this at least it seems to simulate what happens when engines validation happen and should make it easier to debug moving forward (it doesn't have any plugins other than internals so there is less "noise" in there)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting. type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
5 participants