Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions src/createSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
GraphQLBoolean,
GraphQLEnumType,
GraphQLEnumTypeConfig,
GraphQLFieldConfigArgumentMap,
GraphQLFieldConfigMap,
GraphQLFloat,
Expand All @@ -18,10 +20,11 @@ import {
GraphQLUnionType,
GraphQLError,
Kind,
ValueNode,
} from 'graphql';
import * as ts from 'typescript';
import {DateType} from './date';
import {typeAST, AllTypes, Interface, Primitive, Union, InterfaceLiteral, UnionLiteral} from 'ts-type-ast';
import {typeAST, AllTypes, Interface, Primitive, Union, InterfaceLiteral, UnionLiteral, Enum} from 'ts-type-ast';

type CustomScalarFactory = (type: Primitive) => GraphQLScalarType | undefined;
export function createSchema(
Expand Down Expand Up @@ -78,8 +81,8 @@ export function createSchema(
case 'interface':
case 'interfaceLiteral':
return createGQLType(type, isInput);
// case 'enum':
// return add(type, createGQLEnum(type));
case 'enum':
return add(type, createGQLEnum(type));
case 'union':
case 'unionLiteral':
if (isInput) return add(type, createGQLInputUnion(type));
Expand Down Expand Up @@ -179,7 +182,7 @@ export function createSchema(
description: type.kind === 'union' ? type.doc : undefined,
serialize: validate,
parseValue: validate,
parseLiteral(ast) {
parseLiteral(ast: ValueNode) {
if (ast.kind === Kind.STRING) {
return validate(ast.value);
}
Expand All @@ -205,6 +208,26 @@ export function createSchema(
}
throw new Error('Unexpected type: ' + JSON.stringify(type));
}

function createGQLEnum(type: Enum) {
const values = type.types.reduce((acc, item, index) => {
if (item.kind !== "primitive") {
throw new Error(`Only string enum values are supported: ${JSON.stringify(type)}`)
}
else {
if (item.type !== "string") {
throw new Error(`Only string enum values are supported: ${JSON.stringify(type)}`)
}
acc[item.literal as string] = {value: index}
}
return acc;
}, {} as GraphQLEnumTypeConfig["values"])

return new GraphQLEnumType({
name: type.name,
values
})
}
}

function never(never: never): never {
Expand Down