How To Set Up a Ruby on Rails GraphQL API

Total Blog Views: 44

Blog Status: publish

Created By: charlie Created at: 08-07-2021

Tags: ruby on rails graphql api restapis

Hi there!
Let’s integrate GraphQL in your Rails application and create your first
GraphQL query.
Must have installed in your system:

  • Ruby
  • Rails
  • PostgreSQL

STEP 1: Creating new Rails Application: 

  • To create a new API only Rails application with PostgreSQL
    database run:

rails new rails_graphql_demo -d postgresql -T --api

  • Once application is created, goto :

cd rails_graphql_demo

  • Run below command to create database :

rails db:create

Output
Created database 'rails_graphql_development'
Created database 'rails_graphql_test'

First step is completed, let’s verify if you did it correctly: 

Start the Rails application: rails s
Visit: http://localhost:3000

Note:This file should have auto generated sample query code:

module Types
   class QueryType < Types::BaseObject
     field :test_field, String, null: false,
     description: "An example field added by the generator"
     def test_field
       "Hello World!"
      end
    end
end
  • And #config/routes.rb 
Rails.application.routes.draw do
  post "/graphql", to: "graphql#execute"
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

 

Start the application and install your preferable GraphQL IDE/tool
and check the test_field query

 

 

BONUS STEP: Creating User List query:

  • Create User model and add few columns: 
rails g model User first_name last_name email
rails db:migrate
 

 

  • Next, open db/seeds.rb and add:
n = 5
n.times do | i|
  User.create(first_name: "demo#{i}", last_name: "user#{i}", email: "user#{i}@demo.com")
end
puts "Great..#{n} users added.."

Run Below Command

rails db:seed

 

  • And replace test_field with real users field: 
field :users, [Types::UserType], null: false,
description: "Returns all users"
def users
  User.all
end 

 

  • Now define UserType 
rails g graphql:object user 

This will create file: app/graphql/types/user_type.rb 

 

module Types
  class UserType < Types::BaseObject
    field :id, ID, null: false
    field :first_name, String, null: true
    field :last_name, String, null: true
    field :email, String, null: true
    field :created_at, GraphQL::Types::ISO8601DateTime, null: false
    field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
  end
end

Let’s check this, how it looks: 

 

 

 

Thank You!!
Facing issue, need suggestions related to Ruby on Rails or GraphQL ?
You can add comment reply you back as early as possible


charlie

I am full stack developer, Blogger



Comments



  • swazahmad | almost 4 years ago
    Awesome Information Thanks For Sharing
Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads