Skip to content

Commit ac10d3d

Browse files
authored
feat(gatsby): Materialization (#16091)
Add a node backend independent materialization layer that triggers when nodes are searched on fields only available through resolvers. In addition, this fixes various incompatibilities that loki had due to never being tested through materialization. In addition, node tracking is now happening on node-model level too. In addition, this adds extensions to decide which fields are sortable and searchable and which needs resolve, so we can materialize.
1 parent 59eedc9 commit ac10d3d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3136
-2983
lines changed

packages/gatsby/src/bootstrap/__tests__/graphql-runner.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const createStore = (schema = {}) => {
88
getState: () => {
99
return {
1010
schema,
11-
schemaCustomization: {},
11+
schemaCustomization: {
12+
composer: {},
13+
},
1214
}
1315
},
1416
}
Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,45 @@
1-
const { graphql } = require(`graphql`)
21
const stackTrace = require(`stack-trace`)
3-
const withResolverContext = require(`../schema/context`)
4-
const errorParser = require(`../query/error-parser`).default
52

6-
const createGraphqlRunner = (store, reporter) => (query, context = {}) => {
7-
const { schema, schemaCustomization } = store.getState()
3+
const GraphQLRunner = require(`../query/graphql-runner`)
4+
const errorParser = require(`../query/error-parser`).default
85

9-
return graphql(
10-
schema,
11-
query,
12-
context,
13-
withResolverContext(context, schema, schemaCustomization.context),
14-
context
15-
).then(result => {
16-
if (result.errors) {
17-
const structuredErrors = result.errors
18-
.map(e => {
19-
// Find the file where graphql was called.
20-
const file = stackTrace
21-
.parse(e)
22-
.find(file => /createPages/.test(file.functionName))
6+
module.exports = (store, reporter) => {
7+
const runner = new GraphQLRunner(store)
8+
return (query, context) =>
9+
runner.query(query, context).then(result => {
10+
if (result.errors) {
11+
const structuredErrors = result.errors
12+
.map(e => {
13+
// Find the file where graphql was called.
14+
const file = stackTrace
15+
.parse(e)
16+
.find(file => /createPages/.test(file.functionName))
2317

24-
if (file) {
25-
const structuredError = errorParser({
26-
message: e.message,
27-
location: {
28-
start: { line: file.lineNumber, column: file.columnNumber },
29-
},
30-
filePath: file.fileName,
31-
})
32-
structuredError.context = {
33-
...structuredError.context,
34-
fromGraphQLFunction: true,
18+
if (file) {
19+
const structuredError = errorParser({
20+
message: e.message,
21+
location: {
22+
start: { line: file.lineNumber, column: file.columnNumber },
23+
},
24+
filePath: file.fileName,
25+
})
26+
structuredError.context = {
27+
...structuredError.context,
28+
fromGraphQLFunction: true,
29+
}
30+
return structuredError
3531
}
36-
return structuredError
37-
}
3832

39-
return null
40-
})
41-
.filter(Boolean)
33+
return null
34+
})
35+
.filter(Boolean)
4236

43-
if (structuredErrors.length) {
44-
// panic on build exits the process
45-
reporter.panicOnBuild(structuredErrors)
37+
if (structuredErrors.length) {
38+
// panic on build exits the process
39+
reporter.panicOnBuild(structuredErrors)
40+
}
4641
}
47-
}
4842

49-
return result
50-
})
43+
return result
44+
})
5145
}
52-
53-
module.exports = createGraphqlRunner

packages/gatsby/src/bootstrap/index.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const report = require(`gatsby-cli/lib/reporter`)
2020
const getConfigFile = require(`./get-config-file`)
2121
const tracer = require(`opentracing`).globalTracer()
2222
const preferDefault = require(`./prefer-default`)
23-
const nodeTracking = require(`../db/node-tracking`)
2423
// Add `util.promisify` polyfill for old node versions
2524
require(`util.promisify/shim`)()
2625

@@ -247,11 +246,6 @@ module.exports = async (args: BootstrapArgs) => {
247246
activity.end()
248247
}
249248

250-
// By now, our nodes database has been loaded, so ensure that we
251-
// have tracked all inline objects
252-
nodeTracking.trackDbNodes()
253-
254-
// Copy our site files to the root of the site.
255249
activity = report.activityTimer(`copy gatsby files`, {
256250
parentSpan: bootstrapSpan,
257251
})

packages/gatsby/src/commands/develop.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const db = require(`../db`)
3939
const detectPortInUseAndPrompt = require(`../utils/detect-port-in-use-and-prompt`)
4040
const onExit = require(`signal-exit`)
4141
const queryUtil = require(`../query`)
42-
const queryQueue = require(`../query/queue`)
4342
const queryWatcher = require(`../query/query-watcher`)
4443
const requiresWriter = require(`../bootstrap/requires-writer`)
4544

@@ -147,10 +146,16 @@ async function startServer(program, { activity }) {
147146
graphqlEndpoint,
148147
graphqlHTTP(() => {
149148
const { schema, schemaCustomization } = store.getState()
149+
150150
return {
151151
schema,
152152
graphiql: false,
153-
context: withResolverContext({}, schema, schemaCustomization.context),
153+
context: withResolverContext({
154+
schema,
155+
schemaComposer: schemaCustomization.composer,
156+
context: {},
157+
customContext: schemaCustomization.context,
158+
}),
154159
customFormatErrorFn(err) {
155160
return {
156161
...formatError(err),
@@ -368,7 +373,7 @@ module.exports = async (program: any) => {
368373
await waitJobsFinished()
369374
requiresWriter.startListener()
370375
db.startAutosave()
371-
queryUtil.startListening(queryQueue.createDevelopQueue())
376+
queryUtil.startListeningToDevelopQueue()
372377
queryWatcher.startWatchDeletePage()
373378

374379
activity = report.activityTimer(`start webpack server`)

packages/gatsby/src/db/__tests__/__snapshots__/node-tracking-test.js.snap renamed to packages/gatsby/src/db/__tests__/__snapshots__/sanitize-node.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`track root nodes Node sanitization Remove not supported fields / values 1`] = `
3+
exports[`node sanitization Remove not supported fields / values 1`] = `
44
Object {
55
"children": Array [],
66
"id": "id1",

packages/gatsby/src/db/__tests__/node-tracking-test.js

Lines changed: 0 additions & 209 deletions
This file was deleted.

0 commit comments

Comments
 (0)