1 of 31

https://www.linkedin.com/in/mehreentahir93/

2 of 31

3 of 31

4 of 31

INTEGRATING GRAPHQL �WITH � RUBY ON RAILS

5 of 31

WEBINAR ROADMAP

  • What Is GraphQL�
  • Typical REST Application

  • REST Potential Drawback

  • Graphql Application�
  • Graphql Operations�
  • Proof Of Concept Application

6 of 31

WHAT IS HYPE ABOUT

06 / 29

7 of 31

WHY & HOW IT CAME INTO EXISTENCE

07 / 29

8 of 31

ARCHITECTURE

08 / 29

9 of 31

UNDER FETCHING

SITUATIONS

OVER FETCHING

Need To Display Single Author With All Details

Need To List All Author Names

NOT ENOUGH DATA PER REQUEST�(Doesn’t fetch chapter details)

EXTRA DATA PER REQUEST�(Fetches un necessary book details)

09 / 29

10 of 31

ARCHITECTURE

10 / 29

11 of 31

QUERIES & MUTATIONS

QUERIESmechanism for reading data

MUTATIONSmechanism for creating/updating data

11 / 29

12 of 31

12 / 29

13 of 31

Today’s proof of concept will be on a basic example, in which author can have many books and each book can have multiple chapters.

13 / 29

14 of 31

  • Create rails project
    • rails new graphql_rubyconf --skip-test�
  • Generate models
    • rails g model Author email:string name:string
    • rails g model Book name:string description:text author:belongs_to
    • rails g model Chapter name:string short_description:text book:belongs_to�
  • Migrate DB
    • rake db:migrate�
  • Add missing associations in author and books models
    • author#has_many :books
    • book#has_many :chapters�
  • Datalayer is now all set!

CREATING PROJECT SKELETON

14 / 29

15 of 31

  • Add faker gem
    • gem “faker” (https://github.com/faker-ruby/faker)
    • bundle install�
  • Populate seed file�

2.times do

author = Author.create(name: Faker::Name.name, email: Faker::Internet.email)

3.times do

book = author.books.create(name: Faker::Lorem.sentence(word_count:2), description: Faker::Lorem.paragraph(sentence_count:2))

2.times do

book.chapters.create(name: Faker::Lorem.sentence(word_count: 2), short_description: Faker::Lorem.paragraph(sentence_count: 2))

end

end

end�

  • Verify data in console

SEEDING DATA FOR QUERYING

15 / 29

16 of 31

  • Install graphql

  • Run graphql install
    • rails generate graphql:install�
  • Check the graphql generator section added into rails generators
    • rails generate
  • Schema file analysis�
  • Everything is ready for building some basic queries and we can boot our servers BUT visualization is not good yet.

GRAPHQL INSTALLATION & OBJECT GENERATIONS

16 / 29

17 of 31

  • Install graphiql-rails

  • Following the guide, update the route file

if Rails.env.development?

mount GraphiQL::Rails::Engine, at: "/graphql", graphql_path: "graphql#execute"

end

  • Boot the server at http://localhost:3000/graphql (this is the single point of interaction)�
  • Try the default query

ADDING BETTER VISUALIZATION

17 / 29

18 of 31

GRAPHQL QUERIES

Queries are used to read data

Terminology: ��field

object

18 / 29

19 of 31

  • Query to fetch all authors, which return all authors with name,id and book_count.�
  • Create GraphQL objects
    • rails g graphql:object author

  • Add fields into author type
    • name
    • id
    • book_count�
  • Include these queries into base QueryType
    • add authors field which returns array of author type
    • define the corresponding rails method�
  • Execute the basic query

(1) QUERY LIST OF AUTHORS WITH BASIC INFORMATION

19 / 29

20 of 31

  • Extend the query to fetch relational data for instance books list for the authors.�
  • Create GraphQL objects
    • rails g graphql:object book

  • Add fields into book type
    • name
    • id
    • description

  • Add the association field for books into author type.

  • Execute the basic relational query

(2) QUERY LIST OF AUTHORS WITH BOOKS

20 / 29

21 of 31

  • Extend book to fetch chapters and author details�
  • Create GraphQL objects
    • rails g graphql:object chapter�
  • Add fields into chapter type
    • name
    • short_description
    • id
    • author_name

  • Add the association of chapters into book type.�
  • Execute the query

(3) QUERY LIST OF AUTHORS WITH BOOKS AND ITS CHAPTERS

21 / 29

22 of 31

  • Execute the query�

QUERY

FETCH LIST OF AUTHORS

22 / 29

23 of 31

  • Define single author query in base query for graphql�
    • add author field which returns author type
    • block which takes the required ‘id’ argument
    • define the corresponding rails method

QUERY FOR SINGLE AUTHOR

23 / 29

24 of 31

  • Execute the query��

QUERY

FETCH SINGLE AUTHOR

24 / 29

25 of 31

GRAPHQL MUTATIONS

Mutations are used for Creating and updating data

Terminology:��arguments

fields

resolve ��

25 / 29

26 of 31

  • Create new mutation�
    • Inside mutations folder, create a new file named as create_author.rb

    • Create a class and inherit from base mutation�Mutations::CreateAuthor < Mutations::BaseMutation

    • Add description to this mutation

    • Add arguments which mutation needs for its creation
      • name
      • email

    • Add fields which mutation will return upon creation, in our case it will be
      • author
      • errors�
    • Add the resolve method
      • Perform the active record part in it and create it
      • Return hash with author and error keys�
  • Add the mutation type as field in base mutation type, with referring the mutation we created
    • field :create_author, mutation: Mutations::CreateAuthor�

CREATE AUTHOR MUTATION

26 / 29

27 of 31

  • Execute the mutation�

CREATE AUTHOR MUTATION

27 / 29

28 of 31

  • Create new mutation�
    • Inside mutations folder, create a new file named as update_author.rb

    • Create a class and inherit from base mutation�Mutations::UpdateAuthor < Mutations::BaseMutation

    • Add description to this mutation

    • Add arguments which mutation needs for its creation
      • name
      • ID

    • Add fields which mutation will return upon creation, in our case it will be
      • author
      • errors�
    • Add the resolve method
      • Perform the active record part in it and update it
      • Return hash with author and error keys�
  • Add the mutation type as field in base mutation type, with referring the mutation we created
    • field :update_author, mutation: Mutations::UpdateAuthor

UPDATE AUTHOR MUTATION

28 / 29

29 of 31

  • Execute the mutation���

UPDATE AUTHOR MUTATION

29 / 29

30 of 31

RESOURCES

31 of 31

THANK YOU