Documentation
Writing Plugins
Extend Schema

How to extend the GraphQL Schema?

Each plugin can also specify addToSchema field, and to extend the GraphQLSchema with more types:

module.exports = {
  plugin(schema, documents, config) {
    const typesMap = schema.getTypeMap()
 
    return Object.keys(typesMap).join('\n')
  },
  addToSchema: /* GraphQL */ `
    type MyType {
      field: String
    }
 
    directive @myDirective on OBJECT
  `
}

It’s useful when you wish to add things like declarative @directive to your GraphQLSchema, which affects only the output of the codegen.

For example, let’s add a custom @directive that tells the codegen to ignore a specific type:

module.exports = {
  plugin(schema, documents, config, info) {
    const typesMap = schema.getTypeMap()
 
    return Object.keys(typesMap)
      .filter(typeName => {
        const type = typesMap[typeName]
        const { astNode } = type
 
        if (astNode?.directives?.find(d => d.name.value === 'ignore')) {
          return false
        }
 
        return true
      })
      .join('\n')
  },
  addToSchema: /* GraphQL */ `
    directive @ignore on OBJECT
  `
}