Skip to main content

Arguments

If you wish to define arguments for a field, you must define your argument types explicitly using a type literal in your function or method signature. This allows Grats to "see" the types being used.

/** @gqlField */
myField(args: { greeting: string }): string {
return `${args.greeting} World`;
}

Functional style fields

In functional style fields, the arguments object is the second argument to the function. The first argument is the instance of the base type being extended.

/** @gqlField */
export function userById(_: Query, args: { id: string }): User {
return DB.getUserById(args.id);
}

Default values

Default values for arguments can be defined by using the = operator with destructuring. Note tha tyou must perform the destructuring in the argument list, not in the function body:

class MyClass {
/** @gqlField */
myField({ greeting = "Hello" }: { greeting: string }): string {
return `${greeting} World`;
}
}

Deeply nested default values can also be defined:

class MyClass {
/** @gqlField */
myField({
greeting = { salutation: "Sup" },
}: {
greeting: GreetingConfig;
}): string {
return `${greeting.salutation} World`;
}
}

/** @gqlInput */
type GreetingConfig = {
salutation: string;
};

Deprecated arguments

Optional arguments can be marked as @deprecated by using the @deprecated JSDoc tag:

class MyClass {
/** @gqlField */
myField(_: {
/** @deprecated Unused! */
greeting?: string;
}): string {
return `Hello World`;
}
}