[英]I'm trying to return back a static data from a query in the resolver
I'm using nestjs and graphql to implement this.我正在使用 nestjs 和 graphql 来实现它。 Here is whats inside the service of a source, I created an array of objects to retrieve them through the function getAllPromotions():
这是源服务内部的内容,我创建了一个对象数组以通过 function getAllPromotions() 检索它们:
@ObjectType("Promotions")
export class StaticPromotions{
@Field(() => String, { description: "Id of the promotion"})
id: string;
@Field(() => String, { description: "Image of the item"})
image: string;
@Field(() => String, { description: "Name of the item"})
itemName: string;
@Field(() => String, { description: "Name of the store"})
storeName: string;
@Field(() => String, { description: "The id of the store"})
storeID: string;
@Field(() => String, { description: "The offer of the promotion"})
offer: string;
};
@Injectable()
export class PromotionService {
private staticPromotions: StaticPromotions[] = [
{
id: 'dfdv',
image: 'assets/images/download.jpg',
itemName : 'Chicken Salad',
storeName : 'Sufi',
storeID : 'CCVV',
offer : 'Get 50% OFF'
},
{
id: 'dwewfdv',
image: 'assets/images/download.jpg',
itemName : 'Chicken Salad',
storeName : 'Sufi',
storeID : 'CCVV',
offer : 'Get 50% OFF'
},
]
async getAllPromotions(){
return this.staticPromotions;
}
And this is that inside the resolver of the same source:这是同一来源的解析器内部:
@Resolver('Promotions')
export class PromotionResolver {
constructor(
private promotionService: PromotionService
){};
@Query((returns) => [StaticPromotions])// the return type should be changed later into entity type
@UseGuards(UserAuthStrategy)
async allStoresForUser(
@Context('user') user: User,
): Promise<StaticPromotions[]> {
return this.promotionService.getAllPromotions(user.id);
}
}
But I don't know why it keeps giving me this same error over and over although I've done this before in another resource and it worked.但是我不知道为什么它会一遍又一遍地给我同样的错误,尽管我之前在另一个资源中做过这个并且它有效。 Here is the Error text:
这是错误文本:
[{ "resource": "whatever/promotion.resolver.ts", "owner": "typescript", "code": "2345", "severity": 8, "message": "Argument of type 'TypedPropertyDescriptor<(user: User) => Promise<StaticPromotions[]>>' is not assignable to parameter
of type 'number'.", "source": "ts", "startLineNumber": 13, "startColumn": 6, "endLineNumber": 13, "endColumn": 44 }]
类型为'数字'。”,“源”:“ts”,“startLineNumber”:13,“startColumn”:6,“endLineNumber”:13,“endColumn”:44}]
And also I get this too:而且我也明白了:
[{ "resource": "/whatever/promotion.resolver.ts", "owner": "typescript", "code": "2769", "severity": 8, "message": "No overload matches this call.\n Overload 1 of 3, '(...pipes: (PipeTransform<any, any> | Type<PipeTransform<any, any>>)[]): ParameterDecorator', gave the following error.\n Argument of type '(returns: any) => (typeof StaticPromotions)[]' is not assignable to parameter of type 'PipeTransform<any, any> | Type<PipeTransform<any, any>>'.\n Overload 2 of 3, '(property: string, ...pipes: (PipeTransform<any, any> | Type<PipeTransform<any, any>>)[]): ParameterDecorator', gave the following error.\n Argument of type '(returns: any) => (typeof StaticPromotions)[]' is not assignable to parameter of type 'string'.", "source": "ts", "startLineNumber": 13, "startColumn": 12, "endLineNumber": 13, "endColumn": 43 }]
[{ "resource": "/whatever/promotion.resolver.ts", "owner": "typescript", "code": "2769", "severity": 8, "message": "没有重载匹配这个调用。 \n 重载 1 of 3,'(...pipes: (PipeTransform<any, any> | Type<PipeTransform<any, any>>)[]): ParameterDecorator',出现以下错误。\n 类型的参数 ' (returns: any) => (typeof StaticPromotions)[]' 不可分配给 'PipeTransform<any, any> | Type<PipeTransform<any, any>>' 类型的参数。\n 重载 2 of 3, '(property : string, ...pipes: (PipeTransform<any, any> | Type<PipeTransform<any, any>>)[]): ParameterDecorator', 给出了以下错误。\n 类型的参数 '(returns: any) = > (typeof StaticPromotions)[]' 不可分配给 'string' 类型的参数。", "source": "ts", "startLineNumber": 13, "startColumn": 12, "endLineNumber": 13, "endColumn" : 43 }]
Please note that I don't want to use a model or a dto that's why I'm trying this way.请注意,我不想使用 model 或 dto,这就是我尝试这种方式的原因。
Oh, my bad.哦,我的坏。 The only problem was that I imported the Query obj from '@nestjs/common' instead of importing it from '@nestjs/graphql'.
唯一的问题是我从“@nestjs/common”导入了查询对象,而不是从“@nestjs/graphql”导入它。 Those silly mistakes are what make people experienced.
那些愚蠢的错误是人们经历过的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.