Describe the bug
When trying to use type __Schema!, graphql-code-generator throws the following error: Unknown type "__Schema". after "Generate".
To Reproduce
type Project {
schema: __Schema!
}
type Project {
schema: __Schema!
}
N/A
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:
@graphql-codegen/...: 1.7.0Additional context
Attempting to proxy an introspection from another GraphQL endpoint, not 100% it is going to work as expected.
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
}
@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.
Most helpful comment
@rlindskog I think
graphql.jsitself doesn't have introspection type definitions inGraphQLSchemaobject 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
schemafield in yourcodegen.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.graphqlwhich is seperated from the actual type definitions. Then add it incodegen.yml;This sounds a good solution for me. What do you think @dotansimha ?