Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
Properly print list scalar input types (closes #93)
Browse files Browse the repository at this point in the history
  • Loading branch information
timkendall committed Jan 2, 2022
1 parent cabefd0 commit b16deb9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 49 deletions.
27 changes: 27 additions & 0 deletions codegen/src/printers.ts
Original file line number Diff line number Diff line change
@@ -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 ? "[]" : "")
);
};
30 changes: 2 additions & 28 deletions codegen/src/transforms/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,20 @@ import {
ASTVisitor,
Kind,
GraphQLArgument,
isListType,
isNonNullType,
GraphQLField,
GraphQLObjectType,
GraphQLInputObjectType,
GraphQLInputType,
GraphQLOutputType,
GraphQLNonNull,
GraphQLList,
GraphQLScalarType,
GraphQLEnumType,
GraphQLNamedType,
GraphQLUnionType,
GraphQLInterfaceType,
DocumentNode,
} from "graphql";
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;
Expand Down Expand Up @@ -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 =
Expand Down
23 changes: 3 additions & 20 deletions codegen/src/transforms/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ import {
ASTVisitor,
Kind,
GraphQLArgument,
isListType,
isNonNullType,
GraphQLField,
GraphQLObjectType,
GraphQLInputObjectType,
GraphQLInputType,
GraphQLOutputType,
GraphQLNonNull,
GraphQLList,
GraphQLScalarType,
GraphQLEnumType,
GraphQLNamedType,
GraphQLUnionType,
GraphQLInterfaceType,
DocumentNode,
Expand All @@ -23,30 +19,17 @@ 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"
}`;
};

const printField = (field: GraphQLField<any, any, any>): string => {
const { name, args } = field;
const { args } = field;

const isList = listType(field.type);
const isNonNull = field.type instanceof GraphQLNonNull;
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion codegen/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

1 comment on commit b16deb9

@vercel
Copy link

@vercel vercel bot commented on b16deb9 Jan 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Please sign in to comment.