Skip to content

Plugin for graphql-codegen to generate default documents (fragments, queries, mutations, subscriptions) from GraphQL schema

License

Notifications You must be signed in to change notification settings

zhitkoff/graphql-codegen-default-documents

Repository files navigation

graphql-codegen-default-documents

Plugin for graphql-codegen to generate default documents, such as operations (queries, mutations, subscriptions) as well as 'AllFields' fragments for defined types from your GraphQL schema.

Installation

  • with NPM npm i -D graphql-codegen-default-documents
  • with Yarn: yarn add --dev graphql-codegen-default-documents

Usage

This plugin should be run before other plugins that need to use documents. If you have existing custom documents and include them globally in graphql-codegen config, this plugin will not generate any fragments, queries, mutations or subscriptions with the same names as your custom ones to avoid conflicts.

For example, in your codegen-default-docs.ts file:

const config = {
  overwrite: true,
  schema: [
    'path/to/my-schema.graphql'
  ],
  documents: [
    'path/to/my-custom-documents.graphql'
  ],
  generates: {
    'path/to/generated-docs.graphql': {
       plugins: [ 'graphql-codegen-default-documents' ],
       config: {
        docsToGenerate: [ 'fragment', 'query', 'mutation', 'subscription' ],
        fragmentMinimumFields: 5,
        skipTypename: false
       },
    },
  },
  };

and run it with graphql-codegen --config codegen-default-docs.ts. Then have a second graphql-codegen config file, lets say codegen-next.ts , which points to generated default docs file from the previous one:

const config = {
  overwrite: true,
  schema: [
    'path/to/my-schema.graphql'
  ],
  documents: [
    'path/to/my-custom-documents.graphql',
    // include documents generated by graphql-codegen-default-documents plugin 
    // at the first codegen.ts file
    'path/to/generated-docs.graphql',
  ],
  generates: {
    'path/to/generated/your-other-file.ext': {
      plugins: [
        // other plugins
      ],
      config: {
        // other config
      },
    },
  },
};

and run it with graphql-codegen --config codegen-next.ts.

Acknowledgements

Inspired by https://github.com/argano/graphql-codegen-documents

TODO

  • Handle Unions and Abstract Types
  • Test cases
  • More examples in README

About

Plugin for graphql-codegen to generate default documents (fragments, queries, mutations, subscriptions) from GraphQL schema

Resources

License

Stars

Watchers

Forks

Packages

No packages published