This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
learnexa_graphql_api [2019/04/19 07:35] 182.72.26.6 [Implementation] |
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 ====== |
| ===== Implementation ===== | ===== Implementation ===== | ||
| Line 67: | Line 67: | ||
| <code> | <code> | ||
| class Content < ApplicationRecord | class Content < ApplicationRecord | ||
| + | has_many :course_contents, :dependent => :destroy | ||
| + | has_many :courses, :through => :course_contents, :class_name => 'Course', :source => :course | ||
| end | end | ||
| </code> | </code> | ||
| Line 93: | Line 95: | ||
| field :created_at, String, null: false | field :created_at, String, null: false | ||
| field :updated_at, String, null: false | field :updated_at, String, null: false | ||
| + | field :course_contents, [Types::CourseContentType], null: false | ||
| end | end | ||
| end | end | ||
| Line 125: | Line 128: | ||
| def get_course(id:) | def get_course(id:) | ||
| Course.find(id) | Course.find(id) | ||
| + | end | ||
| + | | ||
| + | def get_content(id:) | ||
| + | Content.find(id) | ||
| end | end | ||
| end | end | ||
| Line 144: | Line 151: | ||
| 10) Start rails server | 10) Start rails server | ||
| - | Open localhost:3000/graphiql to gew | + | 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" | ||