Skip to content

Change Nullability

Alex Ald edited this page May 21, 2019 · 3 revisions

What is it for?

This decorator allows you to modify a field's nullability on a Graphql type. A decorator for PostGraphile's makeChangeNullabilityPlugin.

Decorator Parameters

Decorator takes a single parameter of type ChangeNullabilityOptions

interface ChangeNullabilityOptions {
  fieldName: string;
}

Example

We can use the ChangeNullability decorator to allow the email field on the User Graphql type to be null.

NOTE: This decorator usually will be paired with WrapResolver, since the field may initially not allow nulls, but if your WrapResolver modifies the resolver to sometimes return null, then ChangeNullability is needed.

@SchemaType({ typeName: 'User'})
export class UserType {

  @ChangeNullability({ fieldName: 'email'})
  public emailNullability() {
    return true;
  }
}

With Service

@SchemaType({ typeName: 'User'})
export class UserType {

  public constructor(private configService: ConfigurationService) {}

  @ChangeNullability({ fieldName: 'email'})
  public emailNullability() {
    return this.configService.get('isEmailNullable');
  }
}