UNPKG

5.33 kBMarkdownView Raw
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) [![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
8GraphQL code generator, with flexible support for multiple languages and platforms.
9
10This generator generates both models (based on GraphQL server-side schema), and documents (client-side operations, such as Query, Mutation as Subscription).
11
12Most 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
27To install the package using NPM, run:
28
29 $ npm install -g graphql-code-generator
30
31Or, using Yarn:
32
33 $ yarn global add graphql-code-generator
34
35#### Dev-dependency
36
37You can also add it as dev-dependency to your application:
38
39 $ npm install --save-dev graphql-code-generator
40
41Or, using Yarn:
42
43 $ yarn add --dev graphql-code-generator
44
45## Usage
46
47This package offers both modules exports (to use with NodeJS/JavaScript application), or CLI util.
48
49### CLI
50
51CLI usage is as follow:
52
53 $ gql-gen [options] [documents ...]
54
55Allowed 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| -h,--header | String | Header to add to the introspection HTTP request when using --url |
62| -t,--template | String | Template name, for example: "typescript" |
63| -o,--out | String | Path for output file/directory. When using single-file generator specify filename, and when using multiple-files generator specify a directory |
64| -d,--dev | void | Turns ON development mode - prints output to console instead of files |
65| 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 |
66
67Usage examples:
68
69- With local introspection JSON file, generate TypeScript types:
70
71 $ gql-gen --file mySchema.json --template typescript --out ./typings/ ./src/**/*.graphql
72
73- With remote GraphQL endpoint, generate Flow types:
74
75 $ gql-gen --url http://localhost:3010/graphql --template flow --out ./typings/ ./src/**/*.graphql
76
77- With remote GraphQL endpoint that requires Authorization, generate TypeScript types:
78
79 $ gql-gen --url http://localhost:3010/graphql --header "Authorization: MY_KEY" --template typescript --out ./typings/ ./src/**/*.graphql
80
81- 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)):
82
83 $ gql-gen --file ./dev-test/githunt/schema.json --template typescript --out ./dev-test/githunt/typings.d.ts ./dev-test/githunt/**/*.graphql
84
85## Integrate into a project
86
87To use inside an existing project, I recommend to add a pre-build script that executes the code generator.
88
89#### JavaScript / NodeJS
90
91When using NodeJS/JavaScript application, use NPM script to generate your types, using the command line flags that suits you best
92
93`package.json`:
94
95 // ...
96 "scripts": {
97 "prebuild": "gql-gen --file SCHEMA_FILE --template LANGUAGE_TEMPLATE --out OUT_PATH ./src/**/*.graphql"
98 "build": "YOUR_BUILD_SCRIPT_HERE"
99 },
100 // ...
101
102#### Other Environments
103
104If 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.
105
106## Contributing
107
108Feel free to open issues (for bugs) and create pull requests (add generators / fix bugs).
109
110## License
111
112MIT