0

I have a AppSync API that is linked to a lambda function as data source, the lambda function is calling another API to retrieve data from Database.

However after I setup the schema and resolver as well as the lambda function, it kept return me null result despite I have the actual result in my lambda function. (I printed it in logs so I can see it)

Here is my schema:

scalar AWSDateTime

type Event implements Node {
    appId: String!
    description: String
    eventName: String!
    id: String!
    nodeId: ID!
    ts: AWSDateTime!
}

input EventCondition {
    appId: String
    description: String
    eventName: String
    id: String
    ts: AWSDateTime
}

type EventsConnection {
    nodes: [Event!]!
    totalCount: Int!
}

enum EventsOrderBy {
    APP_ID_ASC
    APP_ID_DESC
    DESCRIPTION_ASC
    DESCRIPTION_DESC
    EVENT_NAME_ASC
    EVENT_NAME_DESC
    ID_ASC
    ID_DESC
    NATURAL
    PRIMARY_KEY_ASC
    PRIMARY_KEY_DESC
}

interface Node {
    nodeId: ID!
}

type Query implements Node {
    event(id: String!): Event
    eventByNodeId(nodeId: ID!): Event
    events(
        condition: EventCondition,
        first: Int,
        last: Int,
        offset: Int,
        orderBy: [EventsOrderBy!]
    ): EventsConnection
    node(nodeId: ID!): Node
    nodeId: ID!
    query: Query!
}

Here is my resolver request mapping for events, I hardcoded it so I can test:

{
    "version" : "2017-02-28",
    "operation": "Invoke",
    "payload": {
        "fieldName": "events",
        "parentTypeName": "query",
        "selectionSetGraphQL": "{\n nodes\n {\n  id\n  appId\n  eventName\n }\n}"
    }
}

Here is my response mapping template:

$util.toJson($context.result)

I expect it directly returns what Lambda Function return which is an object, but it is not. Here is what my lambda function have:

    const client = new GraphQLClient(eventsGraphQLURI, { headers: {} })

    client.request(request).then(
        (data) => {
            response = data;
            console.log(`Result: ${JSON.stringify(response)}`)
        })
        .catch((error) => {
            console.log(`Error Occurred: ${error}`)
    })
    
    return response;

When I check the logs, this is what the API return to my Lambda Function:

    {
        "events": {
            "nodes": [
                {
                    "id": "1e8601cf-a740-4146-a9b7-a38201a89eab",
                    "appId": "2fa39c1e-5b40-4a77-bab3-d22a79f051a7",
                    "eventName": "TemperatureDetection"
                },
                {
                    "id": "2645538",
                    "appId": "cc472578-2389-40ea-8525-a2b86fea23c4",
                    "eventName": "xxx"
                }
            ]
        }
    }

However when I run query on AppSync console, this is what it returns:

{
  "data": {
    "events": null
  }
}

Is there anything that I have done wrong so it will not recognized? When I go and check the logs of the AppSync, there is not any response.context log. All I have for response after the request in the log is as below image:

enter image description here

Deeply appreciates if anyone could help with this, I struggled for like two days on this. Thank you!

1 Answer 1

1

Okay totally overlook on my side. If anyone was wondering the same issue, check the lambda function, it needs to be async and also return the response in a callback.

Here are my new lambda function code that works:

exports.handler = async (event, context, callback) => {
    const client = new GraphQLClient(eventsGraphQLURI, { headers: {} })

    await client.request(request).then(
        (data) => {
            response = data;
            console.log(`Result: ${JSON.stringify(response)}`)
        })
        .catch((error) => {
            console.log(`Error Occurred: ${error}`)
    })
    
    callback(null, response);
}
Sign up to request clarification or add additional context in comments.

1 Comment

why must it be async?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.