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`)
const { createGraphqlRunner } = require(`../create-graphql-runner`)
const { execute, validate, parse } = require(`graphql`)

parse.mockImplementation(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
const stackTrace = require(`stack-trace`)
import stackTrace from "stack-trace"
import { ExecutionResultDataDefault } from "graphql/execution/execute"

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, store as GatsbyStore } from "../redux"
import { Reporter } from "../.."
import { ExecutionResult, Source } from "../../graphql"

const { emitter } = require(`../redux`)

module.exports = (store, reporter) => {
export const createGraphQLRunner = (
store: typeof GatsbyStore,
reporter: Reporter
): ((
query: string | Source,
context: Record<string, any>
) => Promise<ExecutionResult<ExecutionResultDataDefault>>) => {
// 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,11 +26,14 @@ 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) =>
runner.query(query, context).then(result => {
if (result.errors) {
Expand All @@ -30,15 +42,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,
Expand Down
2 changes: 1 addition & 1 deletion 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`)
const { createGraphqlRunner } = require(`./create-graphql-runner`)
const { extractQueries } = require(`../query/query-watcher`)
const requiresWriter = require(`./requires-writer`)
const { writeRedirects } = require(`./redirects-writer`)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const configureStore = (
applyMiddleware(thunk, multi)
)

export const store: Store = configureStore(readState())
export const store: Store<IGatsbyState> = configureStore(readState())

// Persist state.
export const saveState = (): void => {
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