开发者社区> 镜像站> NPM> graphql-codegen-generators
graphql-codegen-generators
GraphQL code generator predefined templates
Last updated 3 years ago by dotansimha .
MIT · Original npm · Tarball · package.json
$ cnpm install graphql-codegen-generators 
SYNC missed versions from official npm registry.

graphql-code-generators

This packages includes the built-in generators for GraphQL Codegen.

Currently includes:

  • TypeScript single file
  • TypeScript multiple files

Build process

The build process of this package is based on Webpack with awesome-typescript-loader to compile TypeScript source code, and raw-loader to load the handlebars templates as string.

The purpose of the build process is to eliminate the need to use fs.readFile for the template files, and integrate the template files into the compiles JS files as strings.

To build this package and the generators template, start by installing the package dependencies:

   $ npm install
   // Or, with Yarn
   $ yarn

Then, you can use the existing NPM scripts to build the package:

    $ yarn build
    // Or, with Yarn
    $ yarn build

Adding a new generator

To add a new generator, start by adding a new directory under ./src/ of this directory, with the name of the generator.

Then, create a file called config.ts.

Your config file should use default export, and export a config variable that implements the GeneratorConfig interface, for example:

import { EInputType, GeneratorConfig } from '../types';

const config: GeneratorConfig = {
  inputType: EInputType.MULTIPLE_FILES,
  templates: {
    // Your templates here
  },
  flattenTypes: true,
  primitives: {
    String: 'string',
    Int: 'number',
    Float: 'number',
    Boolean: 'boolean',
    ID: 'string'
  }
};

export default config;

Next, make sure to import your template files and add them to your templates, for example:

import * as index from './template.handlebars';
import * as type from './type.handlebars';
import * as schema from './schema.handlebars';
import * as documents from './documents.handlebars';
import * as selectionSet from './selection-set.handlebars';
import { EInputType, GeneratorConfig } from '../types';

const config: GeneratorConfig = {
  inputType: EInputType.SINGLE_FILE,
  templates: {
    index,
    type,
    schema,
    documents,
    selectionSet
  },
  flattenTypes: true,
  primitives: {
    String: 'string',
    Int: 'number',
    Float: 'number',
    Boolean: 'boolean',
    ID: 'string'
  },
  outFile: 'types.ts'
};

export default config;

inputType

Allowed values: EInputType.SINGLE_FILE, EInputType.MULTIPLE_FILES

inputType defined the template input type of the generator, and also declares the generator output.

For example, we have TypeScript generators for both single and multiple files.

The input type field effects the rest of the fields:

SINGLE_FILE

When using SINGLE_FILE, you must specify the main template name, with a key called index, and this will be the root of your app.

You also need to specify the outFile of the package, which is the default file name in case of output filename was not specified through the CLI.

MULTIPLE_FILES

When using MULTIPLE_FILES, you need to specify a template for each available compilation context (refer to templates section for the list of available contexts).

You also need to specify the filesExtension for the generated files.

templates

templates field should contains an object, where the key is the name of the template, and the value is a string.

There are special context types for templates, and each type of templates will compile with a different context:

  • index: use with SINGLE_FILE to declare the main entry point of the generated file, compiled with a merged object, containing all SchemaTemplateContext and Document fields.
  • type: use with MULTIPLE_FILES to declare that this template belongs to GraphQL schema type, this template will compile with each type in your schema.
  • inputType: use with MULTIPLE_FILES to declare that this template belongs to GraphQL schema input, this template will compile with each input type in your schema.
  • union: use with MULTIPLE_FILES to declare that this template belongs to GraphQL schema union, this template will compile with each union in your schema.
  • scalar: use with MULTIPLE_FILES to declare that this template belongs to GraphQL schema scalar, this template will compile with each scalar in your schema.
  • enum: use with MULTIPLE_FILES to declare that this template belongs to GraphQL schema enum, this template will compile with each enum in your schema.
  • interface: use with MULTIPLE_FILES to declare that this template belongs to GraphQL schema interface, this template will compile with each interface in your schema.
  • operation: use with MULTIPLE_FILES to declare that this template belongs to GraphQL operation (query/mutation/subsription), this template will compile with Operation context.
  • fragment: use with MULTIPLE_FILES to declare that this template belongs to GraphQL fragment, this template will compile with Fragment context.
  • schema: use with MULTIPLE_FILES to compile with SchemaTemplateContext.
  • documents: use with MULTIPLE_FILES to compile with all operations, the context will be Document.
  • all: same as to index.

Also, all templates specified under templates will be loaded as Handlebars template partials, so you can use it any time inside other templates, for example, the following templates definitions:

const config = {
  // ...
  templates: {
    index: '{{>selectionSet}}',
    selectionSet: 'Hello'
  }
  // ...
};

The index template loads selectionSet template, and it can also provide a context for the specific partial:

