Docblock tags
Arguments
On this page
Any resolver parameter typed with a valid GraphQL type is a GraphQL argument. Gratos generates the argument's definition from the parameter's type and name.
/** @gqlType */
class MyType {
/** @gqlField */
myField(greeting: string): string {
return `${greeting} World`;
}
}type MyType {
myField(greeting: String!): String
}Functional style fields
In a functional style field, the source object must always be the first argument, so GraphQL arguments come after it.
/** @gqlType */
type User = {
/** @gqlField */
name: string;
};
/** @gqlField */
export function greeting(user: User, salutation: string): string {
return `${salutation} ${user.name}`;
}type User {
greeting(salutation: String!): String
name: String
}Default values
A TypeScript default value is the argument's default value in the schema. The default value must be expressible as a GraphQL literal.
/** @gqlType */
class MyClass {
/** @gqlField */
myField(greeting: string = "Hello"): string {
return `${greeting} World`;
}
}type MyClass {
myField(greeting: String! = "Hello"): String
}Deprecated arguments
An optional argument can be marked @deprecated with the @deprecated JSDoc tag:
/** @gqlType */
class MyClass {
/** @gqlField */
myField(
/** @deprecated Unused! */
greeting?: string | null,
): string {
return `Hello World`;
}
}type MyClass {
myField(greeting: String @deprecated(reason: "Unused!")): String
}Nullable arguments
When an argument is nullable in your GraphQL schema, graphql-js may pass an explicit null (when the query passes an explicit null), or leave the argument undefined (when the query omits it, or passes it a variable the operation was not given).
So Gratos requires an argument that is either nullable (someArg: T | null) or optional (someArg?: T) to be both nullable and optional: someArg?: T | null. That way your resolver handles both cases.
The one exception is an argument with a default value: a parameter with a default is optional, and it may stay non-nullable.
/** @gqlQueryField */
export function greeting(name: string = "Max"): string {
return `Hello, ${name}`;
}type Query {
greeting(name: String! = "Max"): String
}The name argument is non-nullable in the schema: a query may not pass an explicit null, and when it omits the argument, the default value is used.
Object-map style fields
To match the graphql-js resolver signature more closely, you may also define your arguments as a single object parameter. It may be in any position, but may not be combined with positional GraphQL arguments. It is not the recommended style, but it is fully supported.
/** @gqlType */
class MyType {
/** @gqlField */
myField(args: { greeting: string }): string {
return `${args.greeting} World`;
}
}type MyType {
myField(greeting: String!): String
}In this style, define default values with the = operator, destructuring. You must destructure in the parameter list, not in the function body:
/** @gqlType */
class MyClass {
/** @gqlField */
myField({ greeting = "Hello" }: { greeting: string }): string {
return `${greeting} World`;
}
}type MyClass {
myField(greeting: String! = "Hello"): String
}As with positional arguments, an object-map argument can be marked @deprecated with the @deprecated JSDoc tag. It must be optional, so, as above, it must accept null too:
/** @gqlType */
class MyClass {
/** @gqlField */
myField(_: {
/** @deprecated Unused! */
greeting?: string | null;
}): string {
return `Hello World`;
}
}type MyClass {
myField(greeting: String @deprecated(reason: "Unused!")): String
}