我正在使用 NestJs、GraphQL apollo federation(模式优先方法)和 Mongoose。
在查询product
orproducts
解析器时,查询返回一个所有属性都为空的对象。
但是,当我在我的服务/解析器中调试和使用断点时,我可以看到这些函数正在返回正确的 mongo 文档。
文件代码片段:
{
"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[]>;
}