GraphQL Api With Rails

Colin Walker
HackerNoon.com
2 min readOct 1, 2016

--

Graphql is an awesome new technology which is making it easier for client
side developers to iterate quickly and batch together network requests.

I really like the promise provided by GraphQl however it is not as easy
as rest to get up and running on immediately. This blog post takes a look
at how to build a working GraphQL backend in just a few steps using rails.

I am using my own project graphql-api as a framework for building out the GraphQL backend.

We will look at building a basic blogging application. A blog will have
an author and some tags.

First create the rails backend.

Add the necessary libraries to the Gemfile

Create our models

Add our has many relations to author class

Great! Now we have a basic setup for our blogging backend. Let's add some
controllers to serve basic Graphql queries.

Add the following file to app/controllers/graphql_controller.rb.

Add the following route to config/routes.rb

Now let's test out the new api with curl. Start up the server and run the
following command to see if we have any blog posts

Let's create an author

Now let's create a blog with that author's id

Let’s try that first query again, but this time let’s try and return the blog authors as well as the blog name.

You can also run updateBlog mutations as well as updateAuthor mutations. Try deleting an author or blog and see what responses you get back.

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising &sponsorship opportunities.

To learn more, read our about page, like/message us on Facebook, or simply, tweet/DM @HackerNoon.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--

Colin Walker
HackerNoon.com

Backend software developer. Loves working with Go, Kubernetes and some Ruby.