What response format does AppSync expect?

695 Views Asked by At

I am new in the AWS and need advice. I have a GraphQL schema described in AppSync. The scheme is simple, it has only one Query called getEmployees. As a Resolver to this query, I use the AWS Lambda function in Python. This AWS Lambda function works without any problem. There are no errors in the logs. When I make a request, I always get empty nulls. What could be the problem? What did I miss?

GraphQL Schema in AppSync:

type Employees {
    employeeId: String!
    employeeFirstName: String
    employeeLastName: String
}

type EmployeesPayload {
    pagingState: String
    employees: [Employees]
}

type Query {
    getEmployees(organizationId: String!, fetchSize: Int!, pagingState: String): EmployeesPayload
}

schema {
    query: Query
}

The AWS Lambda function in Python:

def lambda_handler(event, context):
    organization_id = event['arguments']['organizationId']
    fetch_size = event['arguments']['fetchSize']
    paging_state = event['arguments']['pagingState']

    # Business logic

    response = json.dumps(
        {
            "pagingState": "c76f05f9-337d-44dc-b131-c1dfee3ceefb",
            "employees": [
                {
                    "employeeId": "bd811630-0d42-49cd-8d63-7679d9eb96bb",
                    "employeeFirstName": "James",
                    "employeeLastName": "Bond"
                }
            ]
        },
        indent=4,
        sort_keys=True,
        ensure_ascii=False,
        default=utils.json_serializer
    )

    return response

I make such a query:

query MyQuery {
  getEmployees(organizationId: "5d354323-8b47-447e-8bc7-67dae57248b0", fetchSize: 5, pagingState: null) {
    pagingState
    employees {
      employeeId
      employeeFirstName
      employeeLastName
    }
  }
}

Response:

{
  "data": {
    "getEmployees": {
      "pagingState": null,
      "employees": null
    }
  }
}

P.S. In the AppSync setting, I disabled the request and response mapping template.

1

There are 1 best solutions below

0
On BEST ANSWER

Well, finally I found the answer. With the help of trial and error, I realized that AWS Lambda functions do not need to serialize data in JSON. Sterilization is handled by AppSync itself.

def lambda_handler(event, context):
    organization_id = event['arguments']['organizationId']
    fetch_size = event['arguments']['fetchSize']
    paging_state = event['arguments']['pagingState']

    # Business logic

    response = {
        "pagingState": "c76f05f9-337d-44dc-b131-c1dfee3ceefb",
        "employees": [
            {
                "employeeId": "bd811630-0d42-49cd-8d63-7679d9eb96bb",
                "employeeFirstName": "James",
                "employeeLastName": "Bond"
            }
        ]
    }

    return response