diff --git a/codegen/src/printers.ts b/codegen/src/printers.ts new file mode 100644 index 0000000..7c69e36 --- /dev/null +++ b/codegen/src/printers.ts @@ -0,0 +1,27 @@ +import { + GraphQLInputType, + GraphQLScalarType, + GraphQLEnumType, + GraphQLInputObjectType, +} from "graphql"; + +import { inputType, listType, toPrimitive } from "./utils"; + +export const printInputType = (type: GraphQLInputType): string => { + const isList = listType(type); + const base = inputType(type); + + return ( + (() => { + if (base instanceof GraphQLScalarType) { + return toPrimitive(base); + } else if (base instanceof GraphQLEnumType) { + return base.name; + } else if (base instanceof GraphQLInputObjectType) { + return "I" + base.name; + } else { + throw new Error("Unable to render inputType."); + } + })() + (isList ? "[]" : "") + ); +}; diff --git a/codegen/src/transforms/selectors.ts b/codegen/src/transforms/selectors.ts index 273e91d..4d07c28 100644 --- a/codegen/src/transforms/selectors.ts +++ b/codegen/src/transforms/selectors.ts @@ -3,18 +3,11 @@ import { ASTVisitor, Kind, GraphQLArgument, - isListType, - isNonNullType, GraphQLField, GraphQLObjectType, - GraphQLInputObjectType, - GraphQLInputType, - GraphQLOutputType, GraphQLNonNull, - GraphQLList, GraphQLScalarType, GraphQLEnumType, - GraphQLNamedType, GraphQLUnionType, GraphQLInterfaceType, DocumentNode, @@ -22,13 +15,8 @@ import { import { imp, code } from "ts-poet"; import { invariant } from "outvariant"; -import { - inputType, - outputType, - listType, - toPrimitive, - toLower, -} from "../utils"; +import { printInputType } from "../printers"; +import { inputType, outputType, toPrimitive, toLower } from "../utils"; const printConditionalNamedType = (types: string[]) => { const [first, ...rest] = types; @@ -57,20 +45,6 @@ const printConditionalSelectorArg = (types: string[]) => { } }; -const printInputType = (type: GraphQLInputType): string => { - const _base = inputType(type); - - if (_base instanceof GraphQLScalarType) { - return toPrimitive(_base); - } else if (_base instanceof GraphQLEnumType) { - return _base.name; - } else if (_base instanceof GraphQLInputObjectType) { - return "I" + _base.name; - } else { - throw new Error("Unable to render inputType."); - } -}; - const printArgument = (arg: GraphQLArgument): string => { const type = inputType(arg.type); const typename = diff --git a/codegen/src/transforms/types.ts b/codegen/src/transforms/types.ts index 549f334..a205f6f 100644 --- a/codegen/src/transforms/types.ts +++ b/codegen/src/transforms/types.ts @@ -3,18 +3,14 @@ import { ASTVisitor, Kind, GraphQLArgument, - isListType, isNonNullType, GraphQLField, GraphQLObjectType, GraphQLInputObjectType, GraphQLInputType, - GraphQLOutputType, GraphQLNonNull, - GraphQLList, GraphQLScalarType, GraphQLEnumType, - GraphQLNamedType, GraphQLUnionType, GraphQLInterfaceType, DocumentNode, @@ -23,22 +19,9 @@ import { import { code } from "ts-poet"; import { invariant } from "outvariant"; +import { printInputType } from "../printers"; import { inputType, outputType, listType, toPrimitive } from "../utils"; -const printInputType = (type: GraphQLInputType): string => { - const _base = inputType(type); - - if (_base instanceof GraphQLScalarType) { - return toPrimitive(_base); - } else if (_base instanceof GraphQLEnumType) { - return _base.name; - } else if (_base instanceof GraphQLInputObjectType) { - return "I" + _base.name; - } else { - throw new Error("Unable to render inputType."); - } -}; - const printVariable = (arg: GraphQLArgument): string => { return `${arg.name}: ${printInputType(arg.type)} ${ arg.type instanceof GraphQLNonNull ? "" : "| undefined" @@ -46,7 +29,7 @@ const printVariable = (arg: GraphQLArgument): string => { }; const printField = (field: GraphQLField): string => { - const { name, args } = field; + const { args } = field; const isList = listType(field.type); const isNonNull = field.type instanceof GraphQLNonNull; @@ -129,7 +112,7 @@ export const transform = ( const fields = Object.values(type.getFields()); const printField = (field: GraphQLInputField) => { - const isList = isListType(field.type); + const isList = listType(field.type); const isNonNull = isNonNullType(field.type); const baseType = inputType(field.type); diff --git a/codegen/src/utils.ts b/codegen/src/utils.ts index b232a48..7a2e125 100644 --- a/codegen/src/utils.ts +++ b/codegen/src/utils.ts @@ -36,7 +36,7 @@ export function outputType(type: GraphQLOutputType): GraphQLOutputType { } } -export function listType(type: GraphQLOutputType): boolean { +export function listType(type: GraphQLOutputType | GraphQLInputType): boolean { if (type instanceof GraphQLNonNull) { return listType(type.ofType); } else if (type instanceof GraphQLList) {