You are here

How to Turn Your Data into an API with FaunaDB and GraphQL

Microservices are all the rage these days but accessing data from multiple microservices can be a drag. In this tutorial, you’ll learn how to use GraphQL to make fetching and manipulating data from multiple sources easy as pie. We’ll create a service that provides an API for a basic blog that will include posts, authors and comments. Chris Anderson over at the Fauna blog will show you how.

GraphQL is a query language for APIs. It can fetch data from multiple sources and return the collated data in a nice format. Chris shows you how to combine GraphQL with FaunaDB to create a single HTTP endpoint that will support a blog engine and all the requisite schema for posts, authors and comments. FaunaDB is a good fit for GraphQL because GraphQL queries multiple microservices in parallel and FaunaDB promises strong consistency.

Getting Started

To get started, you’ll need to install the Serverless Framework with NodeV4+. Run ‘npm install serverless -g’ in your terminal. You can get the example service code by cloning the relevant repo https://github.com/fauna/serverless-graphql-blog. Then cd into serverless-graphql-blog and install dependencies with the commands ‘npm install’, ‘cd blogs’, ‘npm install’.

Creating a Database on FaunaDB

Next, you need to create a database to hold the data returned by your GraphQL endpoint. First off, if you don’t have a FaunaDB, you’ll need to start a free trial. Then go to the dashboard and create a database. Enter the name graphql-blog-demo in the text box and click ‘Create Database’ again. Then you’ll need to get your db key. Click in the upper left corner of the screen ‘manage keys’ and then ‘create a key’. Name your key, assign it a server role and then choose the graphql-blog-demo database. Then Click ‘Create Key’ and your key’s secret will be displayed. You’ll need to copy this into your serverless.yaml file, replacing ‘SERVER_SECRET_FOR_YOUR_FAUNADB_DATABASE’.

Deploy Your GraphQL Endpoint

Now you need to deploy your GraphQL endpoint so you can query the FaunaDB database created above. For this, run ‘serverless deploy’. Then invoke the private endpoint to create the classes and indexes in the FaunaDB database. This will create the posts, authors, and comments classes. Simply run ‘serverless invoke --function setupFaunaDB’ in your terminal.

Sampling GraphQL Queries

You’re now ready to experiment with some GraphQL queries. First, create an author and some posts for that author. Go to the authors class in the FaunaDB dashboard at https://dashboard.fauna.com/db/graphql-blog-demo/classes/authors. Click ‘create instance’ and enter some JSON data. Then save the author instance. 

You can now create a blog post for this author. Use GraphiQL, a graph query visualisation tool where you can enter the endpoint returned by the ‘serverless deploy’ command. There you can enter a post query for your new author like this:

Now you have some data. You can query it. Try sample queries, like ‘{ authors { name }}’ to list author names or ‘{ posts { id, title } }’ to list post ids and titles. 

And you’re done!

Chris concludes the tutorial by emphasizing that combining FaunaDB with GraphQL is a good choice if you’re going for a microservices approach with your app. 
 

Original Article

Turn your data into an API with FaunaDB and GraphQL

Seamus Holland
 

Comments