Skip to content

Enum Types

GraphQL Enum Types are a way to define a type that can be one of a set of predefined values.

const TaskStatus = g.enum({
name: "TaskStatus",
values: {
TODO: { value: "todo" },
IN_PROGRESS: { value: "inProgress" },
DONE: { value: "done" },
},
});

Note the key and value don’t have to be the same, the key defines what the enum is for consumers of the GraphQL API.

The value defines what the enum is for the schema implementation when it is received in/returned from resolvers. (this value can be any type, it is not constrained to a string, it could be a number, symbol, TypeScript enum value or any other value)

const field = g.field({
type: TaskStatus,
args: { status: g.arg({ type: g.nonNull(TaskStatus) }) },
resolve(source, args, context) {
return args.status;
status: "todo" | "inProgress" | "done"
},
});

Of course in most cases, the internal and external values will likely be the same so @graphql-ts/schema provides a shorthand for defining enum values with g.enumValues:

const TaskStatus = g.enum({
name: "TaskStatus",
values: g.enumValues(["TODO", "IN_PROGRESS", "DONE"]),
});
const field = g.field({
type: TaskStatus,
args: { status: g.arg({ type: g.nonNull(TaskStatus) }) },
resolve(source, args, context) {
return args.status;
status: "TODO" | "IN_PROGRESS" | "DONE"
},
});