Skip to content

Generate DocumentNode for the schema when building servers #4067

Answered by dotansimha
affanshahid asked this question in General
Discussion options

You must be logged in to vote

It should be pretty simple, you can create something like that as a custom plugin:

// my-plugin.js
const { printSchema, parse } = require('graphql');

module.exports = {
  plugin: (schema) => {
     const schemaStr = printSchema(schema);
     const schemaNode = parse(schemaStr);

     return `export const schema = ${JSON.stringify(schemaNode)}`;
  }
}

Then use it in codegen:

schema: "src/**/*.graphql" # point here to all your schema pieces, codegen will merge it for you
generates:
  ./schema.ts:
     plugins:
       - ./my-plugin.js

@affanshahid

For more info about custom plugins: https://graphql-code-generator.com/docs/custom-codegen/index

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dotansimha
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants