====== Learnexa GraphQL API ====== ===== Implementation ===== 1) Create new Ruby on Rails application Ruby Verions 2.6.2 Rails verions 5.1.4 rails new learnexa_api -d mysql 2) Add GraphQL in gem file gem 'graphql', '~>1.8.10' bundle install 3) Add GraphQL to a Rails app rails g graphql:install bundle install 4) Add existing learnexa db in database.yml file 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 5) Recreate all learnexa models(Sample code given below): app/models/course.rb class Course < ApplicationRecord has_many :course_contents, -> { order :position }, :dependent => :destroy has_many :contents, :through => :course_contents end app/models/course_content.rb class CourseContent < ApplicationRecord belongs_to :course belongs_to :content end app/models/content.rb class Content < ApplicationRecord has_many :course_contents, :dependent => :destroy has_many :courses, :through => :course_contents, :class_name => 'Course', :source => :course end 6) Create course_type.rb file in app/grapghql/types folder: 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 7) Also create content_type.rb, course_content_type.rb files content_type.rb 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 course_content_type.rb module Types class CourseContentType < BaseObject field :id, ID, null: false field :maximum_attempts, Integer, null: false field :course, Types::CourseType, null: false end end 8) In app/grapghql/types/quert_type.rb files add the query methods 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 9) Build a schema with QueryType as the query entry point: app/grapghql/learnexa_api_schema.rb class LearnexaApiSchema < GraphQL::Schema mutation Types::MutationType query Types::QueryType end 10) Start rails server Open localhost:3000/graphiql to run the queries and fetch records. 11) Sample API curl call: Example 1 Request curl \ -X POST \ -H "Content-Type: application/json" \ --data '{ "query": "{ allCourses { title } }" }' \ http://localhost:3000/graphql Response {"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"}]} } Example 2 Request query: { getContent(id: 3234) { title courseContents { maximumAttempts } } } Response: { "data": { "getContent": { "title": "Intermediate System to Intermediate System (IS-IS) Routing Protocol", "courseContents": [ { "maximumAttempts": 1 } ] } } } Example 3 Request: { getCourse(id: 2) { title contents { title } } } Response: { "data": { "getCourse": { "title": "Practitioner’s Perspective on the Enterprise Data Model", "contents": [ { "title": "Practitioner’s Perspective on the Enterprise Data Model" } ] } } } ===== 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"