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, add new generate section before all other ones in your your-graphql-codegen-config-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
       },
    },
  },
  generates: {
    'path/to/generated/your-other-file.ext': {
      // include documents generated by graphql-codegen-default-documents plugin 
      // at the previous 'generates' step
      documents: 'path/to/generated-docs.graphql', 
      plugins: [
        // other plugins
      ],
      config: {
        // other config
      },
    },
  },
};

Acknowledgements

Inspired by https:/argano/graphql-codegen-documents

TODO

  • Handle Unions and Abstract Types
  • A bit of cosmetics (indents and new lines)
  • Optional __typename
  • 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