UNPKG

6.78 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
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| -nm,--no-model | void | If specified, server side schema won't be generated through the template (enums won't omit) |
65| -nd,--no-documents | void | If specified, client side documents won't be generated through the template |
66| -d,--dev | void | Turns ON development mode - prints output to console instead of files |
67| 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 |
68
69**Usage examples:***
70
71- With local introspection JSON file, generate TypeScript types:
72
73 $ gql-gen --file mySchema.json --template typescript --out ./typings/ ./src/**/*.graphql
74
75- With local introspection JSON file, generate TypeScript files, from GraphQL documents inside code files (`.ts`):
76
77 $ gql-gen --file mySchema.json --template typescript --out ./typings/ ./src/**/*.ts
78
79- With remote GraphQL endpoint, generate Flow types:
80
81 $ gql-gen --url http://localhost:3010/graphql --template flow --out ./typings/ ./src/**/*.graphql
82
83- With remote GraphQL endpoint that requires Authorization, generate TypeScript types:
84
85 $ gql-gen --url http://localhost:3010/graphql --header "Authorization: MY_KEY" --template typescript --out ./typings/ ./src/**/*.graphql
86
87- 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)):
88
89 $ gql-gen --file ./dev-test/githunt/schema.json --template typescript --out ./dev-test/githunt/typings.d.ts ./dev-test/githunt/**/*.graphql
90
91## Integrate into a project
92
93To use inside an existing project, I recommend to add a pre-build script that executes the code generator.
94
95#### JavaScript / NodeJS
96
97When using NodeJS/JavaScript application, use NPM script to generate your types, using the command line flags that suits you best
98
99`package.json`:
100
101 // ...
102 "scripts": {
103 "prebuild": "gql-gen --file SCHEMA_FILE --template LANGUAGE_TEMPLATE --out OUT_PATH ./src/**/*.graphql"
104 "build": "YOUR_BUILD_SCRIPT_HERE"
105 },
106 // ...
107
108#### Other Environments
109
110If 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.
111
112## Contributing
113
114Feel free to open issues (for bugs) and create pull requests (add generators / fix bugs).
115
116## License
117
118[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg?maxAge=2592000)](https://raw.githubusercontent.com/apollostack/apollo-ios/master/LICENSE)
119
120MIT