From 670f92f66fa251d5e0dcff72fea5f9a3a48318bc Mon Sep 17 00:00:00 2001 From: Francesco Agnoletto Date: Tue, 16 Jun 2020 23:05:57 +0200 Subject: [PATCH] chore(gatsby): convert schema-customization to typescript (#24259) * Convert schema-customization to TS * Update import * Fix type errors * Revert "Fix type errors" This reverts commit 849c227972af0ca7df89cf93cc6c6a646934a5a0. * 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 --- .../gatsby/src/commands/develop-process.ts | 20 +++++-------- packages/gatsby/src/redux/reducers/index.ts | 2 +- ...stomization.js => schema-customization.ts} | 11 +++++-- packages/gatsby/src/redux/types.ts | 29 +++++++++++++++---- packages/gatsby/src/schema/context.ts | 2 +- 5 files changed, 41 insertions(+), 23 deletions(-) rename packages/gatsby/src/redux/reducers/{schema-customization.js => schema-customization.ts} (83%) diff --git a/packages/gatsby/src/commands/develop-process.ts b/packages/gatsby/src/commands/develop-process.ts index f121ccbd8c148..6c61c38cafb7a 100644 --- a/packages/gatsby/src/commands/develop-process.ts +++ b/packages/gatsby/src/commands/develop-process.ts @@ -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" @@ -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" @@ -200,16 +199,13 @@ async function startServer(program: IProgram): Promise { graphqlEndpoint, graphqlHTTP( (): graphqlHTTP.OptionsData => { - const { - schema, - schemaCustomization, - }: { - schema: GraphQLSchema - schemaCustomization: { - composer: SchemaComposer - 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, diff --git a/packages/gatsby/src/redux/reducers/index.ts b/packages/gatsby/src/redux/reducers/index.ts index eab977e703ca9..a739e9d08ecda 100644 --- a/packages/gatsby/src/redux/reducers/index.ts +++ b/packages/gatsby/src/redux/reducers/index.ts @@ -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" /** diff --git a/packages/gatsby/src/redux/reducers/schema-customization.js b/packages/gatsby/src/redux/reducers/schema-customization.ts similarity index 83% rename from packages/gatsby/src/redux/reducers/schema-customization.js rename to packages/gatsby/src/redux/reducers/schema-customization.ts index 4793cb64604f1..52e6a23c78c71 100644 --- a/packages/gatsby/src/redux/reducers/schema-customization.js +++ b/packages/gatsby/src/redux/reducers/schema-customization.ts @@ -1,4 +1,6 @@ -const initialState = () => { +import { IGatsbyState, ActionsUnion } from "../types" + +const initialState = (): IGatsbyState["schemaCustomization"] => { return { composer: null, context: {}, @@ -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 { @@ -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, diff --git a/packages/gatsby/src/redux/types.ts b/packages/gatsby/src/redux/types.ts index 1d2efd41353f3..e8f38b641c2b6 100644 --- a/packages/gatsby/src/redux/types.ts +++ b/packages/gatsby/src/redux/types.ts @@ -213,12 +213,17 @@ export interface IGatsbyState { } } schemaCustomization: { - composer: SchemaComposer - context: {} // TODO - fieldExtensions: {} // TODO - printConfig: any // TODO - thridPartySchemas: any[] // TODO - types: any[] // TODO + composer: null | SchemaComposer + context: Record + fieldExtensions: GraphQLFieldExtensionDefinition + printConfig: { + path?: string + include?: { types?: Array; plugins?: Array } + exclude?: { types?: Array; plugins?: Array } + withFieldTypes?: boolean + } | null + thirdPartySchemas: GraphQLSchema[] + types: (string | { typeOrTypeDef: DocumentNode; plugin: IGatsbyPlugin })[] } themes: any // TODO logs: IGatsbyCLIState @@ -290,6 +295,9 @@ export type ActionsUnion = | ICreateJobAction | ISetJobAction | IEndJobAction + | ICreateResolverContext + | IClearSchemaCustomizationAction + | ISetSchemaComposerAction | IStartIncrementalInferenceAction | IBuildTypeMetadataAction | IDisableTypeInferenceAction @@ -505,6 +513,15 @@ export interface ICreateResolverContext { | { [camelCasedPluginNameWithoutPrefix: string]: IGatsbyPluginContext } } +interface IClearSchemaCustomizationAction { + type: `CLEAR_SCHEMA_CUSTOMIZATION` +} + +interface ISetSchemaComposerAction { + type: `SET_SCHEMA_COMPOSER` + payload: SchemaComposer +} + export interface ICreatePageAction { type: `CREATE_PAGE` payload: IGatsbyPage diff --git a/packages/gatsby/src/schema/context.ts b/packages/gatsby/src/schema/context.ts index 8413126fb756d..e12481b6a8777 100644 --- a/packages/gatsby/src/schema/context.ts +++ b/packages/gatsby/src/schema/context.ts @@ -18,7 +18,7 @@ export default function withResolverContext({ tracer, }: { schema: GraphQLSchema - schemaComposer: SchemaComposer> + schemaComposer: SchemaComposer> | null context?: Record customContext?: Record nodeModel?: any