-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgql-codegen.ts
128 lines (115 loc) · 4.57 KB
/
gql-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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { defineConfig } from '@eddeee888/gcg-typescript-resolver-files'
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
overwrite: true,
schema: 'packages/backend/src/graphql/**/schema.graphql',
documents: ['packages/frontend/src/graphql/**/*.ts'],
generates: {
//
// Backend
//
// Config Docs:
// https://github.com/eddeee888/graphql-code-generator-plugins/tree/master/packages/typescript-resolver-files#config
//
'packages/backend/src/graphql/__generated__/': defineConfig({
// We already have an existing resolver file structure, so disable the
// built-in resolver codegen
resolverGeneration: 'disabled',
tsConfigFilePath: 'packages/backend/tsconfig.json',
// Configures which files to inspect to get GraphQL type -> TypeScript
// type mappings.
mappersFileExtension: '.gql-to-typescript.ts',
mappersSuffix: 'GraphQLType',
// Disable scalar validation for now - too dangerous due to bad schema
// typing.
// FIXME (ogp-weeloong): fix schema problems and enable this!
scalarsModule: false,
scalarsOverrides: {
// Use type-fest's JSON types in generated code, as they're nice.
JSONObject: {
type: {
input: 'type-fest#JsonObject',
output: 'type-fest#JsonObject',
},
},
JSON: {
type: {
input: 'type-fest#JsonValue',
output: 'type-fest#JsonValue',
},
},
Any: {
type: {
input: 'any',
output: 'any',
},
},
},
typesPluginsConfig: {
// Add some stricter type checking. See
// https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-resolvers
// for more info.
//
// NOTE: immutableTypes is not enabled here as the libraries we use
// (e.g. objectionjs) have not marked their function params as
// readonly; enabling this will cause too much friction for not that
// much gain.
defaultScalarType: 'unknown',
strictScalars: true,
useTypeImports: true,
// In addition to normal functions, resolvers (i.e. code that implements
// a GraphQL mutation / query / field) can also be objects with a
// `resolve` function. Due to this, the generated types for resolvers
// are not callable by default.
//
// However, we only ever use resolver functions, and we directly call
// our resolvers in unit tests (e.g. `await getExecutions(...)`). This
// setting tells graphql-codegen to always type resolvers as functions so that
// we can continue calling our resolvers in unit tests.
makeResolverTypeCallable: true,
// By default, graphql-codegen allows resolvers (for queries / mutations
// / fields) to return Promises (e.g. in getExecutions(...) => Promise<T>,
// T can technically be Promise itself).
//
// This complicates our unit tests because we have to add extra code to
// deal with possibility of nested Promises. Since we never actually
// return nested Promises, to avoid the extra code, we configure
// graphql-codegen to avoid typing nested Promises.
resolverTypeWrapperSignature: 'T',
// Make resolvers' 4th `info` argument optional. This reduces
// boilerplate in our unit tests.
// See https://www.apollographql.com/docs/apollo-server/data/resolvers/#resolver-arguments
optionalInfoArgument: true,
// Set up the default context for our resolvers / queries / mutations.
contextType: '@/graphql/schema.context#AuthenticatedGraphQLContext',
// Some resolvers / queries / mutations need different contexts
fieldContextTypes: [
'Query.getCurrentUser#@/graphql/schema.context#UnauthenticatedGraphQLContext',
],
},
}),
//
// Frontend
//
// Config Docs:
// https://the-guild.dev/graphql/codegen/docs/guides/react-vue#config-api
//
'packages/frontend/src/graphql/__generated__/': {
preset: 'client',
config: {
// Add some stricter type checking.
defaultScalarType: 'unknown',
strictScalars: true,
useTypeImports: true,
immutableTypes: true,
scalars: {
// Use type-fest's JSON types in generated code, as they're nice.
JSONObject: 'type-fest#JsonObject',
JSON: 'type-fest#JsonValue',
Any: 'any',
},
},
},
},
}
export default config