Docblock tags
OneOf inputs
On this page
A OneOf input type is an input type of which exactly one field must be provided, and the value given for that field must not be null. OneOf inputs are part of the GraphQL specification since its September 2025 edition. It models structures similar to unions, for input types. graphql-js supports it from v16.9.0, and Gratos requires graphql 17, so no upgrade is needed.
To define a OneOf input, put @gqlInput in a docblock directly before a type alias of a union of object literal types.
Each member of the union must be an object type with exactly one property, whose value is a valid input type. Think of each member of the union as one of the values the user may provide.
You do not need @gqlAnnotate to add the @oneOf directive to your input type: Gratos adds it for you.
/**
* @gqlInput
*/
export type UserBy = { email: string } | { username: string };input UserBy @oneOf {
email: String
username: String
}OneOf input field descriptions
TypeScript does not "attach" docblocks to a member of a union. To describe a field of a OneOf input, put the description above the field, inside the object literal:
/**
* @gqlInput
*/
export type UserBy =
| {
/** Fetch the user by email */
email: string;
}
| {
/** Fetch the user by username */
username: string;
};input UserBy @oneOf {
"""Fetch the user by email"""
email: String
"""Fetch the user by username"""
username: String
}Working with OneOf inputs
When you handle one of these types in TypeScript, use an exhaustive switch: when you add an option to the union, TypeScript then reports an error everywhere the union is handled.
As of TypeScript 5.3, TypeScript supports a pattern called switch true narrowing, which ensures you have handled every option of this kind of union:
/**
* @gqlInput
*/
export type UserBy = { email: string } | { username: string };
/** @gqlQueryField */
export function getUser(by: UserBy): User {
switch (true) {
case "email" in by:
return User.fromEmail(by.email);
case "username" in by:
return User.fromUsername(by.username);
default: {
// This line will error if an unhandled option is added to the union
const _exhaustive: never = by;
throw new Error(`Unhandled case: ${JSON.stringify(by)}`);
}
}
}
/** @gqlType */
class User {
/** @gqlField */
email?: string;
/** @gqlField */
username?: string;
constructor(email?: string, username?: string) {
this.email = email;
this.username = username;
}
static fromEmail(email: string): User {
return new User(email, undefined);
}
static fromUsername(username: string): User {
return new User(undefined, username);
}
}input UserBy @oneOf {
email: String
username: String
}
type Query {
getUser(by: UserBy!): User
}
type User {
email: String
username: String
}