问题描述
我刚刚开始学习NestJS和GraphQL.我从在UserModule
中定义的单个解析器类UserResolver
开始.此类提供了读取用户列表或特定用户列表的方法.方法用@Query()
装饰,提供了user.graphql
文件,在AppModule
中初始化了GraphQL,所有操作均在文档中进行了描述.一切正常,我可以通过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.graphql
file 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
文件中的查询定义.不能识别的是我在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解析器实现的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!