const config = {
  // ...
  templates: {
    index: '{{>selectionSet types}}',
    selectionSet: '{{#each this }} Type name: {{ name }}{{/each}}'
  }
  // ...
};

You can also load a template from itself, and create a recursive generation of the template.

flattenTypes

Type flattening is a useful feature when generation a template, when true is specified, the generator will return a flatten version of the GraphQL selection set when using inner types.

For example, let's take a look in the following GraphQL schema and query:

type NameFields {
    firstName: String
    lastName: String
}

type User {
    name: NameFields
    email: String
    age: Int
}

type Query {
    me: User
}

schema {
    query: Query
}
query myQuery {
  user {
    me {
      firstName
      lastName
    }
  }
}

This query uses multiple levels of selection set (user > name > firstName), but when adding flattenTypes: true, the generator will append a new field to the operation/fragment context, called innerModels, and it this case it will contains the following:

[
    {
        schemaBaseType: 'User',
        modelType: 'Me',
        fields: [ ... ] // Original SelectionSetFieldNode from the operation
        // .. more fields
    },
    {
        schemaBaseType: 'NameFields',
        modelType: 'Name',
        fields: [ ... ] // Original SelectionSetFieldNode from the operation
        // .. more fields
    }
]

So the two available levels of selection set were flatten into a single level, so you can generate you whole selection set in a single iteration.

The modelType becomes the name of the selection set field, because we use only part of the available fields (for example, the query only asks for part of the User fields), so we can't use the actual GraphQL type from the schema - so each selection set creates new "types", and the usage in the selection set also changes, so the type of me is not User - it's Me.

The actual compilation context when using flattenTypes: true is available here.

primitives

Specify primitives object map to replace the original GraphQL built-in types to a language-specific primitives.

For example, GraphQL type of String is actually a string in TypeScript.

outFile

Use with SINGLE_FILE, specify the default filename for the generated file.

filesExtension

Use with MULTIPLE_FILES, specify the file extension for the generated files.

customHelpers

With customHelpers you can add custom helpers that executes with your custom templates.

Provide an object with key as the name of the helper, and a Function for the helper execution.

Current Tags

  • 0.9.0-alpha.86a530fd                                ...           canary (3 years ago)
  • 0.8.21                                ...           latest (3 years ago)

