GraphQL Setup with an Rails 6 app

Bhagwan Singh
GAMMASTACK
Published in
3 min readJul 31, 2020

GraphQL is one of those new shiny things we’ve been seeing here, there and everywhere, It sounds like you should hurry up and start rewriting your application in GraphQL instead of REST as soon as possible, right? Not exactly.

Here I’m going to show you how you can use GraphQL API in Rails 6.

Let’s Start,

How GraphQL interact with rails

Hope you guys now little about GrahpQL and Rails Basics.

So first you need to install Bundler and Rails 6 in your machine
Here you should aware like in your system install Ruby with version 2.7 or greater with RVM

gem install bundler

gem install rails -v 6

Now you can check your Rails version with command

rails -v

output: Rails 6.0.3.2

Now please create an rails app using below command

rails new graphql-tutorial --skip-action-mailer --skip-action-mailbox --skip-action-text --skip-active-storage --skip-action-cable --skip-javascript --skip-system-test --skip-webpack-install

using cd command we go into the folder of the app created

cd graphql-tutorial

Now please create DB with the below command, here you should have install server of PostgreSQL or MySQL in you machine

bundle exec rails db:create

Greetings!!!!! you have successfully install rails..

Now start rails server

bundle exec rails server

#Open Gemfile in any Edit your are using like sublime Vs-code etc.
add the following dependency to it:

gem 'graphql', '1.9.17'

Here you need to run

bundle install

Now we will install GraphQL in our app using below command,

bundle exec rails generate graphql:install

#Open Gemfile and change the following line:

gem 'graphiql-rails', group: :development to

gem 'graphiql-rails', '1.7.0', group: :development

and again run,

bundle install

Now we have successfully setup GraphQL gem in the app,

Using below command we can generate an Model in rails app to query with GraphQL,

Generate Link model,

bundle exec rails generate model Link url:string description:text

And run migration,

bundle exec rails db:migrate

#Run rails console using rails c and then create a couple of dummy Links:

Link.create url: 'http://graphql.org/', description: 'The Best Query Language'

Link.create url: 'http://dev.apollodata.com/', description: 'Awesome GraphQL Client'

Enter exit to close console,

exit

Here we are going to create an GraphQL endpoint to query links,

rails g graphql:object LinkType id:ID! url:String! description:String!

Note: When you previously ran rails generate graphql:install, it created the root query type in app/graphql/types/query_type.rb for you.

module Types
class QueryType < BaseObject
# queries are just represented as fields
# `all_links` is automatically camelcased to `allLinks`
field :all_links, [LinkType], null: false
# this method is invoked, when `all_link` fields is being
# resolved
def all_links
Link.all
end
end
end

/app/assets/config/manifest.js

//= link graphiql/rails/application.css

//= link graphiql/rails/application.js

Now we have successfully integrate graphql with an rails 6 app.

you can query links using below commands,

visit

http://localhost:3000/graphiql

Here you will see an GraphQL user interface, in this you can test your queries like,

{
allLinks {
id
url
description
}
}

Helpful Links

https://graphql.org/code/

https://www.howtographql.com/graphql-ruby/1-getting-started/

Thanks!!,

I’ll get back to you soon with new article…..:)

--

--