Hitesh Shingala
Hitesh Shingala

Reputation: 11

Unable to infer GraphQL type from TypeScript reflection system. You need to provide explicit type for 'id' of 'Address' class

import { ObjectType, ID, Int, Field } from 'type-graphql';
@ObjectType()
export default class Address {
  @Field(type => ID)
  id: String;

  @Field()
  type: string;

  @Field()
  title: string;

  @Field()
  location: string;
}

info: ts-node-dev ver. 1.0.0 (using ts-node ver. 9.1.1, typescript ver. 4.0.5)

while starting applciation using this command ts-node-dev --respawn server.shop.ts I have got following error. (Unable to infer GraphQL type from TypeScript reflection system. You need to provide explicit type for 'id' of 'Address' class.)

Upvotes: 1

Views: 5253

Answers (2)

Ant
Ant

Reputation: 3459

add

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

in your tsconfig.json

Upvotes: 5

fortunee
fortunee

Reputation: 4332

Define an interface and decorate it with the @InterfaceType() decorator

@InterfaceType()
abstract class IAddress {
  @Field(type => ID)
  id: string;

  @Field()
  type: string;

  @Field()
  title: string;

  @Field()
  location: string;
}

Then implement it in your Address class like this

@ObjectType({ implements: IAddress })
export default class Address implements IAddress {
  id: string;
  type: string;
  title: string;
  location: string
}

Read more on defining interfaces here

Upvotes: 0

Related Questions