-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (110 loc) · 3.37 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
'use strict'
const fp = require('fastify-plugin')
const { introspectSchema } = require('@graphql-tools/wrap')
const { stitchSchemas } = require('@graphql-tools/stitch')
const { buildFederationSchema } = require('mercurius')
const { printSDL } = require('@autotelic/graphql-schema-tools')
async function mercuriusRemoteSchema (fastify, options) {
const {
schema: baseServiceSchema,
replaceSchema,
defineResolvers
} = fastify.graphql
const {
pollingInterval = null,
stitchSchemaOpts = {},
localSubschemaOpts = {},
subschemas = [],
federationMetadata
} = options
const remoteSubschemas = subschemas
async function createSubschema (subschemaConfig) {
// TODO(jkirkpatrick24): Enforce existence of an executor.
const { executor, ...restConfig } = subschemaConfig
return {
schema: await introspectSchema(executor),
executor,
...restConfig
}
}
async function buildSchema () {
const subschemas = await Promise.all(remoteSubschemas.map(createSubschema))
const localSubschema = {
...localSubschemaOpts,
schema: baseServiceSchema
}
const stitchedSchemas = stitchSchemas({
...stitchSchemaOpts,
mergeDirectives: true,
subschemas: [...subschemas, localSubschema]
})
if (federationMetadata) {
const SDL = printSDL(stitchedSchemas, {
filterDirectives: [
'external',
'requires',
'provides',
'key',
'extends'
],
filterTypes: [
'_Any',
'_FieldSet',
'_Service'
],
filterFields: {
Query: ['_service', '_entities']
}
})
const federatedSchema = buildFederationSchema(SDL)
const { _entities, _service } = federatedSchema.getType('Query').getFields()
const federatedResolvers = {}
/* istanbul ignore next */
if (_service && _service.resolve) { federatedResolvers._service = _service.resolve }
if (_entities && _entities.resolve) { federatedResolvers._entities = _entities.resolve }
replaceSchema(stitchedSchemas)
defineResolvers({ Query: federatedResolvers })
} else {
replaceSchema(stitchedSchemas)
}
}
async function addRemoteSchemas (subschemaConfigs) {
subschemaConfigs.forEach(config => remoteSubschemas.push(config))
await buildSchema()
}
async function refreshRemoteSchemas () {
await buildSchema()
}
if (remoteSubschemas.length > 0) {
await buildSchema()
}
let intervalId
function autoRefreshRemoteSchemas (interval = pollingInterval) {
stopAutoRefreshRemoteSchemas()
intervalId = setTimeout(async () => {
await buildSchema()
intervalId = null
autoRefreshRemoteSchemas(interval)
}, interval)
}
function stopAutoRefreshRemoteSchemas () {
if (intervalId !== null) {
clearInterval(intervalId)
intervalId = null
}
}
if (pollingInterval !== null) {
autoRefreshRemoteSchemas()
}
fastify.graphql.addRemoteSchemas = addRemoteSchemas
fastify.graphql.refreshRemoteSchemas = refreshRemoteSchemas
fastify.graphql.autoRefreshRemoteSchemas = autoRefreshRemoteSchemas
fastify.graphql.stopAutoRefreshRemoteSchemas = stopAutoRefreshRemoteSchemas
}
module.exports = fp(mercuriusRemoteSchema, {
name: 'mercurius-remote-schema',
decorators: {
fastify: ['graphql']
},
dependencies: ['mercurius']
})