Nestjs GraphQL Apollo Federation - resolver has an object in debug, returns an object with nulls

889 Views Asked by At

I am using NestJs, GraphQL apollo federation (schema first approach) and Mongoose.

While querying for the product or products resolver, the query returns an object where all the properties are nulls.

However, while I debug and use a breakpoint in my service/resolver I can see that these functions are returning the correct mongo document(s).

Code snippets by file:

{
  "data": {
    "product": {
      "id": null,
      "name": null
    }
  }
}
// product.graphql
type Product {
  id: String
  name: String
  displayName: String
  productId: Int
  partnerId: String
}

type Query {
  product(id: String): Product
  products: [Product]
}
// product.model.ts
@MongooseSchema()
export class ProductDocument extends Document implements Product {
  @Prop({ type: mongoose.Types.ObjectId })
  @Transform((value) => value.toString())
  _id: string;

  @Prop({ type: mongoose.Types.ObjectId })
  @Transform((value) => value.toString())
  id: string;

  @Prop()
  displayName: string;

  @Prop()
  name: string;

  @Prop()
  productId: number;

  @Prop({ type: mongoose.Types.ObjectId })
  @Transform((value) => value.toString())
  partnerId: string;
}
// products.resolvers.ts
import { Args, Query, Resolver, ResolveReference } from '@nestjs/graphql';
import { ProductsService } from './products.service';

@Resolver('Product')
export class ProductsResolvers {
  constructor(private productsService: ProductsService) {}

  @Query()
  async product(@Args('id') id: string) {
    const data = await this.productsService.findById(id);
    return data;
  }

  @Query()
  async products() {
    const res = await this.productsService.find();
    return res;
  }

  @ResolveReference()
  resolveReference(reference: { __typename: string; id: string }) {
    return this.productsService.findById(reference.id);
  }
}
// auto generated graphql.ts
export interface Product {
    id?: string;
    name?: string;
    displayName?: string;
    productId?: number;
    partnerId?: string;
}

export interface IQuery {
    product(id?: string): Product | Promise<Product>;
    products(): Product[] | Promise<Product[]>;
}
1

There are 1 best solutions below

0
On

In short, I had two Interceptors that changed the response and broke the GraphQL API.