How to create more complex types in graphql using code first approach

Viewed 23

Hello I'm trying to create field for mutation which is array of objects. Code looks like this:

import { Field, InputType } from '@nestjs/graphql';

type ChainToAddress = {
  chain: string;
  address: string;
};

@InputType()
export class CreateONSInput {
  @Field()
  ons: string;

  @Field(type => [ChainToAddress])
  chainAddressess: ChainToAddress;
}

Currently is throwing following error:

'ChainToAddress' only refers to a type, but is being used as a value here.

Any idea how to make it work, tried with interfaces / type but somehow not able to manage it

1 Answers

You need to create an InputType/ObjectType out of your nested interface to make it work.

@InputType()
class ChainToAddress {
  @Field()
  chain: string;
  @Field()
  address: string;
};

@InputType()
export class CreateONSInput {
  @Field()
  ons: string;

  @Field(type => [ChainToAddress])
  chainAddressess: ChainToAddress[];
}
Related