簡體   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