繁体   English   中英

我正在尝试从解析器中的查询返回 static 数据

[英]I'm trying to return back a static data from a query in the resolver

我正在使用 nestjs 和 graphql 来实现它。 这是源服务内部的内容,我创建了一个对象数组以通过 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;
    }

这是同一来源的解析器内部:

@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);
    }
}

但是我不知道为什么它会一遍又一遍地给我同样的错误,尽管我之前在另一个资源中做过这个并且它有效。 这是错误文本:

 [{ "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

类型为'数字'。”,“源”:“ts”,“startLineNumber”:13,“startColumn”:6,“endLineNumber”:13,“endColumn”:44}]

而且我也明白了:

[{ "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 }]

请注意,我不想使用 model 或 dto,这就是我尝试这种方式的原因。

哦,我的坏。 唯一的问题是我从“@nestjs/common”导入了查询对象,而不是从“@nestjs/graphql”导入它。 那些愚蠢的错误是人们经历过的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM