AppSyncJS解析器info对象selectionSetList未定义,字段却存在于外部
问题原因与解决方案
问题根源
AppSync在将解析器中的ctx.info序列化传递给Lambda时,默认不会包含selectionSetList和selectionSetGraphQL字段——这两个属于解析器运行时的内部计算属性,不会被自动序列化到info对象的传输 payload 中,直接传递整个ctx.info时,这俩字段会被丢弃,导致Lambda中event.info里看不到它们。
解决方案
方案1:手动组装完整的info对象传递
修改解析器的request函数,把需要的selectionSetList和selectionSetGraphQL合并到info对象中,而非单独传递:
export function request(ctx) { return { operation: 'Invoke', payload: { arguments: ctx.arguments, source: ctx.source, request: ctx.request, // 手动组装包含完整字段的info对象 info: { ...ctx.info, selectionSetList: ctx.info.selectionSetList, selectionSetGraphQL: ctx.info.selectionSetGraphQL }, prev: ctx.prev, stash: ctx.stash }, }; }
这样Lambda中event.info就能包含所有需要的字段,与TypeScript的AppSyncResolverEvent.info类型声明匹配。
方案2:调整TypeScript类型匹配实际传递结构(若保留单独传字段的写法)
如果想继续保留单独传递infoselectionSetList和selectionSetGraphQL的写法,需要自定义Lambda的event类型,避免类型不匹配:
import { AppSyncResolverEvent } from "aws-lambda"; // 自定义扩展后的Event类型 type ExtendedAppSyncResolverEvent<T = any, U = any> = AppSyncResolverEvent<T, U> & { infoselectionSetList: string[], selectionSetGraphQL: string }; export async function handler(event: ExtendedAppSyncResolverEvent<any, any>) { // 可正常访问event.infoselectionSetList和event.selectionSetGraphQL console.log(event.infoselectionSetList); }
额外说明
虽然AppSyncResolverEvent.info的类型声明里包含了selectionSetList和selectionSetGraphQL,但这是因为这些字段在AppSync解析器运行时存在,序列化传输到Lambda时会被过滤,必须手动显式传递这两个字段,才能在Lambda中获取到。
内容的提问来源于stack exchange,提问作者Davide Fazio
相关产品推荐
相关产品推荐

