Skip to content

Commit

Permalink
chore(gatsby): Port gql runner to TypeScript (#21956)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Mar 4, 2020
1 parent 996ac5e commit 9fdc395
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
const { parse, validate, execute } = require(`graphql`)
const { debounce } = require(`lodash`)
import {
parse,
validate,
execute,
DocumentNode,
GraphQLSchema,
Source,
GraphQLError,
ExecutionResult,
} from "graphql"
import { debounce } from "lodash"
import nodeStore from "../db/nodes"
import createPageDependency from "../redux/actions/add-page-dependency"

const withResolverContext = require(`../schema/context`)
const { LocalNodeModel } = require(`../schema/node-model`)
import withResolverContext from "../schema/context"
import { LocalNodeModel } from "../schema/node-model"
import { Store } from "redux"
import { IReduxState } from "../redux/types"

type Query = string | Source

class GraphQLRunner {
constructor(store) {
this.store = store
const nodeStore = require(`../db/nodes`)
const createPageDependency = require(`../redux/actions/add-page-dependency`)
parseCache: Map<Query, DocumentNode>

// eslint-disable-next-line @typescript-eslint/no-explicit-any
nodeModel: any // TODO: convert "../schema/node-model" from Flow

schema: GraphQLSchema

validDocuments: WeakSet<DocumentNode>
scheduleClearCache: () => void

constructor(protected store: Store<IReduxState>) {
const { schema, schemaCustomization } = this.store.getState()

this.nodeModel = new LocalNodeModel({
Expand All @@ -23,19 +45,22 @@ class GraphQLRunner {
this.scheduleClearCache = debounce(this.clearCache.bind(this), 5000)
}

clearCache() {
clearCache(): void {
this.parseCache.clear()
this.validDocuments = new WeakSet()
}

parse(query) {
parse(query: Query): DocumentNode {
if (!this.parseCache.has(query)) {
this.parseCache.set(query, parse(query))
}
return this.parseCache.get(query)
return this.parseCache.get(query) as DocumentNode
}

validate(schema, document) {
validate(
schema: GraphQLSchema,
document: DocumentNode
): readonly GraphQLError[] {
if (!this.validDocuments.has(document)) {
const errors = validate(schema, document)
if (!errors.length) {
Expand All @@ -46,7 +71,8 @@ class GraphQLRunner {
return []
}

query(query, context) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
query(query: Query, context: Record<string, any>): Promise<ExecutionResult> {
const { schema, schemaCustomization } = this.store.getState()

if (this.schema !== schema) {
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GraphQLSchema } from "graphql"

export enum ProgramStatus {
BOOTSTRAP_FINISHED = `BOOTSTRAP_FINISHED`,
BOOTSTRAP_QUERY_RUNNING_FINISHED = `BOOTSTRAP_QUERY_RUNNING_FINISHED`,
Expand All @@ -24,7 +26,7 @@ export interface IReduxState {
jobs: {
active: Array<any> // TODO
}
schema: any
schema: GraphQLSchema
schemaCustomization: any
config: {
developMiddleware: any
Expand Down

0 comments on commit 9fdc395

Please sign in to comment.