108 Versions

  • 0.8.21                                ...           3 years ago
  • 0.8.20                                ...           3 years ago
  • 0.8.19                                ...           3 years ago
  • 0.8.18                                ...           3 years ago
  • 0.8.17                                ...           3 years ago
  • 0.8.16                                ...           3 years ago
  • 0.8.15                                ...           3 years ago
  • 0.9.0-alpha.86a530fd                                ...           3 years ago
  • 0.9.0-alpha.dd6f89a7                                ...           3 years ago
  • 0.8.14                                ...           3 years ago
  • 0.9.0-alpha.0bf8d921                                ...           3 years ago
  • 0.9.0-alpha.5004263d                                ...           3 years ago
  • 0.8.13                                ...           3 years ago
  • 0.8.12                                ...           3 years ago
  • 0.8.11                                ...           3 years ago
  • 0.8.10                                ...           3 years ago
  • 0.8.9                                ...           3 years ago
  • 0.8.8                                ...           3 years ago
  • 0.9.0-alpha.b4c4afea                                ...           3 years ago
  • 0.9.0-alpha.6c0c5bdf                                ...           3 years ago
  • 0.9.0-alpha.5e331b10                                ...           3 years ago
  • 0.9.0-alpha.00efbe34                                ...           3 years ago
  • 0.9.0-alpha.a13bb670                                ...           3 years ago
  • 0.9.0-alpha.417b87dd                                ...           3 years ago
  • 0.9.0-alpha.574a7a7b                                ...           3 years ago
  • 0.8.7                                ...           3 years ago
  • 0.8.6                                ...           3 years ago
  • 0.9.0-alpha.118cb289                                ...           3 years ago
  • 0.9.0-alpha.41c502be                                ...           3 years ago
  • 0.9.0-alpha.80022c38                                ...           3 years ago
  • 0.9.0-alpha.ebd3e3b5                                ...           3 years ago
  • 0.9.0-alpha.70002868                                ...           3 years ago
  • 0.9.0-alpha.4ef2ace9                                ...           3 years ago
  • 0.9.0-alpha.a6c3d1d1                                ...           3 years ago
  • 0.9.0-alpha.9c759e1a                                ...           3 years ago
  • 0.9.0-alpha.f365e6db                                ...           3 years ago
  • 0.9.0-alpha.6b9f20a7                                ...           3 years ago
  • 0.9.0-alpha.dfde14c1                                ...           3 years ago
  • 0.9.0-alpha.2f7495c1                                ...           3 years ago
  • 0.8.5                                ...           3 years ago
  • 0.8.4                                ...           3 years ago
  • 0.8.3                                ...           3 years ago
  • 0.9.0-alpha.1a4d72cb                                ...           3 years ago
  • 0.9.0-alpha.465f2525                                ...           3 years ago
  • 0.9.0-alpha.5fd20a9a                                ...           3 years ago
  • 0.8.0                                ...           3 years ago
  • 0.8.0-alpha.15f89c5b                                ...           3 years ago
  • 0.8.0-alpha.bf43df88                                ...           3 years ago
  • 0.8.0-alpha.c833ee09                                ...           3 years ago
  • 0.8.0-alpha.167b50c6                                ...           3 years ago
  • 0.8.0-alpha.2c1bcf44                                ...           3 years ago
  • 0.8.0-alpha.8b63add2                                ...           3 years ago
  • 0.7.0-alpha.82bb43f2                                ...           3 years ago
  • 0.7.0-alpha.383c3d35                                ...           3 years ago
  • 0.7.0-alpha.542dd03d                                ...           3 years ago
  • 0.7.0-alpha.b5ecec0c                                ...           3 years ago
  • 0.7.0-alpha.3dcc416e                                ...           3 years ago
  • 0.7.0-alpha.073f588d                                ...           3 years ago
  • 0.6.0-alpha.4aa8b5de                                ...           3 years ago
  • 0.6.0-alpha.836ffd2e                                ...           3 years ago
  • 0.6.0-alpha.8e61e231                                ...           3 years ago
  • 0.6.0-alpha.275b77af                                ...           3 years ago
  • 0.6.0-alpha.a6e3567b                                ...           3 years ago
  • 0.6.0-alpha.3e7644c0                                ...           3 years ago
  • 0.6.0-alpha.24ca3932                                ...           3 years ago
  • 0.6.0-alpha.31d807a4                                ...           3 years ago
  • 0.6.0-alpha.602b816e                                ...           3 years ago
  • 0.6.0-alpha.e5724a2a                                ...           3 years ago
  • 0.6.0-alpha.8a21c8cc                                ...           3 years ago
  • 0.6.0-alpha.a03e97a2                                ...           3 years ago
  • 0.6.0-alpha.fe25425e                                ...           3 years ago
  • 0.6.0-alpha.0897eec2                                ...           3 years ago
  • 0.6.0-alpha.8a5e7815                                ...           3 years ago
  • 0.6.0-alpha.f5818070                                ...           3 years ago
  • 0.6.0-alpha.040ffe5d                                ...           3 years ago
  • 0.6.0-alpha.458cc367                                ...           3 years ago
  • 0.6.0-alpha.b746c9f3                                ...           3 years ago
  • 0.6.0-alpha.19fa4b91                                ...           3 years ago
  • 0.6.0-alpha.6ee86113                                ...           3 years ago
  • 0.6.0-alpha.6b10a2b2                                ...           3 years ago
  • 0.6.0-alpha.aedc961c                                ...           3 years ago
  • 0.6.0-alpha.8640f1df                                ...           3 years ago
  • 0.6.0-alpha.ba79dba1                                ...           3 years ago
  • 0.6.0-alpha.2449484b                                ...           3 years ago
  • 0.6.0-alpha.d5fe220f                                ...           3 years ago
  • 0.6.0-alpha.a6fea330                                ...           3 years ago
  • 0.6.0-alpha.cc47c396                                ...           3 years ago
  • 0.6.0-alpha.866878b2                                ...           3 years ago
  • 0.6.0-alpha.4ec61588                                ...           3 years ago
  • 0.6.0-alpha.1e35b32a                                ...           3 years ago
  • 0.6.0-alpha.624c8437                                ...           3 years ago
  • 0.6.0-alpha.2bcde2b7                                ...           3 years ago
  • 0.6.0-alpha.1bc8b137                                ...           3 years ago
  • 0.6.0-alpha.9509210                                ...           3 years ago
  • 0.6.0-alpha.f56888d8                                ...           3 years ago
  • 0.6.0-alpha.b44f7c86                                ...           3 years ago
  • 0.6.0-alpha.4e12bbc7                                ...           3 years ago
  • 0.6.0-alpha.548c19d9                                ...           3 years ago
  • 0.6.0-alpha.cc01df5b                                ...           3 years ago
  • 0.6.0-alpha.7854fe32                                ...           3 years ago
  • 0.6.0-alpha.d8fc1f37                                ...           3 years ago
  • 0.6.0-alpha.6cb4c2a8                                ...           3 years ago
  • 0.6.0-alpha.3744e90e                                ...           3 years ago
  • 0.6.0-alpha.65d94de4                                ...           3 years ago
  • 0.6.0-alpha.afd0ebbc                                ...           3 years ago
  • 0.6.0-alpha.14ecb322                                ...           3 years ago
  • 0.6.0-alpha.d381584b                                ...           3 years ago
  • 0.6.0-alpha.62b22f38                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 113
This Month 111
Last Day 108
Last Week 5
Last Month 301
Dependencies (0)
None
Dev Dependencies (0)
None