Better IoT with GraphQL and AppSync

Sarjeel Yusuf
10 min readJun 14, 2019

--

“If you think that the Internet has changed your life, think again. The Internet of Things is about to change it all over again!” These were the words uttered by Brenden O’Brien, the Chief Architect and Co-founder of Aria Systems. Was he right though? Well, when it comes to assessing the potential of IoT, Mr. O’Brien was definitely right. IoT is changing the world, whether it be consumer products or how governments are tackling global issues, but has it fulfilled the expectations of Mr. O’Brien.

However, with all the benefits and motivation for IoT, the adoption of the concept isn’t following the trends that were initially expected as it still has to mature and is currently riddled with issues. We are seeing solutions to these problems blossoming in the form of new developments in other domains of technology and software. One such development is GraphQL which can ease IoT development with its efficient querying mechanism, static schemas and self-documenting APIs. Further benefits are reaped when AppSync is used as the supporting GraphQL service due to features such as it’s offline support and code generation, all allowing you to focus specifically on the IoT side of IoT. Hence the purpose of this article is to highlight how using GraphQL to service your IoT applications can greatly improve the entire development experience and push for greater adoption.

Understanding the Basics

Before we delve into how GraphQL and AppSync can further the development of IoTs, we must first define the problems in the IoT domain and understand what AppSync along with GrapQL is.

Fundamental Challenges of IoT

Researchers at Queen’s University Belfast published a paper in the 10th International Conference on Frontiers of Information Technology, titled “Future Internet: The Internet of Things Architecture, Possible Applications and Key Challenges”. The paper highlighted the issues hampering IoT adoption and major issues could be identified as listed below:

  • Lack of standardization — Any IoT environment could be expected to have multiple types of IoT devices with varying communication methods and data needs. Implementing such a backend infrastructure could be cumbersome and susceptible to failure due to changes in how data would be serviced.
  • Lack of supporting Infrastructure — Do target communities have the basic infrastructure to ensure the success of IoT adoption.
  • Security and privacy — IoT could witness the handling of immensely sensitive data and hence ensuring the privacy and security of such data is imperative.

Even though the list of challenges facing the IoT domain is not an exhaustive one, the three mentioned above are the main technological pains in adopting IoT. Great personalities such as Rob van Kranenburg, the founder of Council_IoT, have further echoed these concerns with IoT along with socio-economic problems, making it difficult to implement IoT solutions. Nevertheless, by being able to solve these technological challenges at least, the practical possibility of IoT solutions becomes apparent. This is where GraphQL and AppSync can step in.

GraphQL and AppSync 101

Getting right to it, GraphQL is a syntax that defines how data should be retrieved. It gives form to our data. On the other hand, AppSync is the actual GraphQL tool, the service which can be used to implement GraphQL solutions. Therefore, GraphQL is basically a syntax that allows you to define what data you exactly need. It has three major components which include:

  • Schemas, which are the definitions of all operations
  • Data Sources, from where the data is queried
  • Resolvers, which act as connecting blocks between the schema operations and data sources.

It is not necessary to use AppSync to implement GraphQL solutions, as there are other GraphQL services such as Apollo and Relay, but for the sake of improving IoT development AppSync is the right way to go. The reasons justifying such a statement follow throughout the rest of this piece.

Benefits of GraphQL

Irrespective of what tool you choose to implement your GraphQL solution, the established advantage that GraphQL alone brings to your IoT architecture is a client-friendly data management system in the backend. This is because GraphQL allows you to produce APIs where the data retrieved depends entirely on the client request, absent of issues such as overfetching. Other features include caching and the need for only a single endpoint.

All of this is possible due to how GraphQL visualizes data which is in the form of Application Data Graphs. This involves handling data in the way it is actually viewed by us, an interconnected web of related entities, where the single endpoint offered by GraphQL simply redirects the system to the root of the graph.

Application Data Graph

Understanding the underlying mechanics of GraphQL, it can be said the guys over at Facebook have produced a great piece of technology backed by solid computer science. This, in turn, leads to great potential for the IoT domain.

Standardization of IoT Data

Due to the fact that with GraphQL schemas clients can get the exact data that they requested, IoT devices can be added to the network without worrying about new end-points and enhancements to data retrieval and processing.

Going with the example illustrated in the Application Data graph above, let’s say the Luangwa National Park in Zambia decides to use IoT systems to monitor the wildlife. One IoT device may only need to retrieve the animal’s species-related data that is stored in one table. Another IoT device is added to the network which needs the information of the reserve along with information of animals in the reserve. Instead of having to make an entirely new table and endpoint to capture the required reserve data along with the animal data, defining the GraphQL schema alone could ensure that no change to the data management system in the backend is required.

IoT developers could simply connect the device to the network and expect to retrieve the required data with the necessary query calls. Hence there is no need for additional work.

GraphQL Schema

Faster and Easier Development

As mentioned, you could potentially access all the data you need, with only one endpoint. Moreover, all the data as defined by the GraphQL’s schema is returned with just one call even if the required data is stored in different tables. That means that procuring more information means just adding more data fields in the query, and not having to edit how data is fetched in the backend. Literally, this could mean that a well planned GraphQL schema could be created once and never have to be touched again allowing developers to focus primarily on IoT devices.

