Skip to content

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. Supports all fields types, including unions and interfaces. For schema-first GraphQL development, it allows to start quickly by generating default documents and type bindings that fetch all fields and then fine-tune and optimize.

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 skip any fragments, queries, mutations or subscriptions with the same names as your custom ones to avoid conflicts.

For example, create 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/default-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-typescript.ts to generate TypeScript types, 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 default documents generated by graphql-codegen-default-documents plugin
    'path/to/generated/default-docs.graphql',
  ],
  generates: {
    'path/to/generated/types.ts': {
      plugins: [
        'typescript',
        'typescript-operations',
        'typescript-document-nodes'
      ],
      config: {
        // TypeScript plugins config
      },
    },
  },
};

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

Acknowledgements

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors