Graphql-code-generator: Receiving error: Unknown type "__Schema". after running graphql-code-generator.

Created on 7 Nov 2019  路  9Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
When trying to use type __Schema!, graphql-code-generator throws the following error: Unknown type "__Schema". after "Generate".

To Reproduce

  • create a schema with the following type:
type Project {
  schema: __Schema!
}
  • run graphql code generator from the above schema.
  1. My GraphQL schema:
type Project {
  schema: __Schema!
}
  1. My GraphQL operations:

N/A

  1. My codegen.yml config file:
overwrite: true
schema: ./packages/api/src/schema.graphql
documents: packages/**/*.graphql
generates:
  ./packages/api/src/resolvers/types.ts:
    config:
      contextType: ../types#Context
    plugins:
      - typescript
      - typescript-resolvers
  ./packages/app/src/apollo/index.tsx:
    plugins:
      - typescript
      - typescript-operations
      - typescript-react-apollo
    config:
      gqlImport: apollo-boost#gql
      withHooks: true
      withComponent: false
      withHOC: false
      reactApolloVersion: 3\

Expected behavior
To generate typescript types from the __Schema! type.

Environment:

  • OS: Mac OS
  • @graphql-codegen/...: 1.7.0
  • NodeJS: 12.9.1

Additional context
Attempting to proxy an introspection from another GraphQL endpoint, not 100% it is going to work as expected.

bug

Most helpful comment

@rlindskog I think graphql.js itself doesn't have introspection type definitions in GraphQLSchema object intentionally. As described in here;
https://github.com/graphql/graphql-js/issues/73#issuecomment-206712151

However, you can let codegen know about them by adding those type definitions into the schema field in your codegen.yml;
https://github.com/graphql/graphql-spec/blob/master/spec/Section%204%20--%20Introspection.md#schema-introspection

For example you can save introspection types in a file introspection-types.graphql which is seperated from the actual type definitions. Then add it in codegen.yml;

schema:
   - ./packages/api/src/schema.graphql
   - ./packages/api/src/introspection-types.graphql

This sounds a good solution for me. What do you think @dotansimha ?

All 9 comments

When you use __Schema, you refer to the internal type that represents the schema? or it's something you are creating?

I think it is a built-in Object Type of GraphQL like we used in introspection query;

type Query {
  __schema: __Schema
}

https://graphql.org/learn/introspection/

@rlindskog ?

@ardatan is correct. It鈥檚 from the introspection query types

Ex.

query {
  __schema {
    queryType {
      name
    }
  }
}

@rlindskog I think graphql.js itself doesn't have introspection type definitions in GraphQLSchema object intentionally. As described in here;
https://github.com/graphql/graphql-js/issues/73#issuecomment-206712151

However, you can let codegen know about them by adding those type definitions into the schema field in your codegen.yml;
https://github.com/graphql/graphql-spec/blob/master/spec/Section%204%20--%20Introspection.md#schema-introspection

For example you can save introspection types in a file introspection-types.graphql which is seperated from the actual type definitions. Then add it in codegen.yml;

schema:
   - ./packages/api/src/schema.graphql
   - ./packages/api/src/introspection-types.graphql

This sounds a good solution for me. What do you think @dotansimha ?

@ardatan I think it's a good workaround :)

Great, thanks so much @ardatan and @dotansimha!

@ardatan When generating typescript-resolvers, it's not generating the introspection types on type ResolversTypes, but it generates everything else. If I remove the double-underscore (example: type Field {...} instead of type __Field {...} it seems to work fine. (except for type Directive {...})

Error:

Property '__Type' does not exist on type 'ResolversTypes'.

@rlindskog That's normal behavior because you cannot add resolvers to meta types and fields, and double underscore means it is a meta field in GraphQL Spec.

Was this page helpful?
0 / 5 - 0 ratings