Moreover, changes in the data schemas of GraphQL will not break the GraphQL based API that your IoT devices are using, provided that you are not removing fields that the IoT devices are querying. Therefore, you no longer have to worry about potential issues in data services and API documentation. The performance of your data layer is resistant to changes in the data structure.

Query And Return Data

Added Benefits with AppSync

Considering that GraphQL is simply a data querying and manipulation language, you still need a service to implement the novel concept. Around the beginning of this piece, I made the claim that AppSync is the perfect development service to implement the GraphQL based data services for your IoT applications. This is because of the various features that AppSync adds to the already powerful GraphQL features such as batch-processing, offline-support and much more.

Overall, AWS has managed to make GraphQL development incredibly easy and accessible. AppSync has proven effective as a development tool due to its powerful functionality and simple set-up, the two qualities of every developer’s dream tool. All this means that the general issues plaguing IoT can be overcome and IoT development as a whole becomes much more simpler and directed towards the device itself.

No Problem With Choppy Connection

AppSync Overview

Network connectivity is a major issue impeding IoT adoption. Several countries and regions lack the infrastructure required to maintain constant connectivity. That means IoT devices cannot always be connected to their compute and data service backends. However, AppSync brings with it offline support, a feature that mitigates the pains of inconsistent network connectivity.

Going back to our example in the plains of the Zambian Savannah, if one of the devices has lost internet connection its data is saved locally. Once the device reconnects, all data manipulations made within the device are automatically relayed back to the data store via AppSync. This allows improved performance for IoT devices in regions with poor infrastructure, aiding the adoption of IoT.

Batch-Processing IoT Data

Considering the fact that IoT supporting infrastructure is a huge barrier for IoT adoption, continuously connecting to your data store can be a burden on the IoT network. AppSync along with underlying GraphQL technology provides an answer to the problem with batch processing. The feature thus allows you to send or retrieve several entities of data with a single request, reducing the traffic on the IoT network.

You can achieve batch processing via AppSync by creating GraphQL resolvers in the AppSync console. Resolver templates are already available and you can use them as is or build on top of them. Thus reducing the programming burden.

Batch Resolver

The end result achieved is the reduction to the infrastructure required and hence greater motivation to adopt IoT services.

Codegen for the Lazy Developer

AppSync has an extensive automatic codegen support and developer friendly Query console. Additional features include connecting data sources via the AppSync console itself and templates for resolvers.

Therefore, with AppSync you can set up the entire GraphQL database service for your IoT architecture, without writing a single line of code. That is AMAZING. In fact, the feature is so popular that most resources regarding AppSync that you find scattered across the internet demonstrate how to set up GraphQL APIs using AppSync’s automatic codegen functionalities, and resolver templates. This is an immensely helpful feature since developers would have to learn no, or very little, about the setup of GraphQL APIs which is a relatively novel technology in the industry requiring sincere learning.

As a result, with the amazing codegen support, developers can focus more on their IoT devices. Setup of backend data services can now be abstracted away to the AppSync console.

Connecting Data Sources via AppSync Console

The Serverless Advantage

With AppSync, you can make your GraphQL serverless by configuring AWS Lambda data sources via the AppSync console. That means your IoT devices can now gain the full benefits of the leading FaaS service out there in the Serverless domain.

Serverless is a perfect platform for IoT devices, especially those that are not used frequently. This is because of the serverless postulates that AWS Lambda manifests. These include:

  • Abstraction of server responsibility.
  • Scalability
  • Pay-as-you-go model

The three properties of a serverless architecture would greatly benefit IoT adoption. IoT developers now would no longer need to worry about setting up and maintaining their own servers within their architecture. This job can now be abstracted to the AWS vendor.

Additionally, IoT users will no longer have to worry about scalability. Connecting Lambda functions between the GraphQL layer and device means that the application can scale according to the needs within the architecture. So for example, if there is a sudden spike in the IoT devices accessing the data layer, then Lambda functions can allocate the necessary resources to ensure no hindrance in performance.

Furthermore, one of the most important benefits of serverless is the pricing model, where you pay for only those resources you consume at a specific time. That means if one IoT device is not being used, it will not be billed just because it is still available. The transactions of your IoT devices to your GraphQL data store via Lambda functions are billed only upon each invocation. Thıs can potentıally reduce costs significantly.

AWS Lambda functions dominate the FaaS services within the serverless domain. By AppSync connecting to Lambda functions, IoT infrastructures can benefit greatly in terms of cost and ease of operation.

This ease of operation is further aided by the SaaS ecosystem surrounding Lambda functions. For example, Thundra.io, a monitoring tool, provides extensive monitoring and observability to Lambda functions and therefore to the transactions of your IoT devices.

Therefore, in conclusion, GraphQL via AppSync is the best way to go when developing your backend data service in your IoT architecture. This is because GraphQL allows scalability and flexibility of the IoT architecture through its client-specific data retrieval properties. AppSync furthers the benefits of using GraphQL by providing features such as batch processing and offline support. Its connection with AWS Lambda is a major advantage, allowing you to reap the benefits of a serverless IoT architecture. Hence even though there are other development services for GraphQL, the overwhelming benefits that AppSync provides justifies my call for its usage. Eventually, the end goal is achieved in mitigating the problems of IoT and advocating for its adoption.

--

--

Sarjeel Yusuf

An engineer turned product manager passionate about cloud computing and everything DevOps.Product Manager @Atlassian building DevOps capabilities.