JSPM

graphql-code-generator

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6804
  • Score
    100M100P100Q60798F
  • License MIT

GraphQL types and code generator based on schema

Package Exports

  • graphql-code-generator
  • graphql-code-generator/dist/cli

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (graphql-code-generator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

GraphQL Code Generator

npm version Build Status codecov Commitizen friendly GitHub license

Overview

GraphQL code generator, with flexible support for multiple languages and platforms.

This generator generates both models (based on GraphQL server-side schema), and documents (client-side operations, such as Query, Mutation as Subscription).

Most of the generators support single-file (which is a large file with all of your types/classes/interfaces/enums), and some support multiple-files (file for each model/document, with support for imports).

Supported languages/platforms:

Language Type CLI Name
TypeScript Single File ts, typescript, ts-single, typescript-single
TypeScript Multiple Files ts-multiple, typescript-multiple
Flow Single File flow, flow-single

Installation

Global

To install the package using NPM, run:

$ npm install -g graphql-code-generator

Or, using Yarn:

$ yarn global add graphql-code-generator

Dev-dependency

You can also add it as dev-dependency to your application:

$ npm install --save-dev graphql-code-generator

Or, using Yarn:

$ yarn add --dev graphql-code-generator

Usage

This package offers both modules exports (to use with NodeJS/JavaScript application), or CLI util.

CLI

CLI usage is as follow:

$ gql-gen [options] [documents ...]

Allowed flags:

Flag Name Type Description
-f,--file String Introspection JSON file, must provide file or URL flag
-u,--url String GraphQL server endpoint to fetch the introspection from, must provide URL or file flag
-t,--template String Template name, for example: "typescript"
-o,--out String Path for output file/directory. When using single-file generator specify filename, and when using multiple-files generator specify a directory
-d,--dev void Turns ON development mode - prints output to console instead of files
documents... [String] Space separated paths of .graphql files, allows glob path, this field is optional - if no documents specified, only server side schema types will be generated

Usage examples:

  • With local introspection JSON file, generated TypeScript types:

      $ gql-gen --file mySchema.json --template typescript --out ./typings/ ./src/**/*.graphql
  • With remote GraphQL endpoint, generated TypeScript types:

      $ gql-gen --url http://localhost:3010/graphql --template typescript --out ./typings/ ./src/**/*.graphql
  • Example using pre-defined files inside this repo (using Apollo's GitHunt-API and GitHunt-Angular2):

      $ gql-gen --file ./dev-test/githunt/schema.json --template typescript --out ./dev-test/githunt/typings.d.ts ./dev-test/githunt/**/*.graphql

Integrate into a project

To use inside an existing project, I recommend to add a pre-build script that executes the code generator.

JavaScript / NodeJS

When using NodeJS/JavaScript application, use NPM script to generate your types, using the command line flags that suits you best

package.json:

// ...
"scripts": {
    "prebuild": "gql-gen --file SCHEMA_FILE --template LANGUAGE_TEMPLATE --out OUT_PATH ./src/**/*.graphql"
    "build": "YOUR_BUILD_SCRIPT_HERE"
},
// ...

Other Environments

If you are using GraphQL with environment different from NodeJS and wish to generate types and interfaces for your platform, start by installing NodeJS and the package as global, and then add the generation command to your build process.

Contributing

Feel free to open issues (for bugs) and create pull requests (add generators / fix bugs).

License

MIT