nestjs 中多个 graphql 解析器实现的问题 [英] Problem with multiple graphql resolver implementations in nestjs

查看:40
本文介绍了nestjs 中多个 graphql 解析器实现的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习 NestJS 和 GraphQL.我从 UserModule 中定义的单个解析器类 UserResolver 开始.此类提供读取用户列表或特定用户的方法.方法用 @Query() 修饰,提供了一个 user.graphql 文件,GraphQL 在 AppModule 中初始化,如文档中所述.一切正常,我可以通过 Insomnia Tool 或 Playground 获取用户或特定用户的列表.我很高兴!

I just started learning NestJS and GraphQL. I started with a single resolver class UserResolver defined in UserModule. This class provides methods to read a list of users or a specific user. Methods are decorated with @Query(), a user.graphqlfile is provided, GraphQL is initialized in AppModule, all as described in the docs. All works well, I can get a list of users or specific user through Insomnia Tool or through Playground. I am happy!

现在我创建了第二个模块,RoleModule.我创建了一个 role.graphql 文件和一个 RoleResolver 类,我基本上复制了为 User 所做的所有工作,但这次是为 Role.类型 Role 的 GraphQL 类型定义以及 role.graphql 文件中的 Query 定义被识别.无法识别的是我在 RoleResolver 类中的 Query() 实现,它们没有被调用.

Now I have created a second module, RoleModule. I created a role.graphql file and a RoleResolver class, I basically replicated all the work done for User but this time for Role. The GraphQL type definition for type Role as well as the Query definitions in the role.graphql file are recognized. What is not recognized are my Query() implementations in the RoleResolver class, they are not getting invoked.

如果我把所有这些 Role 相关的 @Query() 定义放到 UserResolver 类中,这些 Role现在正在调用相关查询.

If I put all these Role related @Query() definitions into the UserResolver class, these Role related queries are now getting invoked.

这是预期的行为吗?我是否需要将所有 GraphQL 查询定义放在一个类中?是否可以将 NestJS-GraphQL 解析器实现分布在多个模块上?难道我做错了什么?请帮忙.

Is that expected behavior? Do I need to put all my GraphQL query definitions into a single class? Is it possible to spread NestJS-GraphQL resolver implementations over several modules? Am I doing something wrong? Please help.

推荐答案

确保从 @nestjs/graphql 导入 Query> 来自 @nestjs/common.

Make sure that you import Query from @nestjs/graphql and not from @nestjs/common.

这篇关于nestjs 中多个 graphql 解析器实现的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