Graphql-code-generator: GraphQLError: Unknown field '__type' on type 'Query' when using flattenGeneratedTypes

Created on 12 Jun 2020  ·  15Comments  ·  Source: dotansimha/graphql-code-generator

Describe the bug

To Reproduce
Steps to reproduce the behavior:

Create a GraphQL file that is valid GraphQL:

query FilterInputTypes {
  __type(name: "ProductAttributeFilterInput") {
    inputFields {
      name
      type {
        name
      }
    }
  }
}

See the error while generating:
https://codesandbox.io/s/distracted-yonath-hhbqi?file=/package.json

$ yarn codegen
$ graphql-codegen --config ./codegen.yml
  ✔ Parse configuration
  ❯ Generate outputs
    ❯ Generate types.ts
      ✔ Load GraphQL schemas
      ✔ Load GraphQL documents
      ✖ Generate
        → Unknown field '__type' on type 'Query'.


 Found 1 error

  ✖ document.graphql
    GraphQLError: Unknown field '__type' on type 'Query'.
        at createUserError (/sandbox/node_modules/relay-compiler/lib/core/CompilerError.js:35:1
0)
        at GraphQLDefinitionParser._transformField (/sandbox/node_modules/relay-compiler/lib/co
re/RelayParser.js:779:13)
        at /sandbox/node_modules/relay-compiler/lib/core/RelayParser.js:614:23
        at Array.map (<anonymous>)
        at GraphQLDefinitionParser._transformSelections (/sandbox/node_modules/relay-compiler/l
ib/core/RelayParser.js:610:36)
        at GraphQLDefinitionParser._transformOperation (/sandbox/node_modules/relay-compiler/li
b/core/RelayParser.js:584:27)
        at GraphQLDefinitionParser.transform (/sandbox/node_modules/relay-compiler/lib/core/Rel
ayParser.js:351:21)
        at parseDefinition (/sandbox/node_modules/relay-compiler/lib/core/RelayParser.js:327:17
)
        at /sandbox/node_modules/relay-compiler/lib/core/RelayParser.js:154:18
        at eachWithCombinedError (/sandbox/node_modules/relay-compiler/lib/core/CompilerError.j
s:93:9)
        at RelayParser.transform (/sandbox/node_modules/relay-compiler/lib/core/RelayParser.js:
151:5)
        at /sandbox/node_modules/relay-compiler/lib/core/RelayParser.js:92:19
        at Object.run (/sandbox/node_modules/relay-compiler/lib/core/GraphQLCompilerProfiler.js
:55:30)
        at Object.transform (/sandbox/node_modules/relay-compiler/lib/core/RelayParser.js:90:19
)
        at Object.optimizeDocuments (/sandbox/node_modules/@graphql-tools/relay-operation-optim
izer/index.cjs.js:24:40)
        at Object.optimizeOperations (/sandbox/node_modules/@graphql-codegen/visitor-plugin-com
mon/index.cjs.js:2618:50)
    GraphQLError: Unknown field '__type' on type 'Query'.
        at createUserError (/sandbox/node_modules/relay-compiler/lib/core/CompilerError.js:35:1
0)
        at GraphQLDefinitionParser._transformField (/sandbox/node_modules/relay-compiler/lib/co
re/RelayParser.js:779:13)
        at /sandbox/node_modules/relay-compiler/lib/core/RelayParser.js:614:23
        at Array.map (<anonymous>)
        at GraphQLDefinitionParser._transformSelections (/sandbox/node_modules/relay-compiler/l
ib/core/RelayParser.js:610:36)
        at GraphQLDefinitionParser._transformOperation (/sandbox/node_modules/relay-compiler/li
b/core/RelayParser.js:584:27)
        at GraphQLDefinitionParser.transform (/sandbox/node_modules/relay-compiler/lib/core/Rel
ayParser.js:351:21)
        at parseDefinition (/sandbox/node_modules/relay-compiler/lib/core/RelayParser.js:327:17
)
        at /sandbox/node_modules/relay-compiler/lib/core/RelayParser.js:154:18
        at eachWithCombinedError (/sandbox/node_modules/relay-compiler/lib/core/CompilerError.j
s:93:9)
        at RelayParser.transform (/sandbox/node_modules/relay-compiler/lib/core/RelayParser.js:
151:5)
        at /sandbox/node_modules/relay-compiler/lib/core/RelayParser.js:92:19
        at Object.run (/sandbox/node_modules/relay-compiler/lib/core/GraphQLCompilerProfiler.js
:55:30)
        at Object.transform (/sandbox/node_modules/relay-compiler/lib/core/RelayParser.js:90:19
)
        at Object.optimizeDocuments (/sandbox/node_modules/@graphql-tools/relay-operation-optim
izer/index.cjs.js:24:40)
        at Object.optimizeOperations (/sandbox/node_modules/@graphql-codegen/visitor-plugin-com
mon/index.cjs.js:2618:50)

Expected behavior
I would expect it to work as the __type query should be available in _any_ schema, right?

Environment:
sandbox

Additional context

bug plugins waiting-for-release

Most helpful comment

Thanks for the hard work everybody, I've been testdriving the PR for a while now and it seems to be working perfectly!

All 15 comments

This error is coming from Relay Compiiler. So I am not sure if we have anything to do with this situation.
@dotansimha What do you think?

This might be the solution, will try that out: https://github.com/facebook/relay/issues/1715#issuecomment-613086318

Ok, i've solved the introspection error in another way (exclude it from graphql-code-generator). But now I'm running into the following error:

TypeError: parentType.isTypeOf is not a function
        at SelectionSetToObject._collectInlineFragments (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/s
rc/selection-set-to-object.js:49:70)
        at SelectionSetToObject.flattenSelectionSet (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/src/s
election-set-to-object.js:169:14)
        at SelectionSetToObject._buildGroupedSelections (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/s
rc/selection-set-to-object.js:188:47)
        at SelectionSetToObject.transformSelectionSet (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/src
/selection-set-to-object.js:297:30)
        at SelectionSetToObject.buildSelectionSetString (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/s
rc/selection-set-to-object.js:262:89)
        at /Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/src/selection-set-to-object.js:199:41
        at Array.reduce (<anonymous>)
        at SelectionSetToObject._buildGroupedSelections (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/s
rc/selection-set-to-object.js:189:80)
        at SelectionSetToObject.transformSelectionSet (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/src
/selection-set-to-object.js:297:30)
        at TypeScriptDocumentsVisitor.OperationDefinition (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common
/src/base-documents-visitor.js:94:39)
        at Object.visit (/Users/paulhachmang/Sites/reachdigital-next/node_modules/graphql/language/visitor.js:242:26)
        at Object.plugin (/Users/paulhachmang/Sites/reachdigital-next/dist/plugins/typescript/operations/src/index.js:17:27)
        at executePlugin (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/cli/dist/graphql-codegen-core/src/execute-plugin.js:29:35)
        at /Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/cli/dist/graphql-codegen-core/src/codegen.js:77:30
        at Array.map (<anonymous>)
        at Object.codegen (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/cli/dist/graphql-codegen-core/src/codegen.js:67:54)
    TypeError: parentType.isTypeOf is not a function
        at SelectionSetToObject._collectInlineFragments (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/s
rc/selection-set-to-object.js:49:70)
        at SelectionSetToObject.flattenSelectionSet (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/src/s
election-set-to-object.js:169:14)
        at SelectionSetToObject._buildGroupedSelections (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/s
rc/selection-set-to-object.js:188:47)
        at SelectionSetToObject.transformSelectionSet (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/src
/selection-set-to-object.js:297:30)
        at SelectionSetToObject.buildSelectionSetString (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/s
rc/selection-set-to-object.js:262:89)
        at /Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/src/selection-set-to-object.js:199:41
        at Array.reduce (<anonymous>)
        at SelectionSetToObject._buildGroupedSelections (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/s
rc/selection-set-to-object.js:189:80)
        at SelectionSetToObject.transformSelectionSet (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common/src
/selection-set-to-object.js:297:30)
        at TypeScriptDocumentsVisitor.OperationDefinition (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/typescript-operations/dist/plugins/other/visitor-plugin-common
/src/base-documents-visitor.js:94:39)
        at Object.visit (/Users/paulhachmang/Sites/reachdigital-next/node_modules/graphql/language/visitor.js:242:26)
        at Object.plugin (/Users/paulhachmang/Sites/reachdigital-next/dist/plugins/typescript/operations/src/index.js:17:27)
        at executePlugin (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/cli/dist/graphql-codegen-core/src/execute-plugin.js:29:35)
        at /Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/cli/dist/graphql-codegen-core/src/codegen.js:77:30
        at Array.map (<anonymous>)
        at Object.codegen (/Users/paulhachmang/Sites/reachdigital-next/node_modules/@graphql-codegen/cli/dist/graphql-codegen-core/src/codegen.js:67:54)

I tried to recreate it in the sandbox but can't get it to error out. Any ideas?

@paales In your reproduction I am seeing a different error;

Interface field UserInterface.id expected but User does not provide it.

Actually this error is correct because your schema is invalid; User should implement ÙserInterface completely. Also you need to fix your configuration file as well because typescript-react-apollo needstypescript and typescript-operations plugins together with it.

I couldn't see anything related to introspection types or fields. Maybe I'm missing someting.

@ardatan

  • Whoops, fixed the UserInterface.id error.
  • I've always ran without typescript and typescript-operations and it works as expected. I've added them in the demo for convenience.

Now the only error we have is TypeError: parentType.isTypeOf is not a function:

Schermafbeelding 2020-06-15 om 10 01 43

@ardatan Can I provide any more information, would like to use interfaces, but TypeError: parentType.isTypeOf is not a function error is blocking :)

@ardatan Have you had time to take a look? Would _really_ love got get this to work if at all possible. :)

Seeing something similar here when trying to use this feature: TypeError: parentType.isTypeOf is not a function

@dotansimha Do you have any idea why this happens?

Please ignore if not useful: in our case, this throws where:

typeOnSchema = SomeInterface
parentType = SomeObjectThatImplementsSomeInterface

And the key of isParentType is set, but its value is set to undefined. I did some super light digging and it seems this is happening here:

https://github.com/ardatan/graphql-tools/blob/ae7c968deb2e6f51024a385abfc6455c0db5a5df/packages/wrap/src/wrapSchema.ts#L60

I wrapped the call to isTypeOf in a null-chain and the schema generates alright (although I didn't really check correctness, and I don't think it's correct): parentType.isTypeOf?.(typeOnSchema, null, null)

I am seeing the same TypeError: parentType.isTypeOf is not a function when I enable Relay optimizer. I added some logging and it seems than none of the parentType objects that come through have it defined.

~Could it be some peer graphql-tools dependency not implementing this? What is responsible for implementing that function?~ edit: seems @SirensOfTitan figured it out and I am just catching up 😅

@benmosher Are you planning on creating a PR by any chance? :)

Edit: Oh, apparently I didn't look.. https://github.com/dotansimha/graphql-code-generator/pull/4839

Fixed in @graphql-codegen/[email protected]. Thanks @benmosher

Thanks for the hard work everybody, I've been testdriving the PR for a while now and it seems to be working perfectly!

Was this page helpful?
0 / 5 - 0 ratings