-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.ts
81 lines (79 loc) · 2.19 KB
/
codegen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* eslint-disable import/no-default-export */
import type { CodegenConfig } from '@graphql-codegen/cli';
import { DocumentNode, Kind, visit } from 'graphql';
import type { Types } from '@graphql-codegen/plugin-helpers';
import type { ClientPresetConfig } from '@graphql-codegen/client-preset';
function addTypeNameToEveryField(document: DocumentNode) {
return visit(document, {
Field: {
leave(node) {
if (node.selectionSet) {
return {
...node,
selectionSet: {
kind: Kind.SELECTION_SET,
selections: [
...node.selectionSet.selections,
{
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
value: '__typename',
},
},
],
},
};
}
return node;
},
},
});
}
const addTypename: Types.DocumentTransformFunction = async ({ documents }) =>
documents.map(({ document, location }) => ({
document: addTypeNameToEveryField(document as DocumentNode),
location,
}));
const config: CodegenConfig = {
schema: '../server/schema.graphql',
documents: ['src/**/*.{ts,tsx}'],
ignoreNoDocuments: true,
generates: {
'./src/__generated__/': {
config: {
scalars: {
Datetime: 'Date',
JSON: 'Record<string, unknown>',
},
},
preset: 'client',
presetConfig: {
gqlTagName: 'graphql',
dedupeFragments: true,
persistedDocuments: {
mode: 'embedHashInDocument',
hashAlgorithm: 'sha256',
},
skipTypename: false,
} as ClientPresetConfig,
documentTransforms: [{ transform: addTypename }],
},
'./src/__generated__/graphql-types.ts': {
plugins: ['@homebound/graphql-typescript-scalar-type-policies'],
config: {
scalars: {
Datetime: 'Date',
JSON: 'Record<string, unknown>',
},
scalarTypePolicies: {
Datetime: '../graphql-scalars.ts#datetimeTypePolicy',
},
},
},
},
hooks: {
afterAllFileWrite: ['prettier --write $1'],
},
};
export default config;