From 2ef310bd5bf523128db2716ebc601c1b06d34828 Mon Sep 17 00:00:00 2001 From: Yiming Pan <3207113562@qq.com> Date: Fri, 17 Jul 2020 22:04:03 +0800 Subject: [PATCH] chore(gatsby): Migrate type-builders to TypeScript (#23892) * chore(gatsby): Migrate type-builders to TypeScript * chore(gatsby): Migrate type-builders to TypeScript Co-authored-by: Blaine Kasten --- .../gatsby/src/redux/actions/restricted.ts | 4 +- .../src/schema/__tests__/build-schema.js | 4 +- .../gatsby/src/schema/__tests__/context.js | 2 +- packages/gatsby/src/schema/__tests__/print.js | 2 +- .../src/schema/__tests__/rebuild-schema.js | 2 +- .../extensions/__tests__/child-relations.js | 2 +- .../extensions/__tests__/field-extensions.js | 2 +- .../schema/extensions/__tests__/interfaces.js | 5 +- .../src/schema/infer/__tests__/infer.js | 6 +- .../src/schema/infer/__tests__/merge-types.js | 4 +- packages/gatsby/src/schema/schema.js | 2 +- .../gatsby/src/schema/types/type-builders.js | 103 ---------------- .../gatsby/src/schema/types/type-builders.ts | 116 ++++++++++++++++++ packages/gatsby/src/schema/types/type-defs.js | 2 +- packages/gatsby/src/utils/api-runner-node.js | 4 +- 15 files changed, 135 insertions(+), 125 deletions(-) delete mode 100644 packages/gatsby/src/schema/types/type-builders.js create mode 100644 packages/gatsby/src/schema/types/type-builders.ts diff --git a/packages/gatsby/src/redux/actions/restricted.ts b/packages/gatsby/src/redux/actions/restricted.ts index 8cb4a68b89f0b..8692cfddb4b91 100644 --- a/packages/gatsby/src/redux/actions/restricted.ts +++ b/packages/gatsby/src/redux/actions/restricted.ts @@ -204,8 +204,8 @@ export const actions = { types: | string | GraphQLOutputType - | GatsbyGraphQLType - | Array, + | GatsbyGraphQLType + | Array>, plugin: IGatsbyPlugin, traceId?: string ): ICreateTypes => { diff --git a/packages/gatsby/src/schema/__tests__/build-schema.js b/packages/gatsby/src/schema/__tests__/build-schema.js index 25842571acbd1..780ad598ff13e 100644 --- a/packages/gatsby/src/schema/__tests__/build-schema.js +++ b/packages/gatsby/src/schema/__tests__/build-schema.js @@ -13,11 +13,11 @@ jest.mock(`../../utils/api-runner-node`) const { store } = require(`../../redux`) const { actions } = require(`../../redux/actions`) const { build } = require(`..`) -const { +import { buildObjectType, buildUnionType, buildInterfaceType, -} = require(`../types/type-builders`) +} from "../types/type-builders" const withResolverContext = require(`../context`) const nodes = require(`./fixtures/node-model`) diff --git a/packages/gatsby/src/schema/__tests__/context.js b/packages/gatsby/src/schema/__tests__/context.js index 244c83f11a040..ff15c64f3a512 100644 --- a/packages/gatsby/src/schema/__tests__/context.js +++ b/packages/gatsby/src/schema/__tests__/context.js @@ -1,7 +1,7 @@ const { graphql } = require(`graphql`) const { build } = require(`..`) const withResolverContext = require(`../context`) -const { buildObjectType } = require(`../types/type-builders`) +import { buildObjectType } from "../types/type-builders" const { store } = require(`../../redux`) const { dispatch } = store const { actions } = require(`../../redux/actions/restricted`) diff --git a/packages/gatsby/src/schema/__tests__/print.js b/packages/gatsby/src/schema/__tests__/print.js index bcfc573f6a64e..dfccfddac0b6b 100644 --- a/packages/gatsby/src/schema/__tests__/print.js +++ b/packages/gatsby/src/schema/__tests__/print.js @@ -1,5 +1,5 @@ const { build } = require(`..`) -const { buildObjectType } = require(`../types/type-builders`) +import { buildObjectType } from "../types/type-builders" const { store } = require(`../../redux`) const { actions } = require(`../../redux/actions/restricted`) const { printTypeDefinitions } = actions diff --git a/packages/gatsby/src/schema/__tests__/rebuild-schema.js b/packages/gatsby/src/schema/__tests__/rebuild-schema.js index 1dc1029f9ee3e..168f92298aeaa 100644 --- a/packages/gatsby/src/schema/__tests__/rebuild-schema.js +++ b/packages/gatsby/src/schema/__tests__/rebuild-schema.js @@ -10,7 +10,7 @@ const { const { store } = require(`../../redux`) const { actions } = require(`../../redux/actions`) const { build, rebuild } = require(`..`) -const { buildObjectType } = require(`../types/type-builders`) +import { buildObjectType } from "../types/type-builders" jest.mock(`../../utils/api-runner-node`) diff --git a/packages/gatsby/src/schema/extensions/__tests__/child-relations.js b/packages/gatsby/src/schema/extensions/__tests__/child-relations.js index 2c02e441ff717..770bfdc863ad5 100644 --- a/packages/gatsby/src/schema/extensions/__tests__/child-relations.js +++ b/packages/gatsby/src/schema/extensions/__tests__/child-relations.js @@ -1,7 +1,7 @@ const { graphql } = require(`graphql`) const { build } = require(`../..`) const withResolverContext = require(`../../context`) -const { buildObjectType } = require(`../../types/type-builders`) +import { buildObjectType } from "../../types/type-builders" const { store } = require(`../../../redux`) const { dispatch } = store const { actions } = require(`../../../redux/actions`) diff --git a/packages/gatsby/src/schema/extensions/__tests__/field-extensions.js b/packages/gatsby/src/schema/extensions/__tests__/field-extensions.js index 3e20eebb9cd6f..00fa9e326e962 100644 --- a/packages/gatsby/src/schema/extensions/__tests__/field-extensions.js +++ b/packages/gatsby/src/schema/extensions/__tests__/field-extensions.js @@ -1,7 +1,7 @@ const { GraphQLString, graphql } = require(`graphql`) const { build } = require(`../..`) const withResolverContext = require(`../../context`) -const { buildObjectType } = require(`../../types/type-builders`) +import { buildObjectType } from "../../types/type-builders" const { store } = require(`../../../redux`) const { actions } = require(`../../../redux/actions`) const { dispatch } = store diff --git a/packages/gatsby/src/schema/extensions/__tests__/interfaces.js b/packages/gatsby/src/schema/extensions/__tests__/interfaces.js index 1f5cab9b687d3..dcacc4ca3488a 100644 --- a/packages/gatsby/src/schema/extensions/__tests__/interfaces.js +++ b/packages/gatsby/src/schema/extensions/__tests__/interfaces.js @@ -1,10 +1,7 @@ const { graphql } = require(`graphql`) const { build } = require(`../..`) const withResolverContext = require(`../../context`) -const { - buildInterfaceType, - buildObjectType, -} = require(`../../types/type-builders`) +import { buildInterfaceType, buildObjectType } from "../../types/type-builders" const { store } = require(`../../../redux`) const { dispatch } = store const { actions } = require(`../../../redux/actions`) diff --git a/packages/gatsby/src/schema/infer/__tests__/infer.js b/packages/gatsby/src/schema/infer/__tests__/infer.js index 001be701cb6ec..392f887bb4c43 100644 --- a/packages/gatsby/src/schema/infer/__tests__/infer.js +++ b/packages/gatsby/src/schema/infer/__tests__/infer.js @@ -7,7 +7,7 @@ const { store } = require(`../../../redux`) const { actions } = require(`../../../redux/actions`) const { buildSchema } = require(`../../schema`) const { createSchemaComposer } = require(`../../schema-composer`) -const { buildObjectType } = require(`../../types/type-builders`) +import { buildObjectType } from "../../types/type-builders" const { hasNodes } = require(`../inference-metadata`) const { TypeConflictReporter } = require(`../type-conflict-reporter`) const withResolverContext = require(`../../context`) @@ -1007,7 +1007,7 @@ Object { }, ].concat(getFileNodes()) - let result = await getQueryResult( + const result = await getQueryResult( nodes, ` file { @@ -1032,7 +1032,7 @@ Object { }, ].concat(getFileNodes()) - let result = await getQueryResult( + const result = await getQueryResult( nodes, ` files { diff --git a/packages/gatsby/src/schema/infer/__tests__/merge-types.js b/packages/gatsby/src/schema/infer/__tests__/merge-types.js index 6396478ce7a29..ebd132cb0a70e 100644 --- a/packages/gatsby/src/schema/infer/__tests__/merge-types.js +++ b/packages/gatsby/src/schema/infer/__tests__/merge-types.js @@ -1,4 +1,4 @@ -const { buildObjectType } = require(`../../types/type-builders`) +import { buildObjectType } from "../../types/type-builders" const { store } = require(`../../../redux`) const { build } = require(`../..`) const { actions } = require(`../../../redux/actions`) @@ -133,7 +133,7 @@ describe(`merges explicit and inferred type definitions`, () => { infer, addDefaultResolvers, }) => { - let extensions = {} + const extensions = {} if (infer != null) { extensions.infer = infer if (addDefaultResolvers != null) { diff --git a/packages/gatsby/src/schema/schema.js b/packages/gatsby/src/schema/schema.js index 15f09c8a4276a..9fa6f31291e94 100644 --- a/packages/gatsby/src/schema/schema.js +++ b/packages/gatsby/src/schema/schema.js @@ -37,7 +37,7 @@ const { import { getPagination } from "./types/pagination" const { getSortInput, SORTABLE_ENUM } = require(`./types/sort`) const { getFilterInput, SEARCHABLE_ENUM } = require(`./types/filter`) -const { isGatsbyType, GatsbyGraphQLTypeKind } = require(`./types/type-builders`) +import { isGatsbyType, GatsbyGraphQLTypeKind } from "./types/type-builders" const { isASTDocument, parseTypeDef, diff --git a/packages/gatsby/src/schema/types/type-builders.js b/packages/gatsby/src/schema/types/type-builders.js deleted file mode 100644 index 0e6ec14731e8d..0000000000000 --- a/packages/gatsby/src/schema/types/type-builders.js +++ /dev/null @@ -1,103 +0,0 @@ -// @flow - -import type { - ComposeObjectTypeConfig, - ComposeInputObjectTypeConfig, - ComposeInterfaceTypeConfig, - ComposeUnionTypeConfig, - ComposeEnumTypeConfig, - ComposeScalarTypeConfig, -} from "graphql-compose" - -const GatsbyGraphQLTypeKind = { - OBJECT: `OBJECT`, - INPUT_OBJECT: `INPUT_OBJECT`, - UNION: `UNION`, - INTERFACE: `INTERFACE`, - ENUM: `ENUM`, - SCALAR: `SCALAR`, -} - -export type GatsbyGraphQLType = - | { - kind: GatsbyGraphQLTypeKind.OBJECT, - config: ComposeObjectTypeConfig, - } - | { - kind: GatsbyGraphQLTypeKind.INPUT_OBJECT, - config: ComposeInputObjectTypeConfig, - } - | { - kind: GatsbyGraphQLTypeKind.UNION, - config: ComposeUnionTypeConfig, - } - | { - kind: GatsbyGraphQLTypeKind.INTERFACE, - config: ComposeInterfaceTypeConfig, - } - | { - kind: GatsbyGraphQLTypeKind.ENUM, - config: ComposeEnumTypeConfig, - } - | { - kind: GatsbyGraphQLTypeKind.SCALAR, - config: ComposeScalarTypeConfig, - } - -const buildObjectType = config => { - return { - kind: GatsbyGraphQLTypeKind.OBJECT, - config, - } -} - -const buildUnionType = config => { - return { - kind: GatsbyGraphQLTypeKind.UNION, - config, - } -} - -const buildInterfaceType = config => { - return { - kind: GatsbyGraphQLTypeKind.INTERFACE, - config, - } -} - -const buildInputObjectType = config => { - return { - kind: GatsbyGraphQLTypeKind.INPUT_OBJECT, - config, - } -} - -const buildEnumType = config => { - return { - kind: GatsbyGraphQLTypeKind.ENUM, - config, - } -} - -const buildScalarType = config => { - return { - kind: GatsbyGraphQLTypeKind.SCALAR, - config, - } -} - -const isGatsbyType = something => - typeof something === `object` && - something.kind && - GatsbyGraphQLTypeKind[something.kind] - -module.exports = { - GatsbyGraphQLTypeKind, - buildObjectType, - buildUnionType, - buildInterfaceType, - buildInputObjectType, - buildEnumType, - buildScalarType, - isGatsbyType, -} diff --git a/packages/gatsby/src/schema/types/type-builders.ts b/packages/gatsby/src/schema/types/type-builders.ts new file mode 100644 index 0000000000000..ae8d490045b56 --- /dev/null +++ b/packages/gatsby/src/schema/types/type-builders.ts @@ -0,0 +1,116 @@ +import { + ComposeObjectTypeConfig, + ComposeInputObjectTypeConfig, + ComposeInterfaceTypeConfig, + ComposeUnionTypeConfig, + ComposeEnumTypeConfig, + ComposeScalarTypeConfig, +} from "graphql-compose" + +enum GatsbyGraphQLTypeKind { + OBJECT = `OBJECT`, + INPUT_OBJECT = `INPUT_OBJECT`, + UNION = `UNION`, + INTERFACE = `INTERFACE`, + ENUM = `ENUM`, + SCALAR = `SCALAR`, +} + +export type GatsbyGraphQLType = + | { + kind: GatsbyGraphQLTypeKind.OBJECT + config: ComposeObjectTypeConfig + } + | { + kind: GatsbyGraphQLTypeKind.INPUT_OBJECT + config: ComposeInputObjectTypeConfig + } + | { + kind: GatsbyGraphQLTypeKind.UNION + config: ComposeUnionTypeConfig + } + | { + kind: GatsbyGraphQLTypeKind.INTERFACE + config: ComposeInterfaceTypeConfig + } + | { + kind: GatsbyGraphQLTypeKind.ENUM + config: ComposeEnumTypeConfig + } + | { + kind: GatsbyGraphQLTypeKind.SCALAR + config: ComposeScalarTypeConfig + } + +function buildObjectType( + config: ComposeObjectTypeConfig +): GatsbyGraphQLType { + return { + kind: GatsbyGraphQLTypeKind.OBJECT, + config, + } +} + +function buildUnionType( + config: ComposeUnionTypeConfig +): GatsbyGraphQLType { + return { + kind: GatsbyGraphQLTypeKind.UNION, + config, + } +} + +function buildInterfaceType( + config: ComposeInterfaceTypeConfig +): GatsbyGraphQLType { + return { + kind: GatsbyGraphQLTypeKind.INTERFACE, + config, + } +} + +function buildInputObjectType( + config: ComposeInputObjectTypeConfig +): GatsbyGraphQLType { + return { + kind: GatsbyGraphQLTypeKind.INPUT_OBJECT, + config, + } +} + +function buildEnumType( + config: ComposeEnumTypeConfig +): GatsbyGraphQLType { + return { + kind: GatsbyGraphQLTypeKind.ENUM, + config, + } +} + +function buildScalarType( + config: ComposeScalarTypeConfig +): GatsbyGraphQLType { + return { + kind: GatsbyGraphQLTypeKind.SCALAR, + config, + } +} + +function isGatsbyType(something: any): something is GatsbyGraphQLTypeKind { + return ( + typeof something === `object` && + something.kind && + GatsbyGraphQLTypeKind[something.kind] + ) +} + +export { + GatsbyGraphQLTypeKind, + buildObjectType, + buildUnionType, + buildInterfaceType, + buildInputObjectType, + buildEnumType, + buildScalarType, + isGatsbyType, +} diff --git a/packages/gatsby/src/schema/types/type-defs.js b/packages/gatsby/src/schema/types/type-defs.js index ef1a4bf0187d2..78a9691da130d 100644 --- a/packages/gatsby/src/schema/types/type-defs.js +++ b/packages/gatsby/src/schema/types/type-defs.js @@ -1,5 +1,5 @@ const { parse, Kind: GraphQLASTNodeKind } = require(`graphql`) -const { isGatsbyType } = require(`./type-builders`) +import { isGatsbyType } from "./type-builders" const { inferExtensionName, dontInferExtensionName } = require(`../extensions`) const report = require(`gatsby-cli/lib/reporter`) diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 9cb350d5df9a1..6949bdfbb6784 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -11,14 +11,14 @@ const fs = require(`fs-extra`) const { getCache } = require(`./get-cache`) import { createNodeId } from "./create-node-id" const { createContentDigest } = require(`gatsby-core-utils`) -const { +import { buildObjectType, buildUnionType, buildInterfaceType, buildInputObjectType, buildEnumType, buildScalarType, -} = require(`../schema/types/type-builders`) +} from "../schema/types/type-builders" const { emitter, store } = require(`../redux`) const { getNodes,