1

In my NestJS project I'm creating a GraphQL endpoint for a service that returns a dictonary object:

{
 [key: number]: MyDataDto
}

In my .interfaces.ts file I would like to have something like:

interface ArrayAsObject<T> {
  [key: number]: T
}

@ObjectType()
export class MyDataDtoAsDictionary implements ArrayAsObject<MyDataDto> {
  @Field(() => MyDataDto)
  [key: number]: MyDataDto
}

But it gives me an error on the line with @Field

Decorators are not valid here. (ts1206)

If I remove the @Field decorator it throws:

Type MyDataDtoAsDictionary must define one or more fields.

What is the right way to decorate that kind of object for GraphQL resolver?

Sergei Klinov
  • 730
  • 2
  • 12
  • 25
  • Did you ever find the answer or did you choose another approach? – TheHiggsBroson Oct 19 '20 at 23:41
  • 1
    @TheHiggsBroson No, I haven't find any proper solution, so I had to create another service that would return an array of `MyDataDto` and handle it a bit differently in frontend app. – Sergei Klinov Oct 20 '20 at 04:04
  • Has anyone had the solution, graphql seems not to encourage it: https://stackoverflow.com/a/41559109/4984888 – Neo.Mxn0 Dec 15 '21 at 09:48

0 Answers0