diff --git a/packages/gatsby/src/bootstrap/__tests__/__snapshots__/graphql-runner.js.snap b/packages/gatsby/src/bootstrap/__tests__/__snapshots__/create-graphql-runner.js.snap similarity index 100% rename from packages/gatsby/src/bootstrap/__tests__/__snapshots__/graphql-runner.js.snap rename to packages/gatsby/src/bootstrap/__tests__/__snapshots__/create-graphql-runner.js.snap diff --git a/packages/gatsby/src/bootstrap/__tests__/graphql-runner.js b/packages/gatsby/src/bootstrap/__tests__/create-graphql-runner.js similarity index 88% rename from packages/gatsby/src/bootstrap/__tests__/graphql-runner.js rename to packages/gatsby/src/bootstrap/__tests__/create-graphql-runner.js index e9dba17611223..3445cc43dec90 100644 --- a/packages/gatsby/src/bootstrap/__tests__/graphql-runner.js +++ b/packages/gatsby/src/bootstrap/__tests__/create-graphql-runner.js @@ -1,6 +1,6 @@ jest.mock(`graphql`) -const createGraphqlRunner = require(`../graphql-runner`) +import { createGraphQLRunner } from "../create-graphql-runner" const { execute, validate, parse } = require(`graphql`) parse.mockImplementation(() => { @@ -31,7 +31,7 @@ describe(`grapqhl-runner`, () => { }) it(`should return the result when grapqhl has no errors`, async () => { - const graphqlRunner = createGraphqlRunner(createStore(), reporter) + const graphqlRunner = createGraphQLRunner(createStore(), reporter) const expectation = { data: { @@ -46,7 +46,7 @@ describe(`grapqhl-runner`, () => { }) it(`should return an errors array when structured errors found`, async () => { - const graphqlRunner = createGraphqlRunner(createStore(), reporter) + const graphqlRunner = createGraphQLRunner(createStore(), reporter) const expectation = { errors: [ @@ -64,7 +64,7 @@ describe(`grapqhl-runner`, () => { }) it(`should throw a structured error when created from createPage file`, async () => { - const graphqlRunner = createGraphqlRunner(createStore(), reporter) + const graphqlRunner = createGraphQLRunner(createStore(), reporter) const errorObject = { stack: `Error diff --git a/packages/gatsby/src/bootstrap/graphql-runner.js b/packages/gatsby/src/bootstrap/create-graphql-runner.ts similarity index 54% rename from packages/gatsby/src/bootstrap/graphql-runner.js rename to packages/gatsby/src/bootstrap/create-graphql-runner.ts index 3d6fb71b2b94d..35967ec130eb1 100644 --- a/packages/gatsby/src/bootstrap/graphql-runner.js +++ b/packages/gatsby/src/bootstrap/create-graphql-runner.ts @@ -1,14 +1,27 @@ -const stackTrace = require(`stack-trace`) +import stackTrace from "stack-trace" +import { ExecutionResultDataDefault } from "graphql/execution/execute" +import { Store } from "redux" -const GraphQLRunner = require(`../query/graphql-runner`).default -const errorParser = require(`../query/error-parser`).default +import GraphQLRunner from "../query/graphql-runner" +import errorParser from "../query/error-parser" +import { emitter } from "../redux" +import { Reporter } from "../.." +import { ExecutionResult, Source } from "../../graphql" +import { IGatsbyState } from "../redux/types" -const { emitter } = require(`../redux`) +type Runner = ( + query: string | Source, + context: Record +) => Promise> -module.exports = (store, reporter) => { +export const createGraphQLRunner = ( + store: Store, + reporter: Reporter +): Runner => { // TODO: Move tracking of changed state inside GraphQLRunner itself. https://github.com/gatsbyjs/gatsby/issues/20941 let runner = new GraphQLRunner(store) - ;[ + + const eventTypes: string[] = [ `DELETE_CACHE`, `CREATE_NODE`, `DELETE_NODE`, @@ -17,12 +30,15 @@ module.exports = (store, reporter) => { `SET_SCHEMA`, `ADD_FIELD_TO_NODE`, `ADD_CHILD_NODE_TO_PARENT_NODE`, - ].forEach(eventType => { - emitter.on(eventType, event => { + ] + + eventTypes.forEach(type => { + emitter.on(type, () => { runner = new GraphQLRunner(store) }) }) - return (query, context) => + + return (query, context): ReturnType => runner.query(query, context).then(result => { if (result.errors) { const structuredErrors = result.errors @@ -30,15 +46,18 @@ module.exports = (store, reporter) => { // Find the file where graphql was called. const file = stackTrace .parse(e) - .find(file => /createPages/.test(file.functionName)) + .find(file => /createPages/.test(file.getFunctionName())) if (file) { const structuredError = errorParser({ message: e.message, location: { - start: { line: file.lineNumber, column: file.columnNumber }, + start: { + line: file.getLineNumber(), + column: file.getColumnNumber(), + }, }, - filePath: file.fileName, + filePath: file.getFileName(), }) structuredError.context = { ...structuredError.context, diff --git a/packages/gatsby/src/bootstrap/index.js b/packages/gatsby/src/bootstrap/index.js index 8bee7678798a5..ffc8fc8eb413e 100644 --- a/packages/gatsby/src/bootstrap/index.js +++ b/packages/gatsby/src/bootstrap/index.js @@ -28,7 +28,7 @@ process.on(`unhandledRejection`, (reason, p) => { report.panic(reason) }) -const createGraphqlRunner = require(`./graphql-runner`) +import { createGraphQLRunner } from "./create-graphql-runner" const { extractQueries } = require(`../query/query-watcher`) const requiresWriter = require(`./requires-writer`) const { writeRedirects } = require(`./redirects-writer`) @@ -469,7 +469,7 @@ module.exports = async (args: BootstrapArgs) => { payload: _.flattenDeep([extensions, apiResults]), }) - const graphqlRunner = createGraphqlRunner(store, report) + const graphqlRunner = createGraphQLRunner(store, report) // Collect pages. activity = report.activityTimer(`createPages`, { diff --git a/packages/gatsby/src/types.ts b/packages/gatsby/src/types.ts index 1835e473fc1dc..66727d89500b5 100644 --- a/packages/gatsby/src/types.ts +++ b/packages/gatsby/src/types.ts @@ -2,7 +2,7 @@ export interface IMatch { id: string context: { sourceMessage: string - [key: string]: string + [key: string]: string | boolean } error?: Error | undefined [key: string]: unknown