0

I have a graphQL type defined in a .graphql file for a nestjs schema first application. The type is:

type address {
  id: String
  Title: String
  Url: String
}

Now, using nestjs I have a resolver function as

@Query('address')
async getAll(): Promise<any> {
  return await this.addressService.getAll();
}

The result from the await this.addressService.getAll() is an array of objects with the properties having _ in them like:

[
  {
    id: 1,
    address_title: 'title',
    address_url: 'url'

  }
]

I am also using @nestjs/graphql library for the @Query, @ResolveField and such. My problem is, how can I map the response properties to the graphql properties that I have written as a SDL in the .graphql file in nestjs framework? Is there any way we can map the graphql query properties to the result properties using nestjs?

I looked at the graphql alias, also mentioned here but that is not useful for me. Any help on this is highly appreciated.

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62

1 Answers1

0

You may use direct approach:

@Query('address')
getAll(): Promise<any> {
  return this.addressService.getAll().map(this.addressMapper);
}

private addressMapper(address: any): address {
  return {id: address.id, Title: address.address_title, Url: address_url};
}

Or use NestJS pipes.

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
Viktor Gusev
  • 555
  • 3
  • 12