-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
257 lines (219 loc) · 8.21 KB
/
index.js
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
'use strict'
const fp = require('fastify-plugin')
const { createCache } = require('async-cache-dedupe')
const { validateOpts } = require('./lib/validation')
const createReport = require('./lib/report')
module.exports = fp(async function (app, opts) {
const { all, policy, ttl, stale, skip, storage, onDedupe, onHit, onMiss, onSkip, onError, logInterval, logReport } = validateOpts(app, opts)
let cache = null
let report = null
app.graphql.cache = {
refresh () {
buildCache()
setupSchema(app.graphql.schema, policy, all, cache, skip, onDedupe, onHit, onMiss, onSkip, onError, report)
},
invalidate (references, storage) {
return cache.invalidateAll(references, storage)
},
clear () {
cache.clear()
report.clear()
}
}
app.addHook('onReady', async () => {
app.graphql.cache.refresh()
report.refresh()
})
app.addHook('onClose', () => {
report && report.close()
})
if (app.graphqlGateway) {
// Add hook to regenerate the resolvers when the schema is refreshed
app.graphqlGateway.addHook('onGatewayReplaceSchema', async (instance, schema) => {
buildCache()
setupSchema(schema, policy, all, cache, skip, onDedupe, onHit, onMiss, onSkip, onError, report)
})
}
function buildCache () {
// Default the first two parameters of onError(prefix, fieldName, err)
cache = createCache({ ttl, stale, storage, onError: onError.bind(null, 'Internal Error', 'async-cache-dedupe') })
report = createReport({ app, all, policy, logInterval, logReport })
}
}, {
fastify: '5.x',
dependencies: ['mercurius']
})
function setupSchema (schema, policy, all, cache, skip, onDedupe, onHit, onMiss, onSkip, onError, report) {
const schemaTypeMap = schema.getTypeMap()
// validate policies vs schema
const policies = !all && policy
? Object.keys(policy).reduce((o, key) => {
o.push(...Object.keys(policy[key]).map(fieldName => `${key}.${fieldName}`))
return o
}, [])
: []
for (const schemaType of Object.values(schemaTypeMap)) {
const fieldPolicy = all || policy[schemaType.name]
if (!fieldPolicy) {
continue
}
// Handle fields on schema type
if (typeof schemaType.getFields === 'function') {
for (const [fieldName, field] of Object.entries(schemaType.getFields())) {
const policy = getPolicyOptions(fieldPolicy, fieldName)
if (all || policy) {
// Override resolvers for caching purposes
if (typeof field.resolve === 'function') {
const originalFieldResolver = field.resolve
if (!all) {
policies.splice(policies.indexOf(`${schemaType.name}.${fieldName}`), 1)
}
field.resolve = makeCachedResolver(schemaType.name, fieldName, cache, originalFieldResolver, policy, skip, onDedupe, onHit, onMiss, onSkip, onError, report)
}
}
}
}
if (typeof schemaType.resolveReference === 'function') {
const resolver = '__resolveReference'
const policy = getPolicyOptions(fieldPolicy, '__resolveReference')
if (policy) {
// Override reference resolver for caching purposes
const originalResolver = schemaType.resolveReference
policies.splice(policies.indexOf(`${schemaType.name}.${resolver}`), 1)
schemaType.resolveReference = makeCachedResolver(schemaType.name, resolver, cache, originalResolver, policy, skip, onDedupe, onHit, onMiss, onSkip, onError, report)
}
}
}
if (!all && policies.length) {
throw new Error(`policies does not match schema: ${policies.join(', ')}, it must be a resolver or a loader`)
}
}
function getPolicyOptions (fieldPolicy, fieldName) {
if (!fieldPolicy[fieldName]) {
return
}
if (fieldPolicy[fieldName].__options) {
return fieldPolicy[fieldName].__options
}
return fieldPolicy[fieldName]
}
function makeCachedResolver (prefix, fieldName, cache, originalFieldResolver, policy, skip, onDedupe, onHit, onMiss, onSkip, onError, report) {
const name = prefix + '.' + fieldName
onDedupe = onDedupe.bind(null, prefix, fieldName)
onHit = onHit.bind(null, prefix, fieldName)
onMiss = onMiss.bind(null, prefix, fieldName)
onSkip = onSkip.bind(null, prefix, fieldName)
onError = onError.bind(null, prefix, fieldName)
report.wrap({ name, onDedupe, onHit, onMiss, onSkip })
let ttl, stale, storage, references, invalidate
if (policy) {
ttl = policy.ttl
stale = policy.stale
storage = policy.storage
references = policy.references
invalidate = policy.invalidate
}
cache.define(name, {
onDedupe: report[name].onDedupe,
onHit: report[name].onHit,
onMiss: report[name].onMiss,
onError,
ttl,
stale,
storage,
references,
serialize ({ self, arg, info, ctx }) {
// We need to cache only for the selected fields to support Federation
// TODO detect if we really need to do this in most cases
const fields = []
for (let i = 0; i < info.fieldNodes.length; i++) {
const node = info.fieldNodes[i]
if (!node.selectionSet) {
continue
}
for (let j = 0; j < node.selectionSet.selections.length; j++) {
if (node.selectionSet.selections[j].kind === 'InlineFragment') {
fields.push(...node.selectionSet.selections[j].selectionSet.selections.map(s => s.name.value))
} else { // kind = 'Field'
fields.push(node.selectionSet.selections[j].name.value)
}
}
}
fields.sort()
// We must skip ctx and info as they are not easy to serialize
const id = { self, arg, fields }
if (!policy) {
return id
}
// use a custom policy serializer if any
if (policy.key) {
return policy.key({ self, arg, info, ctx, fields })
} else if (policy.extendKey) {
const append = policy.extendKey(self, arg, ctx, info)
if (append) {
id.extendKey = '~' + append
}
}
return id
}
}, async function ({ self, arg, ctx, info }) {
return originalFieldResolver(self, arg, ctx, info)
})
return async function (self, arg, ctx, info) {
let result
let resolved
// dont use cache on mutation and subscriptions
[result, resolved] = await getResultForMutationSubscription({ self, arg, ctx, info, originalFieldResolver })
// dont use cache on skip by policy or by general skip
if (!resolved) {
[result, resolved] = await getResultIfSkipDefined({ self, arg, ctx, info, skip, policy, name, report, originalFieldResolver, onError })
}
// use cache to get the result
if (!resolved) {
// in case of che original resolver will throw, the error will be forwarded
// onError is already been called by cache events binding
result = await cache[name]({ self, arg, ctx, info })
}
if (invalidate) {
// Invalidates references and calls onError if fails
await invalidation(invalidate, cache, name, self, arg, ctx, info, result, onError)
}
return result
}
}
async function invalidation (invalidate, cache, name, self, arg, ctx, info, result, onError) {
try {
const references = await invalidate(self, arg, ctx, info, result)
await cache.invalidate(name, references)
} catch (err) {
onError(err)
}
}
async function getResultForMutationSubscription ({ self, arg, ctx, info, originalFieldResolver }) {
const resolved = false
let result = null
if (info.operation && (info.operation.operation === 'mutation' || info.operation.operation === 'subscription')) {
result = await originalFieldResolver(self, arg, ctx, info)
return [result, true]
}
return [result, resolved]
}
async function getResultIfSkipDefined ({ self, arg, ctx, info, skip, policy, name, report, originalFieldResolver, onError }) {
const resolved = false
let result = null
let isSkipped = false
try {
isSkipped = ((skip && (await skip(self, arg, ctx, info))) ||
(policy && policy.skip && (await policy.skip(self, arg, ctx, info))))
} catch (error) {
onError(error)
result = await originalFieldResolver(self, arg, ctx, info)
return [result, true]
}
if (isSkipped) {
report[name].onSkip()
result = await originalFieldResolver(self, arg, ctx, info)
return [result, true]
}
return [result, resolved]
}