|
| 1 | +import { Callout } from '@theguild/components' |
| 2 | + |
| 3 | +# Document Transform |
| 4 | + |
| 5 | +## Basic Usage |
| 6 | + |
| 7 | +Each plugin can also provide a function `transformDocuments` for transforming `documents`: |
| 8 | + |
| 9 | +```js {5-14} |
| 10 | +const { visit } = require('graphql') |
| 11 | + |
| 12 | +module.exports = { |
| 13 | + plugin() {}, // Nothing to do |
| 14 | + transformDocuments(schema, documents, config) { |
| 15 | + return documents.map(documentFile => { |
| 16 | + documentFile.document = visit(documentFile.document, { |
| 17 | + Field(node) { |
| 18 | + // This function triggered per each field |
| 19 | + } |
| 20 | + }) |
| 21 | + return documentFile |
| 22 | + }) |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +You can specify the plugin with `documentTransforms` field in your config: |
| 28 | + |
| 29 | +```ts {9} |
| 30 | +import { CodegenConfig } from '@graphql-codegen/cli' |
| 31 | + |
| 32 | +const config: CodegenConfig = { |
| 33 | + schema: 'http://localhost:4000/graphql', |
| 34 | + documents: ['src/**/*.tsx'], |
| 35 | + generates: { |
| 36 | + './src/gql/': { |
| 37 | + preset: 'client', |
| 38 | + documentTransforms: ['./my-transform.js'], |
| 39 | + plugins: [] |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export default config |
| 45 | +``` |
| 46 | + |
| 47 | +<Callout> |
| 48 | + If you specify it with only the `plugins` field and not with the `documentTransforms` field, the `transformDocuments` |
| 49 | + function will not be executed. |
| 50 | +</Callout> |
| 51 | + |
| 52 | +## Example |
| 53 | + |
| 54 | +For example, let's remove a `@loacalOnlyDirective` directive from documents: |
| 55 | + |
| 56 | +```js |
| 57 | +const { visit, print } = require('graphql') |
| 58 | + |
| 59 | +module.exports = { |
| 60 | + plugin(schema, documents, config) { |
| 61 | + // Output `documents` as an example. |
| 62 | + return documents.map(documentFile => `${print(documentFile.document)}`).join('\n') |
| 63 | + }, |
| 64 | + transformDocuments(schema, documents, config) { |
| 65 | + return documents.map(documentFile => { |
| 66 | + documentFile.document = visit(documentFile.document, { |
| 67 | + Directive: { |
| 68 | + leave(node) { |
| 69 | + if (node.name.value === 'localOnlyDirective') return null |
| 70 | + } |
| 71 | + } |
| 72 | + }) |
| 73 | + return documentFile |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +If you want to execute the `plugin` function as above, you specify it with the `plugins` field as well as the `documentTransforms` field in your config: |
| 80 | + |
| 81 | +```ts {9-10} |
| 82 | +import { CodegenConfig } from '@graphql-codegen/cli' |
| 83 | + |
| 84 | +const config: CodegenConfig = { |
| 85 | + schema: 'http://localhost:4000/graphql', |
| 86 | + documents: ['src/**/*.tsx'], |
| 87 | + generates: { |
| 88 | + './src/gql/': { |
| 89 | + preset: 'client', |
| 90 | + documentTransforms: ['./my-transform.js'], |
| 91 | + plugins: ['./my-transform.js'] |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +export default config |
| 97 | +``` |
| 98 | + |
| 99 | +## Validation |
| 100 | + |
| 101 | +Plugin can also validate before executing the `transformDocuments`: |
| 102 | + |
| 103 | +```js {20-24} |
| 104 | +const { visit } = require('graphql') |
| 105 | + |
| 106 | +module.exports = { |
| 107 | + plugin() {}, |
| 108 | + transformDocuments(schema, documents, config) { |
| 109 | + return documents |
| 110 | + }, |
| 111 | + validateBeforeTransformDocuments(schema, documents, config) { |
| 112 | + if (config.somethingWrong) { |
| 113 | + throw new Error(`Something wrong!`) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
0 commit comments