dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.86k stars 1.33k forks source link

Cannot add meta types or fields to schema #3461

Open mjbcopland opened 4 years ago

mjbcopland commented 4 years ago

Describe the bug As described in https://github.com/dotansimha/graphql-code-generator/issues/2911#issuecomment-561019738 and https://github.com/dotansimha/graphql-code-generator/issues/3459#issuecomment-583487477, we're supposed to add meta types and fields to the schema ourselves for use with codegen. However that doesn't work as the schema fails validation:

Error: Name "__schema" must not begin with "__", which is reserved by GraphQL introspection.

To Reproduce Steps to reproduce the behavior:

  1. My GraphQL schema:
type Query {
  __schema: __Schema!
}
  1. My GraphQL operations:
query Query {
  __schema {
    __typename
  }
}
  1. My codegen.yml config file:
schema:
  - src/schema.graphql

documents:
  - src/document.graphql

generates:
  src/types.ts:
    plugins:
      - typescript

Expected behavior

Types successfully generated for the meta query.

Environment:

dotansimha commented 4 years ago

The current behaviour of the codegen is to ignore fields that starts with __ because they are usually GraphQL internals. I wasn't sure if it possible to extend those. Do you have a spec/issue related to it? because it seems like this error is coming from GraphQL validation method and not from the codegen.

mjbcopland commented 4 years ago

This is related to my previous issue #3459. Codegen does ignore meta fields, except for the function isUsingTypes used by the import-types and near-operation-file presets. I opened an issue and a PR (#3460) around this and both were closed on the basis that I should add meta fields to the schema myself.

This issue is to document/track that doing so results in a GraphQL validation error as you mentioned, and hopefully reopen the discussion on my previous issue.