Unable to infer GraphQL type from TypeScript reflection system

Viewed 12452

I'm trying to create an Alert class as follows:

@ObjectType()
export class Alert {
  @Field()
  status: 'error' | 'success' | 'warning' | 'info' | undefined

  @Field(() => [String])
  messages: string[];
}

However, it gives me the following error message:

Error: Unable to infer GraphQL type from TypeScript reflection system. You need to provide explicit type for 'status' of 'Alert' class.

2 Answers

Create a type for 'error' | 'success' | 'warning' | 'info' | undefined and add it to the field definition as such: @Field(() => StatusType)

It's basically complaining that it can't infer the type of that field as it's not a primitive, whenever a field is an object or other user defined type you will need to provide type information like I've described

my work around for the problem was an enum and tuple type.

//anything after EnumStatus is optional. depends on how much control you want.
type TSuccessArray = [EnumStatus.ERROR, EnumStatus.SUCCESS,EnumStatus.WARNING,EnumStatus.INFO,EnumStatus.UNDEFINED]

export enum EnumStatus {
        ERROR = "error",
        SUCCESS = 'success',
        WARNING = 'warning',
        INFO = "info",
        UNDEFINED = "undefined"
      }

    @ObjectType()
export class Alert {
  @Field(() => [String])
  status: TSuccessArray[];

  @Field(() => [String])
  messages: string[];
}

and somewhere in your code create a table or object with those options.

`const myArr:succesArray = [EnumStatus.ERROR, EnumStatus.SUCCESS,EnumStatus.WARNING,EnumStatus.INFO,EnumStatus.UNDEFINED]`;
Related