Skip to content

Commit

Permalink
chore(gatsby): migrate derived-types to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
sasurau4 committed Jul 15, 2020
1 parent f1b3a66 commit 3272f3e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/infer/add-inferred-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const report = require(`gatsby-cli/lib/reporter`)

import { isFile } from "./is-file"
import { isDate } from "../types/date"
const { addDerivedType } = require(`../types/derived-types`)
import { addDerivedType } from "../types/derived-types"
import { is32BitInteger } from "../../utils/is-32-bit-integer"
const { getNode, getNodes } = require(`../../redux/nodes`)

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 @@ -43,7 +43,7 @@ const {
parseTypeDef,
reportParsingError,
} = require(`./types/type-defs`)
const { clearDerivedTypes } = require(`./types/derived-types`)
import { clearDerivedTypes } from "./types/derived-types"
const { printTypeDefinitions } = require(`./print`)

const buildSchema = async ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,37 @@
* user explicitly defines `FooFields` type (via `createTypes` call) it won't be considered
* a derived type
*/
const { ObjectTypeComposer, InterfaceTypeComposer } = require(`graphql-compose`)
import {
ObjectTypeComposer,
InterfaceTypeComposer,
ScalarTypeComposer,
SchemaComposer,
InputTypeComposer,
EnumTypeComposer,
UnionTypeComposer,
} from "graphql-compose"

const clearDerivedTypes = ({ schemaComposer, typeComposer }) => {
type AllTypeComposer =
| ObjectTypeComposer
| InputTypeComposer
| EnumTypeComposer
| InterfaceTypeComposer
| UnionTypeComposer
| ScalarTypeComposer

const getDerivedTypes = ({
typeComposer,
}: {
typeComposer: AllTypeComposer
}): any => typeComposer.getExtension(`derivedTypes`) || new Set()

export const clearDerivedTypes = ({
schemaComposer,
typeComposer,
}: {
schemaComposer: SchemaComposer<any>
typeComposer: AllTypeComposer
}): void => {
const derivedTypes = getDerivedTypes({ typeComposer })

for (const typeName of derivedTypes.values()) {
Expand All @@ -50,15 +78,13 @@ const clearDerivedTypes = ({ schemaComposer, typeComposer }) => {
typeComposer.setExtension(`derivedTypes`, new Set())
}

const addDerivedType = ({ typeComposer, derivedTypeName }) => {
export const addDerivedType = ({
typeComposer,
derivedTypeName,
}: {
typeComposer: AllTypeComposer
derivedTypeName: string
}): void => {
const derivedTypes = getDerivedTypes({ typeComposer })
typeComposer.setExtension(`derivedTypes`, derivedTypes.add(derivedTypeName))
}

const getDerivedTypes = ({ typeComposer }) =>
typeComposer.getExtension(`derivedTypes`) || new Set()

module.exports = {
clearDerivedTypes,
addDerivedType,
}
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/types/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
GraphQLList,
isSpecifiedScalarType,
} = require(`graphql`)
const { addDerivedType } = require(`./derived-types`)
import { addDerivedType } from "./derived-types"
const { InputTypeComposer } = require(`graphql-compose`)
const { GraphQLJSON } = require(`graphql-compose`)
import { GraphQLDate } from "./date"
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/types/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
GraphQLInputObjectType,
GraphQLList,
} = require(`graphql`)
const { addDerivedType } = require(`./derived-types`)
import { addDerivedType } from "./derived-types"

const SORTABLE_ENUM = {
SORTABLE: `SORTABLE`,
Expand Down

0 comments on commit 3272f3e

Please sign in to comment.