You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apollo与type-graphql问题:查询解析器无法获取传入Input参数

GraphQL解析器无法获取Input参数,参数被EntityManager覆盖的问题排查

问题现象

执行GraphQL查询时已正常传入Input参数:

{ getAllPayments(input: {processed: true}) { id processed } }

但解析器中打印input时,输出的却是TypeORM的EntityManager实例,同时代码抛出ApolloError提示"no input passed"。

问题原因

TypeORM的@Transaction装饰器会自动将EntityManager作为第一个参数注入到被装饰的方法中,导致原方法中定义的input参数位置错位——原本的第一个参数input被注入的EntityManager覆盖,自然无法获取到查询传入的参数。

解决方案

在解析器方法的参数列表最前面,显式添加EntityManager参数(使用@TransactionManager()装饰器标记),确保参数顺序正确:

修改后的解析器代码:

@Transaction()
@Query(() => [Payment], { nullable: false })
async getAllPayments(
  @TransactionManager() manager: EntityManager, // 新增:注入的EntityManager放在第一个位置
  @Arg('input', { nullable: true }) input?: GetPaymentsInput
): Promise<Payment[]> {
  if (!input?.processed) {
    throw new ApolloError('no input passed');
  }

  // 若需要在事务中操作仓库,建议使用注入的manager获取仓库(保证事务一致性)
  // return manager.getCustomRepository(PaymentRepository).getAll({ ... });
  
  return getCustomRepository(PaymentRepository).getAll({
    processed: input?.processed,
    paymentProvider: input?.paymentProvider,
    paymentType: input.paymentType,
  });
}

说明

使用@Transaction装饰器时,TypeORM会强制注入EntityManager作为方法的第一个参数,无论你是否需要手动使用它。如果不调整参数顺序,后续所有参数的位置都会偏移,导致无法正确获取GraphQL传入的参数。调整后,input参数就能正常接收查询中传递的{processed: true}值,不会再抛出错误。

内容的提问来源于stack exchange,提问作者Brace Sproul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 15:05:24