简体   繁体   中英

Unable to infer GraphQL type from TypeScript reflection system

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.

Create a type for 'error' | 'success' | 'warning' | 'info' | undefined 'error' | 'success' | 'warning' | 'info' | undefined '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]`;

You can try adding this to your tsconfig.json file

"experimentalDecorators": true,
"emitDecoratorMetadata": true,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM