Skip to content

Commit

Permalink
chore(gatsby): Migrate graphql-runner into TypeScript (#22860)
Browse files Browse the repository at this point in the history
* feat: typed graphql-runner

* fix: anything else context can also be boolean (graphql-runner.ts:L55)

* refactor: format

* fix: caling default export from ts

* fix: defining default in test suite

* cleanup PR

* fix ci

* fix lint again

* fix ci again

* update snapshot

Co-authored-by: Blaine Kasten <blainekasten@gmail.com>
  • Loading branch information
rayriffy and blainekasten authored Apr 9, 2020
1 parent 246c39c commit 5037e9d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
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

0 comments on commit 5037e9d

Please sign in to comment.