Test and Debug Resolvers - AWS AppSync

Test and Debug Resolvers

AWS AppSync executes resolvers on a GraphQL field against a data source. As described in Resolver Mapping Template Overview, resolvers communicate with data sources by using a templating language. This enables you to customize the behavior, and apply logic and conditions before and after communicating with the data source. For an introductory tutorial-style programming guide for writing resolvers, see the Resolver Mapping Template Programming Guide.

To help developers write, test, and debug these resolvers, the AWS AppSync console also provides tools to create a GraphQL request and response with mock data, down to the individual field resolver. Additionally, you can perform queries, mutations, and subscriptions in the AWS AppSync console, and see a detailed log stream from Amazon CloudWatch of the entire request. This includes results from a data source.

Testing with Mock Data

When a GraphQL resolver is invoked, it contains a context object that has relevant information about the request for you to program against. This includes arguments from a client, identity information, and data from the parent GraphQL field. It also has results from the data source, which can be used in the response template. For more information about this structure and the available helper utilities to use when programming, see the Resolver Mapping Template Context Reference.

When writing or editing a resolver function, you can pass a mock or test context object into the console editor. This enables you to see how both the request and the response templates evaluate without actually running against a data source. For example, you can pass a test firstname: Shaggy argument and see how it evaluates when using $ctx.args.firstname in your template code. You could also test the evaluation of any utility helpers such as $util.autoId() or util.time.nowISO8601().

Test a Resolver

In the AWS AppSync console, go to the Schema page, and choose an existing resolver on the right to edit it. Or, choose Attach to add a new resolver. At the top of the page, choose Select test context, choose Create new context, and then enter a name. Next, either select from an existing sample context object or populate the JSON manually, and then choose Save. To evaluate your resolver using this mocked context object, choose Run Test.

For example, suppose you have an app storing a GraphQL type of Dog that uses automatic ID generation for objects and stores them in Amazon DynamoDB. You also want to write some values from the arguments of a GraphQL mutation, and allow only specific users to see a response. The following shows what the schema might look like:

type Dog { breed: String color: String } type Mutation { addDog(firstname: String, age: Int): Dog }

When you add a resolver for the addDog mutation, you can populate a context object like the one following. The following has arguments from the client of name and age, and a username populated in the identity object:

{ "arguments" : { "firstname": "Shaggy", "age": 4 }, "source" : {}, "result" : { "breed" : "Miniature Schnauzer", "color" : "black_grey" }, "identity": { "sub" : "uuid", "issuer" : "https://cognito-idp.region.amazonaws.com/userPoolId", "username" : "Nadia", "claims" : { }, "sourceIP" : "x.x.x.x", "defaultAuthStrategy" : "ALLOW" } }

You can test this using the following request and response mapping templates:

Request Template

{ "version" : "2017-02-28", "operation" : "PutItem", "key" : { "id" : { "S" : "$util.autoId()" } }, "attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args) }

Response Template

#if ($context.identity.username == "Nadia") $util.toJson($ctx.result) #else $util.unauthorized() #end

The evaluated template has the data from your test context object and the generated value from $util.autoId(). Additionally, if you were to change the username to a value other than Nadia, the results won’t be returned because the authorization check would fail. For more information about fine grained access control, see Authorization Use Cases.

Debugging a Live Query

There’s no substitute for an end-to-end test and logging to debug a production application. AWS AppSync lets you log errors and full request details using Amazon CloudWatch. Additionally, you can use the AWS AppSync console to test GraphQL queries, mutations, and subscriptions and live stream log data for each request back into the query editor to debug in real time. For subscriptions, the logs display connection-time information.

To perform this, you need to have Amazon CloudWatch logs enabled in advance, as described in Monitoring and Logging. Next, in the AWS AppSync console, choose the Queries tab and then enter a valid GraphQL query. In the lower-right section, select the Logs check box to open the logs view. At the top of the page, choose the play arrow icon to run your GraphQL query. In a few moments, your full request and response logs for the operation are streamed to this section and you can view then in the console.