Setup Graphql in your brand new Ruby on Rails application
Graphql - A Query language for your API
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data.
It's your new alternative to REST APIs which is trending now!
Integrating Graphql in our Rails application is pretty straight forward!
Let's go through that process in this post.
graphql gem is a ruby implementation of GraphQL.
First, Add graphql to your Gemfile.
$ bundle install
For installing the generators for graphql, run following command.
rails generate graphql:install
This installation sets up the basic architecture for graphql. Now your screen should look like below one:
This command creates the graphql_controller.rb under the app/controllers folder.
As per the standards, there is an endpoint defined for graphql is /graphql which points to execute method of your Graphql controller.
So all the APIs we will generate, call the graphql#execute method first. There is also a graphql folder created under the app folder. This defines the structure for implementing GraphQL APIs. In detail we'll look in my upcoming blog posts.
Now our auto updated routes file looks like following image:
Also, add following snippet to precompile the graphiql assets (JS and CSS files)
//= link graphiql/rails/application.css //= link graphiql/rails/application.js
Now, hit localhost:3000/graphiql.
Voila!! Your basic setup of GraphQL in Rails application is done!
GraphQL provides two types of APIs:
We will look into this in more detail in my upcoming blog post. Keep following!
Thanks for reading! Happy coding!