Welcome to another year of the Mojolicious Advent Calendar! 2018 has been very good to Mojolicious and I could think of no better way to kick off this calendar than with a recap of the 2018 Nordic Perl Workshop and MojoConf held in Oslo, Norway.
How to easily add a GraphQL interface to the public REST API of your Mojolicious application
During the Mojolicious 2017 Advent calendar series, we looked at how to build a public REST API using Mojolicious. A technology that is getting a lot of buzz is GraphQL. Now that it has been ported to Perl 5, what if there were an easy way to let people access your API using it?
Over the course of this advent calendar, we have discussed the Wishlist example application several times. We used it to motivate discussions about templates, models, installable apps, and testing. In this post I want to use it somewhat differently. I would like to motivate you, the reader, into action in a few different ways.
Perl is well-known for its one-liners: short programs written as part of the command line invocation of the interpreter. Certainly every programmer or sysadmin has the need, from time to time, to do a quick one-off task programmatically. Such tasks can be done with a full script, to be sure, but once you get the hang of writing them, one-liners can save the time and hassle of actually doing so.
These tasks may include removing unwanted lines from files, collecting data from logs, or even a quick proof-of-concept of something that would become a script later. They can read lines in files, even multiple files, can operate on files in-place, can read from STDIN as a pipe. But while one-liners have been tools of the trade for these activities, certainly no such thing would be practical for web tasks, right?
But of course, on day 5 and day 6 of this series that we saw that we can build command line tools with your app. We have even seen how to use the eval command to run a one-liner against your app. So could we take this further?
Could we do remote data fetching and manipulation as a one-liner? Could we build an entire web application as a one-liner? Would I be asking if the answer was no?
The OpenAPI Specification (formerly named Swagger) is an API description format for REST APIs. An API specification written using the the rules set by the Open API Initiative can be used to describe:
- All the available endpoints. An endpoint is a unique resource that can access or modify a given object.
- Input parameters, such as headers, query parameters and/or body parameters.
- The structure of the response, including headers status codes and the body - if any.
- Authentication methods