简体   繁体   English

如何在nestjs的@ResolveField中访问@Args()

[英]How to access @Args() in @ResolveField in nestjs

I have a query for getting projects from the DB which has args as shown below for filtering projects.我有一个从数据库中获取项目的查询,该数据库具有如下所示的用于过滤项目的 args。 How would I use the same args in the @resolveField for filtering form values of the project我如何在@resolveField 中使用相同的参数来过滤项目的表单值

@Query(() => [Project])
async getProjects(@Args() projectArgs: ProjectArgs) {
  return await this.projectsService.find(projectArgs);
}

@ResolveField("formValues", () => [FormValues])
async getFormValues(@Parent() project: Project) {
  const { id } = project;
  return await this.formsService.findValues({ projectId: id});
}

I have faced this problem before and through thorough searching, I have come to the conclusion that the best and scalable way of doing this is to define the args for the field resolver itself.我之前遇到过这个问题,通过彻底的搜索,我得出的结论是,最好的和可扩展的方法是为字段解析器本身定义 args。 Here is how you do it这是你的做法

@ResolveField("formValues", () => [FormValues])
async getFormValues(@Args() projectArgs: ProjectArgs) {
  ..../////
}

This way you will have to pass the args just as you pass it in the parent query.这样,您将必须像在父查询中传递参数一样传递参数。 Or you could leave them out if you don't want to filter the form values或者,如果您不想过滤表单值,可以将它们排除在外

There are other approaches that you can use.您还可以使用其他方法。 One of them is by setting the info as其中之一是将信息设置为

@Query(() => [Project])
async getProjects(@Args() projectArgs: ProjectArgs, @Info() info) {
  info.variableValues.some_key = value
  return await this.projectsService.find(projectArgs);
}

But this won't scale and make it very tightly coupled但这不会扩展并使其非常紧密地耦合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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