Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate graphql-runner into TypeScript #22860

Merged
merged 11 commits into from
Apr 9, 2020
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down Expand Up @@ -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: {
Expand All @@ -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: [
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string, any>
) => Promise<ExecutionResult<ExecutionResultDataDefault>>

module.exports = (store, reporter) => {
export const createGraphQLRunner = (
store: Store<IGatsbyState>,
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`,
Expand All @@ -17,28 +30,34 @@ 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> =>
runner.query(query, context).then(result => {
if (result.errors) {
const structuredErrors = result.errors
.map(e => {
// 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,
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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`, {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down