Skip to content

Commit

Permalink
chore(gatsby): Migrate type-builders to TypeScript (gatsbyjs#23892)
Browse files Browse the repository at this point in the history
* chore(gatsby): Migrate type-builders to TypeScript

* chore(gatsby): Migrate type-builders to TypeScript

Co-authored-by: Blaine Kasten <blainekasten@gmail.com>
  • Loading branch information
PerfectPan and blainekasten authored Jul 17, 2020
1 parent cb282f5 commit 2ef310b
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 125 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby/src/redux/actions/restricted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ export const actions = {
types:
| string
| GraphQLOutputType
| GatsbyGraphQLType
| Array<string | GraphQLOutputType | GatsbyGraphQLType>,
| GatsbyGraphQLType<any, any>
| Array<string | GraphQLOutputType | GatsbyGraphQLType<any, any>>,
plugin: IGatsbyPlugin,
traceId?: string
): ICreateTypes => {
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/schema/__tests__/build-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/__tests__/context.js
Original file line number Diff line number Diff line change
@@ -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`)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/__tests__/print.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/__tests__/rebuild-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down
Original file line number Diff line number Diff line change
@@ -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`)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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`)
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby/src/schema/infer/__tests__/infer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -1007,7 +1007,7 @@ Object {
},
].concat(getFileNodes())

let result = await getQueryResult(
const result = await getQueryResult(
nodes,
`
file {
Expand All @@ -1032,7 +1032,7 @@ Object {
},
].concat(getFileNodes())

let result = await getQueryResult(
const result = await getQueryResult(
nodes,
`
files {
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/schema/infer/__tests__/merge-types.js
Original file line number Diff line number Diff line change
@@ -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`)
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
103 changes: 0 additions & 103 deletions packages/gatsby/src/schema/types/type-builders.js

This file was deleted.

116 changes: 116 additions & 0 deletions packages/gatsby/src/schema/types/type-builders.ts
Original file line number Diff line number Diff line change
@@ -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<TSource, TContext> =
| {
kind: GatsbyGraphQLTypeKind.OBJECT
config: ComposeObjectTypeConfig<TSource, TContext>
}
| {
kind: GatsbyGraphQLTypeKind.INPUT_OBJECT
config: ComposeInputObjectTypeConfig
}
| {
kind: GatsbyGraphQLTypeKind.UNION
config: ComposeUnionTypeConfig<TSource, TContext>
}
| {
kind: GatsbyGraphQLTypeKind.INTERFACE
config: ComposeInterfaceTypeConfig<TSource, TContext>
}
| {
kind: GatsbyGraphQLTypeKind.ENUM
config: ComposeEnumTypeConfig
}
| {
kind: GatsbyGraphQLTypeKind.SCALAR
config: ComposeScalarTypeConfig
}

function buildObjectType<TSource, TContext>(
config: ComposeObjectTypeConfig<TSource, TContext>
): GatsbyGraphQLType<TSource, TContext> {
return {
kind: GatsbyGraphQLTypeKind.OBJECT,
config,
}
}

function buildUnionType<TSource, TContext>(
config: ComposeUnionTypeConfig<TSource, TContext>
): GatsbyGraphQLType<TSource, TContext> {
return {
kind: GatsbyGraphQLTypeKind.UNION,
config,
}
}

function buildInterfaceType<TSource, TContext>(
config: ComposeInterfaceTypeConfig<TSource, TContext>
): GatsbyGraphQLType<TSource, TContext> {
return {
kind: GatsbyGraphQLTypeKind.INTERFACE,
config,
}
}

function buildInputObjectType<TSource, TContext>(
config: ComposeInputObjectTypeConfig
): GatsbyGraphQLType<TSource, TContext> {
return {
kind: GatsbyGraphQLTypeKind.INPUT_OBJECT,
config,
}
}

function buildEnumType<TSource, TContext>(
config: ComposeEnumTypeConfig
): GatsbyGraphQLType<TSource, TContext> {
return {
kind: GatsbyGraphQLTypeKind.ENUM,
config,
}
}

function buildScalarType<TSource, TContext>(
config: ComposeScalarTypeConfig
): GatsbyGraphQLType<TSource, TContext> {
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,
}
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/types/type-defs.js
Original file line number Diff line number Diff line change
@@ -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`)

Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/utils/api-runner-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2ef310b

Please sign in to comment.