0
votes

In AWS AppSync, I have a Lambda resolver. The resolver handles multiple queries e.g. getUser, getUsers, getClient, etc. How can the Lambda function get the query return type defined in the AppSync GraphQL schema?

The return types are defined in the AppSync GraphQL schema query section:

type Query {
    getClient(_id: ID!): Client
    getUser(_id: ID!): User
    getUsers(): UserConnection
}

The lambda event variables contain the following for the query getUser:

"info": {
    "parentTypeName": "Query",
    "selectionSetList": [
        "_id",
        "email"
    ],
    "selectionSetGraphQL": "{\n  _id\n  email\n}",
    "fieldName": "getUser",
    "variables": {
        "id": "5c42109b2eb8ed82c8862532"
    }
},
"stash": {}

Using the AWS SDK I can call the AppSync API method getIntrospectionSchema but it does not return any queries or mutations -- only an array of all types.

I can also call the getResolver method but it does not return the field return type.

var appsync = new AWS.AppSync();
var params = {
    apiId: 'xxxxx',
    fieldName: 'getUser',
    typeName: 'Query',
};
const data = await appsync.getResolver(params).promise();

Response:
dataSourceName:'myLambdaDatasource'
fieldName:'getUser'
kind:'UNIT'
requestMappingTemplate:null
resolverArn:'arn:.../types/Query/resolvers/getUser'
responseMappingTemplate:null
typeName:'Query'
1

1 Answers

0
votes

As a workaround (and because I'm using the AWS CDK to create the AppSync API) I can inject variables into the datasource Lambda function via a pipeline resolver request mapping template. It's not ideal because we need to mimic the variables sent to a direct Lambda resolver (which could change). For now, this VTL works: $util.toJson($ctx)

api.addQuery(`get${objectTypeName}`, new ResolvableField({
    returnType: objectType.attribute(),
    args: { id: appsync.GraphqlType.id({ isRequired: true }) },
    dataSource,
    pipelineConfig: [],
    requestMappingTemplate: appsync.MappingTemplate.fromString(`
        $util.qr($ctx.stash.put("myVariable", "myValue"))
        {
            "version" : "2017-02-28",
            "operation": "Invoke",
            "payload": $util.toJson($ctx)
        }
    `)
}));

Variables are available in the Lambda event parameter i.e. event.stash.myVariable

"stash": {
    "myVariable": "myValue"
}