Menu
AWS AppSync
AWS AppSync Developer Guide

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. You can find an introductory tutorial-style programming guide for writing resolvers in the Resolver Mapping Template Programming Guide.

To help developers write, test, and debug these resolvers, the AWS AppSync console also provides tools to "mock" a GraphQL request and response, 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. Find more details about this structure and the available helper utilities to use when programming in 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 and 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, open a resolver by navigating to the Schema page, and selecting an existing resolver on the right to edit. Or click Attach to add a new resolver. At the top of the page, choose Select test context, and then choose Create new context. Provide a memorable name. Next, either select from an existing sample context object or populate the JSON manually, then press Save. You can then press the Test button to evaluate your resolver using this mocked context object.

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 a response to be seen only for a certain user. Here's 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 below. This has arguments from the client of name and age, as well as 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 will have 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 would not be returned because the authorization check would fail. Read more about fine-grained authorization in 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 displayed logs are the connection-time information.

To perform this, you need to have Amazon CloudWatch logs enabled in advance, as described in Monitoring and Logging. Next, navigate to the Queries tab of the AWS AppSync console, and type in a valid GraphQL query. In the lower-right section, you'll see a Logs check box, which you can select to drag the view up. Using the "play" Arrow at the top of the page, run your GraphQL query. In a few moments, your full request and response logs for the operation will be streamed to this section of the console for you to view.