Site Tools


Hotfix release available: 2025-05-14b "Librarian". upgrade now! [56.2] (what's this?)
Hotfix release available: 2025-05-14a "Librarian". upgrade now! [56.1] (what's this?)
New release available: 2025-05-14 "Librarian". upgrade now! [56] (what's this?)
Hotfix release available: 2024-02-06b "Kaos". upgrade now! [55.2] (what's this?)
Hotfix release available: 2024-02-06a "Kaos". upgrade now! [55.1] (what's this?)
New release available: 2024-02-06 "Kaos". upgrade now! [55] (what's this?)
Hotfix release available: 2023-04-04b "Jack Jackrum". upgrade now! [54.2] (what's this?)
Hotfix release available: 2023-04-04a "Jack Jackrum". upgrade now! [54.1] (what's this?)
New release available: 2023-04-04 "Jack Jackrum". upgrade now! [54] (what's this?)
Hotfix release available: 2022-07-31b "Igor". upgrade now! [53.1] (what's this?)
Hotfix release available: 2022-07-31a "Igor". upgrade now! [53] (what's this?)
New release available: 2022-07-31 "Igor". upgrade now! [52.2] (what's this?)
New release candidate 2 available: rc2022-06-26 "Igor". upgrade now! [52.1] (what's this?)
New release candidate available: 2022-06-26 "Igor". upgrade now! [52] (what's this?)
Hotfix release available: 2020-07-29a "Hogfather". upgrade now! [51.4] (what's this?)
New release available: 2020-07-29 "Hogfather". upgrade now! [51.3] (what's this?)
New release candidate 3 available: 2020-06-09 "Hogfather". upgrade now! [51.2] (what's this?)
New release candidate 2 available: 2020-06-01 "Hogfather". upgrade now! [51.1] (what's this?)
New release candidate available: 2020-06-01 "Hogfather". upgrade now! [51] (what's this?)
Hotfix release available: 2018-04-22c "Greebo". upgrade now! [50.3] (what's this?)
Hotfix release available: 2018-04-22b "Greebo". upgrade now! [50.2] (what's this?)
learnexa_graphql_api

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
learnexa_graphql_api [2019/04/19 06:59]
182.72.26.6 created
learnexa_graphql_api [2019/08/08 05:29] (current)
182.72.26.6 [Learnexa GrapphQL API]
Line 1: Line 1:
-====== Learnexa ​GrapphQL ​API ======+====== Learnexa ​GraphQL ​API ======
  
-===== Objective ​=====+===== Implementation ​=====
  
 1) Create new Ruby on Rails application 1) Create new Ruby on Rails application
Line 27: Line 27:
 </​code>​ </​code>​
  
-4) Start rails server +4) Add existing learnexa db in database.yml file
-Open localhost:​3000/​graphiql+
  
 +<​code>​
 +default: &​default
 +  adapter: mysql2
 +  encoding: utf8
 +  pool: <%= ENV.fetch("​RAILS_MAX_THREADS"​) { 5 } %>
 +  username: root
 +  password: root
 +  socket: /​var/​run/​mysqld/​mysqld.sock
 +
 +development:​
 +  <<: *default
 +  database: learnexa_db
 +</​code>​
 +
 +5) Recreate all learnexa models(Sample code given below):
 +
 +app/​models/​course.rb
 +
 +<​code>​
 +class Course < ApplicationRecord
 +  has_many :​course_contents,​ -> { order :position }, :dependent => :destroy
 +  has_many :contents, :through => :​course_contents
 +end
 +</​code>​
 +
 +app/​models/​course_content.rb
 +
 +<​code>​
 +class CourseContent < ApplicationRecord
 +  belongs_to :course
 +  belongs_to :content
 +end
 +</​code>​
 +
 +app/​models/​content.rb
 +
 +<​code>​
 +class Content < ApplicationRecord
 +  has_many :​course_contents,​ :dependent => :destroy
 +  has_many :courses, :through => :​course_contents,​ :class_name => '​Course',​ :source => :course
 +end
 +</​code>​
 +
 +6) Create course_type.rb file in app/​grapghql/​types folder:
 +
 +<​code>​
 +module Types
 +  class CourseType < BaseObject
 +    field :id, ID, null: false
 +    field :title, String, null: false
 +    field :​course_contents,​ [Types::​CourseContentType],​ null: false
 +    field :contents, [Types::​ContentType],​ null: false
 +  end
 +end
 +</​code>​
 +
 +7) Also create content_type.rb,​ course_content_type.rb files
 +
 +content_type.rb
 +<​code>​
 +module Types
 +  class ContentType < BaseObject
 +    field :id, ID, null: false
 +    field :title, String, null: false
 +    field :​created_at,​ String, null: false
 +    field :​updated_at,​ String, null: false
 +    field :​course_contents,​ [Types::​CourseContentType],​ null: false
 +  end
 +end
 +</​code>​
 +
 +course_content_type.rb
 +
 +<​code>​
 +module Types
 +  class CourseContentType < BaseObject
 +    field :id, ID, null: false
 +    field :​maximum_attempts,​ Integer, null: false
 +    field :course, Types::​CourseType,​ null: false
 +  end
 +end
 +</​code>​
 +
 +8) In app/​grapghql/​types/​quert_type.rb files add the query methods
 +
 +<​code>​
 +module Types
 +  class QueryType < Types::​BaseObject
 +    field :​all_courses,​ [Types::​CourseType],​ null: false
 +    field :​get_course,​ CourseType, null: false do
 +      argument :id, ID, required: true
 +    end
 +
 +    def all_courses
 +      Course.all
 +    end
 +
 +    def get_course(id:​)
 +      Course.find(id)
 +    end
 +    ​
 +    def get_content(id:​)
 +      Content.find(id)
 +    end
 +  end
 +end
 +</​code>​
 +
 +9) Build a schema with QueryType as the query entry point:
 +
 +app/​grapghql/​learnexa_api_schema.rb
 +
 +<​code>​
 +class LearnexaApiSchema < GraphQL::​Schema
 +  mutation Types::​MutationType
 +  query Types::​QueryType
 +end
 +</​code>​
 +
 +
 +
 +10) Start rails server
 +Open localhost:​3000/​graphiql to run the queries and fetch records.
 +
 +11) Sample API curl call:
 +
 +Example 1
 +
 +Request
 +
 +<​code>​
 +curl \
 +  -X POST \
 +  -H "​Content-Type:​ application/​json"​ \
 +  --data '{ "​query":​ "{ allCourses { title } }" }' \
 +  http://​localhost:​3000/​graphql
 +</​code>​
 +
 +Response
 +
 +<​code>​
 +{"​data":​{"​allCourses":​[{"​title":"​ds"​},​
 +  {"​title":"​Agile Query-Driven Data Modeling for NoSQL"​},​
 +  {"​title":"​Practitioner’s Perspective on the Enterprise Data Model"​},​
 +  {"​title":"​A Foundation For All Things Process: Modeling, Design and Architecture"​},​
 +  {"​title":"​Human-Centered Design"​},​{"​title":"​Application Development with Python and Flask"​}]}
 +}
 +</​code>​
 +
 +Example 2
 +
 +Request query:
 +
 +<​code>​
 +{
 +  getContent(id:​ 3234) {
 +    title
 +    courseContents {
 +      maximumAttempts
 +    }
 +  }
 +}
 +</​code>​
 +
 +Response:
 +
 +<​code>​
 +{
 +  "​data":​ {
 +    "​getContent":​ {
 +      "​title":​ "​Intermediate System to Intermediate System (IS-IS) Routing Protocol",​
 +      "​courseContents":​ [
 +        {
 +          "​maximumAttempts":​ 1
 +        }
 +      ]
 +    }
 +  }
 +}
 +</​code>​
 +
 +Example 3
 +
 +Request:
 +
 +<​code>​
 +{
 +  getCourse(id:​ 2) {
 +    title
 +    contents {
 +      title
 +    }
 +  }
 +}
 +</​code>​
 +
 +Response:
 +
 +<​code>​
 +{
 +  "​data":​ {
 +    "​getCourse":​ {
 +      "​title":​ "​Practitioner’s Perspective on the Enterprise Data Model",​
 +      "​contents":​ [
 +        {
 +          "​title":​ "​Practitioner’s Perspective on the Enterprise Data Model"
 +        }
 +      ]
 +    }
 +  }
 +}
 +</​code>​
 +
 +===== API Setup Server =====
 +
 +1) Download the following zip file
 +
 +{{learnexa_api.zip}}
 +
 +2) Run "​bundle install"​
  
 +3) Change database name in "​database.yml"​. Point it to current devlep01 server database.
  
 +4) Start rails server in the working directory "rails s"
learnexa_graphql_api.1555657155.txt.gz · Last modified: 2019/04/19 06:59 by 182.72.26.6