|
1 | 1 | # graphql-codegen-typescript-validation-schema
|
2 | 2 |
|
3 |
| -GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema |
| 3 | +[](https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/actions/workflows/ci.yml) [](https://badge.fury.io/js/graphql-codegen-typescript-validation-schema) |
4 | 4 |
|
5 |
| -## THIS IS ALPHA VERSION |
| 5 | +[GraphQL code generator](https://github.com/dotansimha/graphql-code-generator) plugin to generate form validation schema from your GraphQL schema. |
| 6 | + |
| 7 | +- [x] support [yup](https://github.com/jquense/yup) |
| 8 | +- [ ] support [zod](https://github.com/colinhacks/zod) |
| 9 | + |
| 10 | +## Quick Start |
| 11 | + |
| 12 | +Start by installing this plugin and write simple plugin config; |
| 13 | + |
| 14 | +```sh |
| 15 | +$ npm i graphql-codegen-typescript-validation-schema |
| 16 | +``` |
| 17 | + |
| 18 | +```yml |
| 19 | +generates: |
| 20 | + path/to/graphql.ts: |
| 21 | + plugins: |
| 22 | + - typescript |
| 23 | + - typescript-validation-schema # specify to use this plugin |
| 24 | + config: |
| 25 | + # You can put the config for typescript plugin here |
| 26 | + # see: https://www.graphql-code-generator.com/plugins/typescript |
| 27 | + strictScalars: true |
| 28 | + # You can also write the config for this plugin together |
| 29 | + schema: yup |
| 30 | +``` |
| 31 | +
|
| 32 | +## Config API Reference |
| 33 | +
|
| 34 | +### `schema` |
| 35 | + |
| 36 | +type: `ValidationSchema` default: `'yup'` |
| 37 | + |
| 38 | +Specify generete validation schema you want. |
| 39 | + |
| 40 | +```yml |
| 41 | +generates: |
| 42 | + path/to/graphql.ts: |
| 43 | + plugins: |
| 44 | + - typescript |
| 45 | + - graphql-codegen-validation-schema |
| 46 | + config: |
| 47 | + schema: yup |
| 48 | +``` |
| 49 | + |
| 50 | +### `importFrom` |
| 51 | + |
| 52 | +type: `string` |
| 53 | + |
| 54 | +import types from generated typescript type path. if not given, omit import statement. |
| 55 | + |
| 56 | +```yml |
| 57 | +generates: |
| 58 | + path/to/graphql.ts: |
| 59 | + plugins: |
| 60 | + - typescript |
| 61 | + path/to/validation.ts: |
| 62 | + plugins: |
| 63 | + - graphql-codegen-validation-schema |
| 64 | + config: |
| 65 | + importFrom: ./graphql # path for generated ts code |
| 66 | +``` |
| 67 | + |
| 68 | +Then the generator generates code with import statement like below. |
| 69 | + |
| 70 | +```ts |
| 71 | +import { GeneratedInput } from './graphql' |
| 72 | +
|
| 73 | +/* generates validation schema here */ |
| 74 | +``` |
| 75 | + |
| 76 | +### `enumsAsTypes` |
| 77 | + |
| 78 | +type: `boolean` default: `false` |
| 79 | + |
| 80 | +Generates enum as TypeScript `type` instead of `enum`. |
| 81 | + |
| 82 | +### `directives` |
| 83 | + |
| 84 | +type: `DirectiveConfig` |
| 85 | + |
| 86 | +Generates validation schema with more API based on directive schema. For example, yaml config and GraphQL schema is here. |
| 87 | + |
| 88 | +```yml |
| 89 | +generates: |
| 90 | + path/to/graphql.ts: |
| 91 | + plugins: |
| 92 | + - typescript |
| 93 | + - graphql-codegen-validation-schema |
| 94 | + config: |
| 95 | + schema: yup |
| 96 | + directives: |
| 97 | + required: |
| 98 | + msg: required |
| 99 | + constraint: |
| 100 | + minLength: min |
| 101 | + # Replace $1 with specified `startsWith` argument value of the constraint directive |
| 102 | + startsWith: ["matches", "/^$1/"] |
| 103 | + format: |
| 104 | + email: email |
| 105 | +``` |
| 106 | +
|
| 107 | +```graphql |
| 108 | +input ExampleInput { |
| 109 | + email: String! @required(msg: "Hello, World!") @constraint(minLength: 50) |
| 110 | + message: String! @constraint(startsWith: "Hello") |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +Then generates yup validation schema like below. |
| 115 | + |
| 116 | +```ts |
| 117 | +export function ExampleInputSchema(): yup.SchemaOf<ExampleInput> { |
| 118 | + return yup.object({ |
| 119 | + email: yup.string().defined().required("Hello, World!").min(50), |
| 120 | + message: yup.string().defined().matches(/^Hello/) |
| 121 | + }) |
| 122 | +} |
| 123 | +``` |
0 commit comments