GraphQL on webtask.io endpoint

Serverless technology is enabling us to easy deploy code without doing any system administration. One of the companies providing a serverless service is webtask.io, as quoted from their website:

All you need is code,
Make serverless endpoints

For curiosity and learning purposes I wanted to get something running on a webtask.io endpoint so I decided to create a GraphQL server with Node.js

Enabling webtask.io on your development environment

The first step was to get the webtask.io enabled on my development environment for creating the GraphQL HTTP endpoint with Node.js.
the command line interface (CLI) seemed for me most suitable because it enables the possibility to run the code locally.

Follow these instructions if you want to install the webtask.io command line interface(cli) on your own development environment.

Setup GraphQL server with express

Second step was to enable the GraphQL server and have it tested locally.
Webtask provides several programming models out of the box and it would be possible to run GraphQL with the Simple function model But because it is a little more easy to get GraphQL running with express it was best to go for the Custom programming model utilizing the webtask-tools package provided by Auth0

Coming up with the following example code to create the GraphQL server with express.

Interesting detail here is the ‘use latest’ statement on line 1 that tells webtask to run the code on the latest version of Node.js enabling some features that we want without transpiling.

Before running this code on webtask.io we can test it locally with the webtask cli tools.

$ wt-cli serve graphql.js
Your webtask is now listening for IPv4 traffic on 127.0.0.1:8080

Then making a test request and see if all is running fine we could issue the following curl command

$ curl -X POST \
http://127.0.0.1:8080 \
-H 'content-type: application/json' \
-d '{"query" : "{ hello }"}'

The expected response would be

{"data":{"hello":"Hello world!"}}

Deploy to webtask.io

finally to deploy the code to webtask.io all we have todo is execute the following command

$ wt create --name graphql graphql.js
* Hint: A package.json file has been detected adjacent to your webtask. Ensuring that all dependencies from that file are available on the platform. This may takea few minutes for new versions of modules so please be patient.
* Hint: If you would like to opt-out from this behaviour, pass in the --ignore-package-json flag.
Resolving 5 modules...
Provisioning 5 modules...
body-parser@1.18.2 is available
express@4.16.2 is available
express-graphql@0.6.11 is available
graphql@0.11.7 is available
webtask-tools@3.2.0 is available
Webtask created
You can access your webtask at the following url:
https://wt-R4nd0mh4a5h-0.run.webtask.io/graphql

Then to test that all is working go to your favorite browser and copy paste the webtask.io link and the graphiql explorer should open and u can perform the hello query and should get some nice result as shown in the image below.

Finally lets test with making a request with curl from the command line

curl -X POST \
> https://wt-R4nd0mh4a5h-0.run.webtask.io-0.run.webtask.io/graphql \
> -H 'content-type: application/json' \
> -d '{"query" : "{ hello }"}'

The expected response would be the same as before

{"data":{"hello":"Hello world!"}}

Summary

With just a few lines of code we can have a GraphQL server running with the least amount of effort on a webtask.io endpoint.

Code

Repository with the full code base can be found here

Disclaimer

Found any technical error? please submit it to me and I will correct the page as soon as possible also note I am not a English native so any grammatical errors please notify me and I will correct them.

Connect

Like what you read? Give Boris Verhaaff a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.