Skip to content

Commit

Permalink
chore(gatsby): convert schema-customization to typescript (#24259)
Browse files Browse the repository at this point in the history
* Convert schema-customization to TS

* Update import

* Fix type errors

* Revert "Fix type errors"

This reverts commit 849c227.

* Remove unneded type

* Fix type

* Remove unneded imports

* Fix import

* Improve typings and defaults

* default null value and handle cases

* update yarn.lock

Co-authored-by: Blaine Kasten <blainekasten@gmail.com>
  • Loading branch information
Kornil and blainekasten committed Jun 16, 2020
1 parent a6ef610 commit 670f92f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
20 changes: 8 additions & 12 deletions packages/gatsby/src/commands/develop-process.ts
Expand Up @@ -2,7 +2,6 @@ import url from "url"
import fs from "fs"
import openurl from "better-opn"
import chokidar from "chokidar"
import { SchemaComposer } from "graphql-compose"

import webpackHotMiddleware from "webpack-hot-middleware"
import webpackDevMiddleware from "webpack-dev-middleware"
Expand All @@ -13,7 +12,7 @@ import webpack from "webpack"
import graphqlHTTP from "express-graphql"
import graphqlPlayground from "graphql-playground-middleware-express"
import graphiqlExplorer from "gatsby-graphiql-explorer"
import { formatError, GraphQLSchema } from "graphql"
import { formatError } from "graphql"

import webpackConfig from "../utils/webpack.config"
import bootstrap from "../bootstrap"
Expand Down Expand Up @@ -200,16 +199,13 @@ async function startServer(program: IProgram): Promise<IServer> {
graphqlEndpoint,
graphqlHTTP(
(): graphqlHTTP.OptionsData => {
const {
schema,
schemaCustomization,
}: {
schema: GraphQLSchema
schemaCustomization: {
composer: SchemaComposer<any>
context: any
}
} = store.getState()
const { schema, schemaCustomization } = store.getState()

if (!schemaCustomization.composer) {
throw new Error(
`A schema composer was not created in time. This is likely a gatsby bug. If you experienced this please create an issue.`
)
}

return {
schema,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/reducers/index.ts
Expand Up @@ -22,7 +22,7 @@ import programReducer from "./program"
import { resolvedNodesCacheReducer } from "./resolved-nodes"
import { nodesTouchedReducer } from "./nodes-touched"
import { flattenedPluginsReducer } from "./flattened-plugins"
import schemaCustomizationReducer from "./schema-customization"
import { schemaCustomizationReducer } from "./schema-customization"
import { inferenceMetadataReducer } from "./inference-metadata"

/**
Expand Down
@@ -1,4 +1,6 @@
const initialState = () => {
import { IGatsbyState, ActionsUnion } from "../types"

const initialState = (): IGatsbyState["schemaCustomization"] => {
return {
composer: null,
context: {},
Expand All @@ -9,7 +11,10 @@ const initialState = () => {
}
}

module.exports = (state = initialState(), action) => {
export const schemaCustomizationReducer = (
state: IGatsbyState["schemaCustomization"] = initialState(),
action: ActionsUnion
): IGatsbyState["schemaCustomization"] => {
switch (action.type) {
case `ADD_THIRD_PARTY_SCHEMA`:
return {
Expand All @@ -22,7 +27,7 @@ module.exports = (state = initialState(), action) => {
composer: action.payload,
}
case `CREATE_TYPES`: {
let types
let types: IGatsbyState["schemaCustomization"]["types"]
if (Array.isArray(action.payload)) {
types = [
...state.types,
Expand Down
29 changes: 23 additions & 6 deletions packages/gatsby/src/redux/types.ts
Expand Up @@ -213,12 +213,17 @@ export interface IGatsbyState {
}
}
schemaCustomization: {
composer: SchemaComposer<any>
context: {} // TODO
fieldExtensions: {} // TODO
printConfig: any // TODO
thridPartySchemas: any[] // TODO
types: any[] // TODO
composer: null | SchemaComposer<any>
context: Record<string, any>
fieldExtensions: GraphQLFieldExtensionDefinition
printConfig: {
path?: string
include?: { types?: Array<string>; plugins?: Array<string> }
exclude?: { types?: Array<string>; plugins?: Array<string> }
withFieldTypes?: boolean
} | null
thirdPartySchemas: GraphQLSchema[]
types: (string | { typeOrTypeDef: DocumentNode; plugin: IGatsbyPlugin })[]
}
themes: any // TODO
logs: IGatsbyCLIState
Expand Down Expand Up @@ -290,6 +295,9 @@ export type ActionsUnion =
| ICreateJobAction
| ISetJobAction
| IEndJobAction
| ICreateResolverContext
| IClearSchemaCustomizationAction
| ISetSchemaComposerAction
| IStartIncrementalInferenceAction
| IBuildTypeMetadataAction
| IDisableTypeInferenceAction
Expand Down Expand Up @@ -505,6 +513,15 @@ export interface ICreateResolverContext {
| { [camelCasedPluginNameWithoutPrefix: string]: IGatsbyPluginContext }
}

interface IClearSchemaCustomizationAction {
type: `CLEAR_SCHEMA_CUSTOMIZATION`
}

interface ISetSchemaComposerAction {
type: `SET_SCHEMA_COMPOSER`
payload: SchemaComposer<any>
}

export interface ICreatePageAction {
type: `CREATE_PAGE`
payload: IGatsbyPage
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/context.ts
Expand Up @@ -18,7 +18,7 @@ export default function withResolverContext<TSource, TArgs>({
tracer,
}: {
schema: GraphQLSchema
schemaComposer: SchemaComposer<IGatsbyResolverContext<TSource, TArgs>>
schemaComposer: SchemaComposer<IGatsbyResolverContext<TSource, TArgs>> | null
context?: Record<string, any>
customContext?: Record<string, any>
nodeModel?: any
Expand Down

0 comments on commit 670f92f

Please sign in to comment.