简体   繁体   English

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

[英]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.

相关问题 我正在尝试从网站上删除数据并在正文中使用 JS function 取回基本的 HTML - I'm trying to scrap data from a website and getting back basic HTML with JS function in the body 我正在尝试从本书中运行Express静态中间件,但看不到该示例试图显示的内容 - I'm trying to run an Express static middleware from this book, and I cannot see what the example is trying to show 我正在尝试通过 id 查询从服务器获取数据,但显示的是一个空数组。 还有控制台日志没有显示任何错误? - I'm trying get data from server by id query but showing an empty array. there also console log not showing any error? 我试图将数据从客户端发布到mongodb - I'm stuck trying to post data from client to mongodb 我正在尝试返回套接字ID? - I'm trying to return socket id? 我正在尝试将从 API 收到的数据放入表中,但没有显示任何内容 - I'm trying to put the data I'm receiving from my API into a table but nothing is displayed 如何在graphql解析器中同时返回错误和数据? - How to return both error and data in a graphql resolver? 如何从graphql中的解析器返回自定义对象? - How to return custom object from resolver in graphql? 如何在需要return语句的GraphQL解析器中调用异步node.js函数? - How do I call an asynchronous node.js function from within a GraphQL resolver requiring a return statement? 我在什么时候返回该查询的数据? - At what point do I return the data from this query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM