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.

MyType.ts
/** @gqlType */
class MyType {
  /** @gqlField */
  myField(greeting: string): string {
    return `${greeting} World`;
  }
}

Functional style fields

In a functional style field, the source object must always be the first argument, so GraphQL arguments come after it.

greeting.ts
/** @gqlType */
type User = {
  /** @gqlField */
  name: string;
};

/** @gqlField */
export function greeting(user: User, salutation: string): string {
  return `${salutation} ${user.name}`;
}

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.

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

Deprecated arguments

An optional argument can be marked @deprecated with the @deprecated JSDoc tag:

MyClass.ts
/** @gqlType */
class MyClass {
  /** @gqlField */
  myField(
    /** @deprecated Unused! */
    greeting?: string | null,
  ): string {
    return `Hello World`;
  }
}

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.

greeting.ts
/** @gqlQueryField */
export function greeting(name: string = "Max"): string {
  return `Hello, ${name}`;
}

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.

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

In this style, define default values with the = operator, destructuring. You must destructure in the parameter list, not in the function body:

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

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:

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