GraphQL Code Generator

GraphQL Code Generator

  • API & Documentation
  • GitHub
  • Help

›Custom Code-Generator Plugins

Getting Started

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

Plugins

  • Available Plugins
  • TypeScript
  • TypeScript Operations
  • TypeScript 1.0 Compatibility
  • TypeScript Resolvers
  • TypeScript GraphQL Files Modules
  • TypeScript document nodes
  • TypeScript MongoDB
  • TypeScript React Apollo
  • TypeScript Apollo Angular
  • TypeScript Stencil Apollo
  • TypeScript Urql
  • TypeScript GraphQL-Request
  • Flow Types
  • Flow Resolvers
  • Flow Operations
  • Kotlin
  • Java
  • Java Resolvers
  • Java Apollo Android
  • Fragment Matcher
  • Introspection
  • Add
  • Time
  • Schema AST
  • Reason Client
  • Relay Operation Optimizer.

Presets

  • near-operation-file
  • import-types

Integrations

  • Apollo Local State
  • Create-React-App
  • GatsbyJS
  • Prettier & Linters
  • Apollo Federation

Custom Code-Generator Plugins

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

Migration

  • Migration to 1.0.0
  • Migration from 0.13 to 0.18

Validate Plugin Configuration

Each plugin can also provide a function to validate the configuration before executing it.

You can use this function to test for other plugins existence (for example, if your plugin requires another plugin to function correctly), to validate the name and path of the output file, validate the schema or documents, and much more!

To add your plugin validation method, export a function called validate from your plugin file:

module.exports = {
  plugin: (schema, documents, config, info) => {
    const typesMap = schema.getTypeMap();

    return Object.keys(typesMap).join('\n');
  },
  validate: (schema, documents, config, outputFile, allPlugins) => {}
};

outputFile is the name of the output file, and you can use it to enforce specific filename of specific file extension.

allPlugins is list of all plugins that requested in this specific output file - use it to create dependencies between plugins.

You can now check the schema, documents, configuration, output file and sibling plugins, and in case something does not fits your requirements, throw an Error:

module.exports = {
  plugin: (schema, documents, config, info) => {
    const typesMap = schema.getTypeMap();

    return Object.keys(typesMap).join('\n');
  },
  validate: (schema, documents, config, outputFile, allPlugins) => {
    if (!config.mustHave) {
      throw new Error(`You must specify "mustHave" in my plugin configuration!`);
    }
  }
};
← Write your first PluginHow to extend the GraphQL Schema? →
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