AWS AppSync – HTTP Resolver

AWS AppSync is an enterprise level, fully managed GraphQL service with real-time data synchronization and offline programming features.
AWS AppSync automatically updates the data in web and mobile applications in real time, and updates data for offline users as soon as they reconnect. AWS AppSync makes it easy to build collaborative mobile and web applications that deliver responsive, collaborative user experiences.

AWS AppSync enables you to use supported data sources (AWS Lambda, Amazon DynamoDB, or Amazon Elasticsearch Service) to perform various operations, in addition to any arbitrary HTTP endpoints to resolve GraphQL fields.

Here you can read more about AWS AppSync:

AppSync now supports HTTP endpoints as data sources: HTTP resolver.
It enables customers to use their existing backend services that use REST APIs with AppSync to leverage the power of GraphQL interfaces.

If you are not familiar with GraphQL, I suggest you these resources:

In this post we are going to see how to create a new AppSync API, a new HTTP data source, a new HTTP resolver and how to run a simple GraphQL statement to query our backend Rest API service.

To run the example I used Python 3.6.3. You need the Python AWS SDK Boto3.

If you have an older version of Boto3, please update it (you need at least version boto3-1.7.59).

Given our REST Api endpoint we are going to build an API to leverage the power of GraphQL interfaces.
We are going to use this dummy Rest API – Json Placeholder:

Request:

Response:

These GraphQL Types describe our data:

Let’s see how to create a new AppSync API.
Fist of all, create a new AWS AppSync client and then create a new API:

When you create a new API you need to specify the API name and the API authentication type. In the example I used the API_KEY authentication type. Here you can read more about authentication types: AWS AppSync Security .

Create a new API key:

Create new GraphQL types:

Data sources and resolvers are how AWS AppSync translates GraphQL requests and fetches information from your AWS resources.
AWS AppSync has support for automatic provisioning and connections with certain data source types. You can use a GraphQL API with your existing AWS resources or build data sources and resolvers. This section takes you through this process in a series of tutorials for better understanding how the details work and tuning options.

Create a new HTTP data source based on our API:

The next step is to create the HTTP Resolver.
A resolver uses a request mapping template to convert a GraphQL expression into a format that a data source can understand. Mapping templates are written in Apache Velocity Template Language (VTL).

This is how our request mapping template looks like.

This is how our response mapping template looks like.

Create the new resolver:

And we are done! We created an API that use REST APIs with AWS AppSync to leverage the power of GraphQL interfaces.


cURL request:

Response:

Leave a Reply

Your email address will not be published. Required fields are marked *