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.
- with NPM
npm i -D graphql-codegen-default-documents
- with Yarn:
yarn add --dev graphql-codegen-default-documents
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
},
},
},
};
Inspired by https:/argano/graphql-codegen-documents
- Handle Unions and Abstract Types
- A bit of cosmetics (indents and new lines)
- Optional __typename
- Test cases
- More examples in README