nestjs/graphql: How to return mixed array of values and nulls?

88 Views Asked by At

I'm using @nestjs/graphql to build graphql API. I have the following object

@ObjectType('Round')
export class RoundDto {
  @Field(() => GraphQLInt)
  round: number;

  @Field(() => [[GraphQLString]])
  teams: Nullable<[string, string]>[];
}

How Can I declare the teams field to return mixed values of string and null? Do I need to write own scalar for it?

I'm expecting to receive teams like

teams: [[1, 2], null, [3, 4], null]

But getting the error Cannot return null for non-nullable field Round.teams.

1

There are 1 best solutions below

0
Pavlo Naumenko On

Regarding this Answer to this question, the decision is returning a tuple or empty array

@ObjectType('Round')
export class RoundDto {
  @Field(() => GraphQLInt)
  round: number;

  @Field(() => [[GraphQLString]])
  teams: ([string, string] | [])[];
}

The answer will be teams: [[1, 2],[], [3, 4],[]]