1 | # GraphQL Code Generator
|
2 |
|
3 | [![npm version](https://badge.fury.io/js/graphql-code-generator.svg)](https://badge.fury.io/js/graphql-code-generator) [![Build Status](https://travis-ci.org/dotansimha/graphql-code-generator.svg?branch=master)](https://travis-ci.org/dotansimha/graphql-code-generator) [![codecov](https://codecov.io/gh/dotansimha/graphql-code-generator/branch/master/graph/badge.svg)](https://codecov.io/gh/dotansimha/graphql-code-generator) [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) [![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg?maxAge=2592000)](https://raw.githubusercontent.com/apollostack/apollo-ios/master/LICENSE)
|
4 |
|
5 |
|
6 | ## Overview
|
7 |
|
8 | GraphQL code generator, with flexible support for multiple languages and platforms.
|
9 |
|
10 | This generator generates both models (based on GraphQL server-side schema), and documents (client-side operations, such as Query, Mutation as Subscription).
|
11 |
|
12 | 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).
|
13 |
|
14 | **Supported languages/platforms:**
|
15 |
|
16 | | Language | Type | CLI Name |
|
17 | |-----------------|----------------|---------------------------------------------------------------------------|
|
18 | | TypeScript | Single File | ts, typescript, ts-single, typescript-single |
|
19 | | TypeScript | Multiple Files | ts-multiple, typescript-multiple |
|
20 | | Flow | Single File | flow, flow-single |
|
21 |
|
22 |
|
23 | ## Installation
|
24 |
|
25 | #### Global
|
26 |
|
27 | To install the package using NPM, run:
|
28 |
|
29 | $ npm install -g graphql-code-generator
|
30 |
|
31 | Or, using Yarn:
|
32 |
|
33 | $ yarn global add graphql-code-generator
|
34 |
|
35 | #### Dev-dependency
|
36 |
|
37 | You can also add it as dev-dependency to your application:
|
38 |
|
39 | $ npm install --save-dev graphql-code-generator
|
40 |
|
41 | Or, using Yarn:
|
42 |
|
43 | $ yarn add --dev graphql-code-generator
|
44 |
|
45 | ## Usage
|
46 |
|
47 | This package offers both modules exports (to use with NodeJS/JavaScript application), or CLI util.
|
48 |
|
49 | ### CLI
|
50 |
|
51 | CLI usage is as follow:
|
52 |
|
53 | $ gql-gen [options] [documents ...]
|
54 |
|
55 | Allowed flags:
|
56 |
|
57 | | Flag Name | Type | Description |
|
58 | |-----------------|----------|----------------------------------------------------------------------------------------|
|
59 | | -f,--file | String | Introspection JSON file, must provide file or URL flag |
|
60 | | -u,--url | String | GraphQL server endpoint to fetch the introspection from, must provide URL or file flag |
|
61 | | -t,--template | String | Template name, for example: "typescript" |
|
62 | | -o,--out | String | Path for output file/directory. When using single-file generator specify filename, and when using multiple-files generator specify a directory |
|
63 | | -d,--dev | void | Turns ON development mode - prints output to console instead of files |
|
64 | | 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 |
|
65 |
|
66 | Usage examples:
|
67 |
|
68 | - With local introspection JSON file, generated TypeScript types:
|
69 |
|
70 | $ gql-gen --file mySchema.json --template typescript --out ./typings/ ./src/**/*.graphql
|
71 |
|
72 |
|
73 | - With remote GraphQL endpoint, generated TypeScript types:
|
74 |
|
75 | $ gql-gen --url http://localhost:3010/graphql --template typescript --out ./typings/ ./src/**/*.graphql
|
76 |
|
77 |
|
78 | - Example using pre-defined files inside this repo (using Apollo's [GitHunt-API](https://github.com/apollostack/Githunt-API) and [GitHunt-Angular2](https://github.com/apollostack/Githunt-angular2)):
|
79 |
|
80 | $ gql-gen --file ./dev-test/githunt/schema.json --template typescript --out ./dev-test/githunt/typings.d.ts ./dev-test/githunt/**/*.graphql
|
81 |
|
82 | ## Integrate into a project
|
83 |
|
84 | To use inside an existing project, I recommend to add a pre-build script that executes the code generator.
|
85 |
|
86 | #### JavaScript / NodeJS
|
87 |
|
88 | When using NodeJS/JavaScript application, use NPM script to generate your types, using the command line flags that suits you best
|
89 |
|
90 | `package.json`:
|
91 |
|
92 | // ...
|
93 | "scripts": {
|
94 | "prebuild": "gql-gen --file SCHEMA_FILE --template LANGUAGE_TEMPLATE --out OUT_PATH ./src/**/*.graphql"
|
95 | "build": "YOUR_BUILD_SCRIPT_HERE"
|
96 | },
|
97 | // ...
|
98 |
|
99 | #### Other Environments
|
100 |
|
101 | 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.
|
102 |
|
103 | ## Contributing
|
104 |
|
105 | Feel free to open issues (for bugs) and create pull requests (add generators / fix bugs).
|
106 |
|
107 | ## License
|
108 |
|
109 | MIT
|