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
const TaskStatus: GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>
TaskStatus
=
const g: GWithContext<unknown>
g
.
enum: <{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>(config: {
values: {
readonly TODO: GEnumValueConfig<"todo">;
readonly IN_PROGRESS: GEnumValueConfig<"inProgress">;
readonly DONE: GEnumValueConfig<...>;
};
... 4 more ...;
extensionASTNodes?: Maybe<...>;
}) => GEnumType<...>
enum
({
name: string
name
: "TaskStatus",
values: {
readonly TODO: GEnumValueConfig<"todo">;
readonly IN_PROGRESS: GEnumValueConfig<"inProgress">;
readonly DONE: GEnumValueConfig<"done">;
}
values
: {
type TODO: GEnumValueConfig<"todo">
TODO
: {
value: any
value
: "todo" },
type IN_PROGRESS: GEnumValueConfig<"inProgress">
IN_PROGRESS
: {
value: any
value
: "inProgress" },
type DONE: GEnumValueConfig<"done">
DONE
: {
value: any
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
const field: GField<unknown, {
status: GArg<GNonNull<GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>>, false>;
}, GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>, unknown, unknown>
field
=
const g: GWithContext<unknown>
g
.
field: <unknown, GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>, (source: unknown, args: {
readonly status: "todo" | "inProgress" | "done";
}, context: unknown) => "todo" | ... 1 more ... | "done", {
...;
}>(field: FieldFuncArgs<...> & {
...;
}) => GField<...>
field
({
type: GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>
type
:
const TaskStatus: GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>
TaskStatus
,
args?: {
status: GArg<GNonNull<GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>>, false>;
}
args
: {
status: GArg<GNonNull<GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>>, false>
status
:
const g: GWithContext<unknown>
g
.
arg: <GNonNull<GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>>, undefined>(arg: {
type: GNonNull<GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>>;
description?: Maybe<...>;
extensions?: (Readonly<...> & Readonly<...>) | ... 1 more ... | undefined;
astNode?: Maybe<...>;
deprecationReason?: Maybe<...>;
} & {
...;
}) => GArg<...>
arg
({
type: GNonNull<GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>>
type
:
const g: GWithContext<unknown>
g
.
nonNull: <GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>>(of: GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>) => GNonNull<...>
nonNull
(
const TaskStatus: GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>
TaskStatus
) }) },
resolve: ((source: unknown, args: {
readonly status: "todo" | "inProgress" | "done";
}, context: unknown, info: GraphQLResolveInfo) => InferValueFromOutputType<GEnumType<{
readonly TODO: "todo";
readonly IN_PROGRESS: "inProgress";
readonly DONE: "done";
}>>) & ((source: unknown, args: {
...;
}, context: unknown) => "todo" | ... 1 more ... | "done")
resolve
(
source: unknown
source
,
args: {
readonly status: "todo" | "inProgress" | "done";
}
args
,
context: unknown
context
) {
return
args: {
readonly status: "todo" | "inProgress" | "done";
}
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
const TaskStatus: GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>
TaskStatus
=
const g: GWithContext<unknown>
g
.
enum: <{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>(config: {
values: {
TODO: GEnumValueConfig<"TODO">;
IN_PROGRESS: GEnumValueConfig<"IN_PROGRESS">;
DONE: GEnumValueConfig<...>;
};
... 4 more ...;
extensionASTNodes?: Maybe<...>;
}) => GEnumType<...>
enum
({
name: string
name
: "TaskStatus",
values: {
TODO: GEnumValueConfig<"TODO">;
IN_PROGRESS: GEnumValueConfig<"IN_PROGRESS">;
DONE: GEnumValueConfig<"DONE">;
}
values
:
const g: GWithContext<unknown>
g
.
enumValues: <["TODO", "IN_PROGRESS", "DONE"]>(values: readonly ["TODO", "IN_PROGRESS", "DONE"]) => {
TODO: GEnumValueConfig<"TODO">;
IN_PROGRESS: GEnumValueConfig<"IN_PROGRESS">;
DONE: GEnumValueConfig<...>;
}
enumValues
(["TODO", "IN_PROGRESS", "DONE"]),
});
const
const field: GField<unknown, {
status: GArg<GNonNull<GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>>, false>;
}, GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>, unknown, unknown>
field
=
const g: GWithContext<unknown>
g
.
field: <unknown, GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>, (source: unknown, args: {
readonly status: "TODO" | "IN_PROGRESS" | "DONE";
}, context: unknown) => "TODO" | "IN_PROGRESS" | "DONE", {
...;
}>(field: FieldFuncArgs<...> & {
...;
}) => GField<...>
field
({
type: GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>
type
:
const TaskStatus: GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>
TaskStatus
,
args?: {
status: GArg<GNonNull<GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>>, false>;
}
args
: {
status: GArg<GNonNull<GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>>, false>
status
:
const g: GWithContext<unknown>
g
.
arg: <GNonNull<GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>>, undefined>(arg: {
type: GNonNull<GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>>;
description?: Maybe<...>;
extensions?: (Readonly<...> & Readonly<...>) | ... 1 more ... | undefined;
astNode?: Maybe<...>;
deprecationReason?: Maybe<...>;
} & {
...;
}) => GArg<...>
arg
({
type: GNonNull<GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>>
type
:
const g: GWithContext<unknown>
g
.
nonNull: <GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>>(of: GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>) => GNonNull<GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>>
nonNull
(
const TaskStatus: GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>
TaskStatus
) }) },
resolve: ((source: unknown, args: {
readonly status: "TODO" | "IN_PROGRESS" | "DONE";
}, context: unknown, info: GraphQLResolveInfo) => InferValueFromOutputType<GEnumType<{
TODO: "TODO";
IN_PROGRESS: "IN_PROGRESS";
DONE: "DONE";
}>>) & ((source: unknown, args: {
...;
}, context: unknown) => "TODO" | ... 1 more ... | "DONE")
resolve
(
source: unknown
source
,
args: {
readonly status: "TODO" | "IN_PROGRESS" | "DONE";
}
args
,
context: unknown
context
) {
return
args: {
readonly status: "TODO" | "IN_PROGRESS" | "DONE";
}
args
.status;
status: "TODO" | "IN_PROGRESS" | "DONE"
},
});