简体   繁体   中英

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

I'm using nestjs and graphql to implement this. Here is whats inside the service of a source, I created an array of objects to retrieve them through the 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 }]

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 }]

Please note that I don't want to use a model or a dto that's why I'm trying this way.

Oh, my bad. The only problem was that I imported the Query obj from '@nestjs/common' instead of importing it from '@nestjs/graphql'. Those silly mistakes are what make people experienced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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