UNPKG

8.56 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) [![bitHound Overall Score](https://www.bithound.io/github/dotansimha/graphql-code-generator/badges/score.svg)](https://www.bithound.io/github/dotansimha/graphql-code-generator) [![codebeat badge](https://codebeat.co/badges/ec220cc6-31f0-4a80-85cc-0dfa162d8e53)](https://codebeat.co/projects/github-com-dotansimha-graphql-code-generator) [![bitHound Dependencies](https://www.bithound.io/github/dotansimha/graphql-code-generator/badges/dependencies.svg)](https://www.bithound.io/github/dotansimha/graphql-code-generator/master/dependencies/npm) [![bitHound Dev Dependencies](https://www.bithound.io/github/dotansimha/graphql-code-generator/badges/devDependencies.svg)](https://www.bithound.io/github/dotansimha/graphql-code-generator/master/dependencies/npm) [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
4
5## Overview
6
7GraphQL code generator, with flexible support for multiple languages and platforms.
8
9This generator generates both models (based on GraphQL server-side schema), and documents (client-side operations, such as Query, Mutation as Subscription).
10
11Most 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).
12
13**Supported languages/platforms:**
14
15| Language | Type | CLI Name |
16|-----------------|----------------|---------------------------------------------------------------------------|
17| TypeScript | Single File | ts, typescript, ts-single, typescript-single |
18| TypeScript | Multiple Files | ts-multiple, typescript-multiple |
19| Flow | Single File | flow, flow-single |
20| Swift (with Apollo) | Single File | swift, swift-apollo, swift-single |
21
22## Examples
23
24Refer to the generated examples inside this repository:
25
26### Star Wars
27
28Based on the GraphQL Star Wars example:
29* [Swift Example](https://github.com/dotansimha/graphql-code-generator/blob/master/dev-test/star-wars/API.swift)
30* [TypeScript Example](https://github.com/dotansimha/graphql-code-generator/blob/master/dev-test/star-wars/typings.d.ts)
31
32### GitHunt
33
34Based on the Apollo's GitHunt example:
35* [Swift Example](https://github.com/dotansimha/graphql-code-generator/blob/master/dev-test/githunt/API.swift)
36* [TypeScript Example](https://github.com/dotansimha/graphql-code-generator/blob/master/dev-test/githunt/typings.d.ts)
37
38## Installation
39
40#### Global
41
42To install the package using NPM, run:
43
44 $ npm install -g graphql-code-generator
45
46Or, using Yarn:
47
48 $ yarn global add graphql-code-generator
49
50#### Dev-dependency
51
52You can also add it as dev-dependency to your application:
53
54 $ npm install --save-dev graphql-code-generator
55
56Or, using Yarn:
57
58 $ yarn add --dev graphql-code-generator
59
60## Usage
61
62This package offers both modules exports (to use with NodeJS/JavaScript application), or CLI util.
63
64### CLI
65
66CLI usage is as follow:
67
68 $ gql-gen [options] [documents ...]
69
70Allowed flags:
71
72| Flag Name | Type | Description |
73|--------------------|----------|----------------------------------------------------------------------------------------|
74| -f,--file | String | Introspection JSON file, must provide file or URL flag |
75| -u,--url | String | GraphQL server endpoint to fetch the introspection from, must provide URL or file flag |
76| -e,--export | String | Path to a JavaScript (es5/6) file that exports (as default export) your `GraphQLSchema` object |
77| -h,--header | String | Header to add to the introspection HTTP request when using --url |
78| -t,--template | String | Template name, for example: "typescript" |
79| -o,--out | String | Path for output file/directory. When using single-file generator specify filename, and when using multiple-files generator specify a directory |
80| -m,--no-schema | void | If specified, server side schema won't be generated through the template (enums won't omit) |
81| -c,--no-documents | void | If specified, client side documents won't be generated through the template |
82| -d,--dev | void | Turns ON development mode - prints output to console instead of files |
83| documents... | [String] | Space separated paths of `.graphql` files or code files (glob path is supported) that contains GraphQL documents inside strings, or with `gql` tag (JavaScript), this field is optional - if no documents specified, only server side schema types will be generated |
84
85**Usage examples:***
86
87- With local introspection JSON file, generate TypeScript types:
88
89 $ gql-gen --file mySchema.json --template typescript --out ./typings/ ./src/**/*.graphql
90
91- With local introspection JSON file, generate TypeScript files, from GraphQL documents inside code files (`.ts`):
92
93 $ gql-gen --file mySchema.json --template typescript --out ./typings/ ./src/**/*.ts
94
95- With remote GraphQL endpoint, generate Flow types:
96
97 $ gql-gen --url http://localhost:3010/graphql --template flow --out ./typings/ ./src/**/*.graphql
98
99- With remote GraphQL endpoint that requires Authorization, generate TypeScript types:
100
101 $ gql-gen --url http://localhost:3010/graphql --header "Authorization: MY_KEY" --template typescript --out ./typings/ ./src/**/*.graphql
102
103- 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)):
104
105 $ gql-gen --file ./dev-test/githunt/schema.json --template typescript --out ./dev-test/githunt/typings.d.ts ./dev-test/githunt/**/*.graphql
106
107## Integrate into a project
108
109To use inside an existing project, I recommend to add a pre-build script that executes the code generator.
110
111#### JavaScript / NodeJS
112
113When using NodeJS/JavaScript application, use NPM script to generate your types, using the command line flags that suits you best
114
115`package.json`:
116
117 // ...
118 "scripts": {
119 "prebuild": "gql-gen --file SCHEMA_FILE --template LANGUAGE_TEMPLATE --out OUT_PATH ./src/**/*.graphql"
120 "build": "YOUR_BUILD_SCRIPT_HERE"
121 },
122 // ...
123
124#### Write a script to generate schema on compilation time
125
126You can import the library, and then write your own script to generate schema typings.
127For example:
128
129```typescript
130import { graphql, introspectionQuery } from 'graphql';
131import { FileResult, Transform, TransformedOptions, getTemplateGenerator } from 'graphql-code-generator';
132import * as fs from 'fs';
133
134import { schema } from './schema';
135// schema is GraphQLScheme Object.
136
137const OUT = "./graphql-types.d.ts";
138
139Promise.all([
140 graphql(schema, introspectionQuery).then(res => res.data),
141 getTemplateGenerator('typescript'),
142]).then(([introspection, template]) => (<TransformedOptions>{
143 introspection: introspection,
144 documents: [],
145 template: template,
146 outPath: OUT,
147 isDev: false,
148 noSchema: false,
149 noDocuments: true,
150}))
151.then(Transform)
152.then((files: FileResult[]) => {
153 files.forEach((fileResult: FileResult) => {
154 fs.writeFileSync(fileResult.path, fileResult.content);
155 });
156 return files;
157});
158```
159
160#### Other Environments
161
162If 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.
163
164## Contributing
165
166Feel free to open issues (for bugs) and create pull requests (add generators / fix bugs).
167
168## License
169
170[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg?maxAge=2592000)](https://raw.githubusercontent.com/apollostack/apollo-ios/master/LICENSE)
171
172MIT