-
Notifications
You must be signed in to change notification settings - Fork 1
/
mesh.ts
233 lines (221 loc) · 6.21 KB
/
mesh.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { getMesh, GetMeshOptions } from '@graphql-mesh/runtime';
import { GraphQLSchema } from 'graphql';
import GraphQLHandler from '@graphql-mesh/graphql';
import MySQLHandler from '@graphql-mesh/mysql';
import { MeshPubSub } from '@graphql-mesh/types';
import { PubSub } from 'graphql-subscriptions';
import LRUCache from '@graphql-mesh/cache-inmemory-lru';
import StitchingMerger from '@graphql-mesh/merger-stitching';
import FilterTransform from '@graphql-mesh/transform-filter-schema';
import CacheTransform from '@graphql-mesh/transform-cache';
import { additionalResolvers } from '@src/mesh/additionalResolvers/additionalResolvers';
import { additionalTypeDefs } from '@src/mesh/additionalTypeDefs';
import ResolversCompositionTransform from '@graphql-mesh/transform-resolvers-composition';
import { Endpoints } from '@src/config/Endpoints';
import { customFetch } from '@src/mesh/customFetch/Fetch';
import { buildMutationResolverComposers } from '@src/rights/rights';
import { mutationConfig } from '@src/rights/config';
import PrefixTransform from '@graphql-mesh/transform-prefix';
import { UnwrapPromise } from '@internalTypes/promise';
import mysqlConnection from '@db/connections/mysql';
import { addEnvironmentPrefix } from '@util/prefix';
import { DefaultLogger } from '@graphql-mesh/utils';
import {
InMemoryStoreStorageAdapter,
MeshStore,
} from '@graphql-mesh/store';
/**
* These mutations are going to be used also in relation to our
* `logDB` mutation.
*/
const allowedEnvRelatedMutations: readonly string[] = addEnvironmentPrefix(
['updateTodoContent']
);
const allowedMutations: readonly string[] = [
...allowedEnvRelatedMutations,
'logDB',
];
const allowedEnvRelatedQueries: readonly string[] = addEnvironmentPrefix(
['todo', 'todoList']
);
const allowedQueries: readonly string[] = [
...allowedEnvRelatedQueries,
'getChangelog',
'getChangelogDev',
'userRights',
];
export const buildMeshConfigOptions = (): GetMeshOptions => {
const cache = new LRUCache();
/**
* We do not use the GraphQL Subscriptions but it
* is required from the types to create an instance of pubsub
*/
const pubsub = new PubSub() as MeshPubSub;
const logger = new DefaultLogger('TEST');
/**
* TODO: I am not 100% sure what the store does?
* I think the store replaces the Introspection Cache which was used to fetch schemas.
* Now the schemas are contained in a store. But probably a better way of using it is to use the GQL Build Artifacts.
* https://github.com/Urigo/graphql-mesh/blob/master/website/docs/recipes/build-mesh-artifacts.md#why-you-should-build-mesh-artifacts
*/
const store = new MeshStore(
'.mesh',
new InMemoryStoreStorageAdapter(),
{
readonly: false,
validate: false,
}
);
// TODO: I believe these properties do not do anything but they satisfy the types
// of the corresponding transforms / input source modules
const apiName = '';
const baseDir: string = (undefined as unknown) as any;
/* eslint-disable @typescript-eslint/no-empty-function */
const syncImportFn = (() => {}) as any;
const importFn = undefined as any;
return {
pubsub,
cache,
merger: new StitchingMerger({
cache,
logger,
pubsub,
store,
}),
sources: [
{
name: 'GraphqlService_dev',
handler: new GraphQLHandler({
store,
importFn,
logger,
baseDir,
pubsub,
cache,
name: 'GraphqlService_dev',
config: {
endpoint: Endpoints.FAKE_GRAPHQL_DEV,
customFetch,
},
}),
transforms: [
new PrefixTransform({
apiName,
syncImportFn,
baseDir,
pubsub,
cache,
config: {
value: 'dev_',
includeRootOperations: true,
},
}),
],
},
{
name: 'GraphqlService_live',
handler: new GraphQLHandler({
store,
importFn,
logger,
baseDir,
pubsub,
cache,
name: 'GraphqlService_live',
config: {
endpoint: Endpoints.FAKE_GRAPHQL_LIVE,
customFetch,
},
}),
transforms: [
new PrefixTransform({
apiName,
syncImportFn,
baseDir,
pubsub,
cache,
config: {
value: 'live_',
includeRootOperations: true,
},
}),
],
},
{
name: 'InternalDB',
handler: new MySQLHandler({
store,
importFn,
logger,
baseDir,
cache,
pubsub,
name: 'InternalDB',
config: {
pool: mysqlConnection,
},
}),
},
],
additionalResolvers,
additionalTypeDefs: [additionalTypeDefs],
transforms: [
new CacheTransform({
apiName,
baseDir,
syncImportFn,
cache,
pubsub,
config: [
{
field: 'Query.user',
cacheKey: 'user',
invalidate: {
ttl: 1 * 60, // invalidate every 1 minute
},
},
],
}),
FilterTransform({
apiName,
baseDir,
syncImportFn,
cache,
pubsub,
config: [
`Mutation.{${allowedMutations.join(', ')}}`,
`Query.{${allowedQueries.join(', ')}}`,
],
}),
new ResolversCompositionTransform({
apiName,
baseDir,
syncImportFn,
cache,
pubsub,
config: [
// Resolvers which are placed below have higher precedence.
...buildMutationResolverComposers(mutationConfig),
],
}),
],
};
};
export const getMeshConfig = async (): Promise<{
schema: GraphQLSchema;
contextBuilder: UnwrapPromise<
ReturnType<typeof getMesh>
>['contextBuilder'];
cache: UnwrapPromise<ReturnType<typeof getMesh>>['cache'];
allowedMutations: readonly string[];
}> => {
const { schema, contextBuilder, cache } = await getMesh(
buildMeshConfigOptions()
);
return {
schema,
contextBuilder,
cache,
allowedMutations,
};
};