|
1 | | -# graphql-cli-generate-fragments |
| 1 | +# graphql-cli-generate-fragments [![npm] |
| 2 | + |
| 3 | +## Installation |
| 4 | + |
| 5 | +```npm i -g graphql-cli graphql-cli-generate-fragments``` |
| 6 | + |
| 7 | +## Usage |
| 8 | +``` |
| 9 | +graphql generate-fragments |
| 10 | + |
2 | 11 | Generate Fragments for Graphql Schemas
|
| 12 | + |
| 13 | +Options: |
| 14 | + --dotenv Path to .env file [string] |
| 15 | + -p, --project Project name [string] |
| 16 | + --output, -o Output folder [string] |
| 17 | + --save, -s Save settings to config file [boolean] [default: "false"] |
| 18 | +"false"] |
| 19 | + --generator, -g Generate to 'js' or 'graphq' [string] |
| 20 | + --verbose Show verbose output messages [boolean] [default: "false"] |
| 21 | + -h, --help Show help [boolean] |
| 22 | + -v, --version Show version number [boolean] |
| 23 | +``` |
| 24 | + |
| 25 | + |
| 26 | +### Graphql Fragments Generation |
| 27 | +Creates graphql fragments containing the fields for each type in the supplied schema. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +The first time you use fragment generation in your project, you need to provide an output folder for your fragments, and the generator you want to use: |
| 32 | +```shell |
| 33 | +$ graphql generate-fragments -p database -o src/generated -g graphql --save |
| 34 | +✔ Fragments for project database written to src/generated/database.fragments.js |
| 35 | +``` |
| 36 | +This will also save the configuration in your `.graphqlconfig` file (see below). |
| 37 | + |
| 38 | +### Automating `graphql generate-fragments` |
| 39 | +After you have set up fragment generation for all projects, you can simply run `graphql generate-fragments` without any parameters to process all projects: |
| 40 | +```shell |
| 41 | +$ graphql prepare |
| 42 | +✔ Fragments for project app written to src/generated/app.fragments.graphql |
| 43 | +✔ Fragments for project database written to src/generated/database.fragments.js |
| 44 | +``` |
| 45 | +## Advanced topics |
| 46 | + |
| 47 | +### Available generators |
| 48 | +The following generators are provided: |
| 49 | + |
| 50 | +| Generator | Purpose | |
| 51 | +| ------------ | -------------------------------------------- | |
| 52 | +| graphql | Generates fragments for all types in schema | |
| 53 | +| js | Wraps the graphql and exports them for inclusion in javascript projects | |
| 54 | + |
| 55 | + |
| 56 | +### `graphql-config` extensions |
| 57 | + |
| 58 | +To store the project configuration for fragment generation, `graphql-cli-generate-fragments` uses two extension keys in the `graphql-config` configuration file. These keys can be set manually, or using the `--save` parameter. |
| 59 | +```diff |
| 60 | +# ./.graphqlconfig.yml |
| 61 | +projects: |
| 62 | + app: |
| 63 | + schemaPath: src/schema.graphql |
| 64 | + extensions: |
| 65 | + endpoints: |
| 66 | + default: 'http://localhost:4000' |
| 67 | ++ prepare-bundle: |
| 68 | ++ src/generated/app.fragments.js |
| 69 | ++ generator: js |
| 70 | + database: |
| 71 | + schemaPath: src/generated/prisma.graphql |
| 72 | + extensions: |
| 73 | + prisma: database/prisma.yml |
| 74 | ++ generate-fragments: |
| 75 | ++ output: src/generated/database.fragments.graphql |
| 76 | ++ generator: graphql |
| 77 | + |
| 78 | +``` |
| 79 | + |
| 80 | +## License |
| 81 | + |
| 82 | +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details |
| 83 | + |
| 84 | +## Acknowledgments |
| 85 | + |
| 86 | +* [lachenmayer](https://github.com/lachenmayer) for [`graphql-fragment-codegen`](https://github.com/lachenmayer/graphql-fragment-codegen) |
| 87 | +* [kbrandwijk](https://github.com/kbrandwijk)/[supergraphql](https://github.com/supergraphql) for [`graphql-cli-prepare`](https://github.com/supergraphql/graphql-cli-prepare) |
| 88 | + |
| 89 | +<hr> |
| 90 | +<p align="center"> |
| 91 | + <img src="https://img.shields.io/badge/built-with_love-blue.svg?style=for-the-badge"/><a href="https://github.com/develomark" target="-_blank"><img src="https://img.shields.io/badge/by-develomark-red.svg?style=for-the-badge"/></a> |
| 92 | +</p> |
| 93 | + |
0 commit comments