Valerie Tremblay
Less Glue Code with AppSync Javascript Resolvers
Reducing the amount of lines of code is not always synonymous with improvement!
We have all met developers who seek to prove their knowledge by writing complex functions in a single unreadable instruction!
Our goal should be to simplify our work and make it easy to maintain. If we can achieve the same functional results with clear and concise code, we are in a better position for the future.
This article is part of a series aimed at showing how to reduce non-value-added code using AWS capabilities. For the introduction, see The Cloud to Reduce Non-Value-Added Code.
AppSync
Appsync, the AWS service, allows you to connect your GraphQL API to different data sources. This connection can be established with a Lambda or with "resolvers" for a direct integration.
Until November 2022, writing resolvers required VTL (Apache Velocity Template), a little-known and disliked language due to its peculiar syntax. Fortunately, AWS recently announced the ability to use JavaScript to write this glue code, thus improving the developer experience.
Comparaison of Different Integration Types
For a simple API, the question is which option provides the best performance, lowest cost, and greatest clarity, with minimal code.
To find the answer to this question, you can check out the sources of this experiment on GitHub, here.
Lines of Code, Clarity, and Maintainability
The solution with VTL resolvers allows for minimizing the number of lines of code. However, the uncommon syntax of VTL can make it difficult to read and understand, which may reduce clarity and maintainability.
As for JavaScript resolvers, they are more accessible but require wrapping each function in a pipeline resolver, which is why they have more infrastructure code lines. Regarding this, the AppSync team mentions that “support for unit resolvers may be introduced at some point based on customer feedback.”
Performance and Costs
All three options offer similar performance. As shown in the performance comparison table, the differences are not significant.
Using Lambda with AppSync offers the flexibility to adjust the amount of allocated memory, which can reduce latency but potentially increases the cost. For example, for one million requests with 512 MB and an average time of 68.7 milliseconds, the cost of Lambda is $0.77.
AppSync offers a fixed cost of $4 for every million requests. This means that using Lambda and AppSync together automatically generates additional costs compared to direct integrations with VTL or JavaScript.
Conclusion
Direct integrations allow for reduced glue code. Additionally, the ability to use a popular programming language like JavaScript to write these integrations makes the solution more accessible and appealing to developers.
This addition to AWS's Serverless lineup allows you to focus on what really matters, like business logic, while positioning yourself for easier maintenance and evolution of your application in the future.