-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
index.js
174 lines (151 loc) · 4.88 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
/* @flow */
const tracer = require(`opentracing`).globalTracer()
const { store } = require(`../redux`)
const { getDataStore, getTypes } = require(`../datastore`)
const { createSchemaComposer } = require(`./schema-composer`)
const { buildSchema } = require(`./schema`)
const { builtInFieldExtensions } = require(`./extensions`)
const { builtInTypeDefinitions } = require(`./types/built-in-types`)
const { TypeConflictReporter } = require(`./infer/type-conflict-reporter`)
const { shouldPrintEngineSnapshot } = require(`../utils/engines-helpers`)
const getAllTypeDefinitions = () => {
const {
schemaCustomization: { types },
} = store.getState()
const builtInTypes = builtInTypeDefinitions().map(typeDef => {
return {
typeOrTypeDef: typeDef,
plugin: undefined,
}
})
// Ensure that user-defined types are processed last
return [
...builtInTypes,
...types.filter(
type => type.plugin && type.plugin.name !== `default-site-plugin`
),
...types.filter(
type => !type.plugin || type.plugin.name === `default-site-plugin`
),
]
}
const getAllFieldExtensions = () => {
const {
schemaCustomization: { fieldExtensions: customFieldExtensions },
} = store.getState()
return {
...customFieldExtensions,
...builtInFieldExtensions,
}
}
// Schema building requires metadata for type inference.
// Technically it means looping through all type nodes, analyzing node structure
// and then using this aggregated node structure in related GraphQL type.
// Actual logic for inference located in inferenceMetadata reducer and ./infer
// Here we just orchestrate the process via redux actions
const buildInferenceMetadata = ({ types }) =>
new Promise(resolve => {
if (!types || !types.length) {
resolve()
return
}
const typeNames = [...types]
// TODO: use async iterators when we switch to node>=10
// or better investigate if we can offload metadata building to worker/Jobs API
// and then feed the result into redux?
const processNextType = async () => {
const typeName = typeNames.pop()
let processingNodes = []
let dispatchCount = 0
function dispatchNodes() {
return new Promise(res => {
store.dispatch({
type: `BUILD_TYPE_METADATA`,
payload: {
typeName,
// only clear metadata on the first chunk for this type
clearExistingMetadata: dispatchCount++ === 0,
nodes: processingNodes,
},
})
setImmediate(() => {
// clear this array after BUILD_TYPE_METADATA reducer has synchronously run
processingNodes = []
// dont block the event loop. node may decide to free previous processingNodes array from memory if it needs to.
setImmediate(() => {
res(null)
})
})
})
}
for (const node of getDataStore().iterateNodesByType(typeName)) {
processingNodes.push(node)
if (processingNodes.length > 1000) {
await dispatchNodes()
}
}
if (processingNodes.length > 0) {
await dispatchNodes()
}
if (typeNames.length > 0) {
// dont block the event loop
setImmediate(() => processNextType())
} else {
resolve()
}
}
processNextType()
})
const build = async ({ parentSpan, fullMetadataBuild = true }) => {
const spanArgs = parentSpan ? { childOf: parentSpan } : {}
const span = tracer.startSpan(`build schema`, spanArgs)
await getDataStore().ready()
if (fullMetadataBuild) {
// Build metadata for type inference and start updating it incrementally
await buildInferenceMetadata({ types: getTypes() })
store.dispatch({ type: `START_INCREMENTAL_INFERENCE` })
}
const {
schemaCustomization: { thirdPartySchemas, printConfig },
inferenceMetadata,
config: { mapping: typeMapping },
program: { directory },
} = store.getState()
const typeConflictReporter = new TypeConflictReporter()
const enginePrintConfig = shouldPrintEngineSnapshot()
? {
path: `${directory}/.cache/schema.gql`,
rewrite: true,
}
: undefined
const fieldExtensions = getAllFieldExtensions()
const schemaComposer = createSchemaComposer({ fieldExtensions })
const schema = await buildSchema({
schemaComposer,
types: getAllTypeDefinitions(),
fieldExtensions,
thirdPartySchemas,
typeMapping,
printConfig,
enginePrintConfig,
typeConflictReporter,
inferenceMetadata,
parentSpan,
})
typeConflictReporter.printConflicts()
store.dispatch({
type: `SET_SCHEMA_COMPOSER`,
payload: schemaComposer,
})
store.dispatch({
type: `SET_SCHEMA`,
payload: schema,
})
span.finish()
}
const rebuild = async ({ parentSpan }) =>
await build({ parentSpan, fullMetadataBuild: false })
module.exports = {
build,
rebuild,
}