cli-lambda-deploy icon indicating copy to clipboard operation
cli-lambda-deploy copied to clipboard

How to type(script) the event of a Serverless resolver sent from an Amplify GraphQL call

Open jdanielsyeah opened this issue 2 years ago • 0 comments

I am trying to type the event object coming from my AWS Amplify/NextJS front-end. It is sent via Amplify/GraphQL and being received in a NodeJS AWS Lambda. The typescript interface contained in import { AppSyncResolverEvent } from "aws-lambda"; - but this doesn't quite match the event object that's being received. Can someone please advise what I'm doing wrong?

Details:

I'm successfully firing an event from my front-end (NextJS) with aws-amplify and @aws-amplify/api-graphql packages:

import { graphqlOperation, GraphQLResult } from "@aws-amplify/api-graphql"; // "version": "2.3.11",
import { API } from "aws-amplify"; // "aws-amplify": "^4.3.28",

...
    // Retrieve schemas from Lambda
    const response = (await API.graphql<{ myAppSyncFunctionResponse: any }>(
      graphqlOperation(myAppSyncFunction),
      { input: "foo" }
    )) as GraphQLResult<{ myAppSyncFunctionResponse: string }>;

The event is being successfully received in my Serverless Lambda and resembles the following:

{
  typeName: "Mutation",
  fieldName: "myAppSyncFunction",
  arguments: { input: { ... } },
  identity: {...},
  source: ...,
  request: ...,
  prev:...
}

However, when I try to use aws-lambda in my nodejs Lambda environment:

import { AppSyncResolverEvent } from "aws-lambda";

export async function eventBridgeResolver(
  event: AppSyncResolverEvent
) {...}

AppSyncResolverEvent contains different properties:

// node_modules/@types/aws-lambda/trigger/appsync-resolver.d.ts
{
 arguments: TArguments;
    identity?: AppSyncIdentity;
    source: TSource;
    request: {
        headers: AppSyncResolverEventHeaders;
    };
    info: {
        selectionSetList: string[];
        selectionSetGraphQL: string;
        parentTypeName: string;
        fieldName: string;
        variables: { [key: string]: any };
    };
    prev: { result: { [key: string]: any } } | null;
    stash: { [key: string]: any };
}

jdanielsyeah avatar Oct 06 '22 13:10 jdanielsyeah