AWS AppSync : A serverless solution for building GraphQL APIs

S

Shashank Rajak

Mar 3, 2023

4 min read

cover image

AppSync is a serverless solution offered by Amazon Web Services (AWS) Cloud for building GraphQL APIs. Why it is a serverless solution? So traditionally, if one has to develop GraphQL APIs from scratch, one will have to do a lot of manual labour to set up the backend server and configure it to handle GraphQL requests/responses. This is where AppSync comes into the picture where we just focus on our GraphQL schema and resolvers logic, and the rest of the heavy lifting is taken care of by AppSync. Technically, we developers don't have to manage the backend server or configurations to build GraphQL APIs, hence it's a serverless solution. This does not mean there is no server, there is indeed a backend server but that is managed by AWS for us.

How does AppSync work in a nutshell?

AWS AppSync

AppSync provides a single GraphQL endpoint that can be used by different client apps like mobile, web, smart IoT devices etc. Behind the scene, AppSync can interact with several data sources like databases or HTTP endpoints to power the GraphQL API. In nutshell, it's like a gateway for frontends that can connect with several backend data sources and get things done.

A simple use case of this solution can be to build APIs with a backend designed on microservices architecture, where we have multiple backend services and a single platform can interact with all of these services and the frontend gets a single endpoint to interact with the backend. Hence, the abstraction of backend complexity is very well managed by AppSync and we just have to focus on building the APIs rather than thinking about all these connections and managing the servers.

AppSync has a few basic components which are essential to learning before developing GraphQL APIs on it -

  1. Schema: Any GraphQL API contains a Schema where we define the capabilities of this API. The Schema usually has queries, mutations, subscriptions and other user-defined types. This is the first step while building GraphQL APIs, where we define all the capabilities of our API in the schema.

    AppSync takes this Schema to next level by allowing AWS custom types and derivatives which make the API development much faster and hassle-free. But these features are not generic to GraphQL and are only available on AppSync. For example, we can use something like AWSDateTime as one of the data types in the schema to represent timestamps but this is limited to AppSync only.

  2. Data Source: This is a very AppSync-specific component. AppSync supports different types of Data Sources like Lambdas, OpenSearch, RDS, HTTP endpoints and DynamoDB. The AppSync API can interact with these different data sources to resolve the requested data. This is one of the most powerful tools available with AppSync where we can interact with multiple data sources from one platform and build a robust API.

  3. Resolvers: For any query or mutation, ultimately some data source has to respond with requested data. As per the name, Resolver's primary role is to resolve the request coming from a query or mutation.
    In AppSync, we have to attach a resolver for every query/mutation. Each of these resolvers will have two parts - a request mapping template and a response mapping template. In the request mapping template, we define the business logic to be executed on every request to the resolver against a data source and once that data is resolved, the response mapping template is invoked which simply translates the response of that operation back to the AppSync schema.

There are a few significant features available on AppSync which make it stand out -

  1. It supports Pub/Sub APIs and WebSocket connection out of the box where we can build real-time applications. For example, it's very easy to build a real-time dashboard app that can show temperate updates from a remotely placed IoT device.

  2. It also supports features for building offline first applications but to utilise this feature we have to use AWS Amplify which is another tech solution by AWS to build full-stack applications.

Overall, AppSync is a great solution for building GraphQL APIs where a lot of backend complexity is taken care of by AWS and developers can easily focus on building APIs. There are few limitations and drawbacks as well which we can talk about in a separate blog.

You can explore more about AppSync here on the official AWS page - https://aws.amazon.com/pm/appsync/

Have thoughts or questions about this article? Feel free to connect with me on LinkedIn or send me an email!