GraphQL Code Generator

GraphQL Code Generator

  • Docs
  • GitHub
  • Help

›Custom Code-Generator Plugins

Getting Started

  • What is GraphQL Code Generator?
  • Codegen Options Config
  • `schema` field
  • `documents` field
  • `config` field
  • `require` field
  • Development Workflow
  • Programmatic Usage

Plugins

  • Available Plugins
  • TypeScript Common
  • Typescript Client
  • TypeScript Server
  • TypeScript Resolvers
  • TypeScript GraphQL Files Modules
  • TypeScript MongoDB
  • TypeScript React Apollo
  • Typescript Apollo Angular
  • Flow Types
  • Flow Resolvers
  • Flow Documents
  • Fragment Matcher
  • Add
  • Time
  • Schema AST
  • Reason Client

Integrations

  • apollo-link-state
  • Create-React-App

Custom Code-Generator Plugins

  • What are Plugins?
  • Write your first Plugin
  • Validate Plugin Configuration
  • How to extend the GraphQL Schema?
  • Using Handlebars

Migration

  • Migration from 0.13

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: `
        type MyType { field: String }

        directive @myDirective on OBJECT
    `
};

It's useful when you wish to add things like declerative @directive to your GraphQLSchema, that effects 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) => {
    const typesMap = schema.getTypeMap();

    return Object.keys(typesMap)
      .filter(typeName => {
        const type = typesMap[typeName];
        const astNode = type.astNode;

        if (astNode && astNode.directives && astNode.directives.find(d => d.name.value === 'ignore')) {
          return false;
        }

        return true;
      })
      .join('\n');
  },
  addToSchema: `
        directive @ignore on OBJECT
    `
};
← Validate Plugin ConfigurationUsing Handlebars →
GraphQL Code Generator
githubmediumtwitter
DocsHelp
© Copyrights by The Guild, all rights reserved
© The Guild 2018
GraphQL Code Generator is licensed under MIT and can be used free of charge with no restrictions