Last active
October 15, 2018 05:03
-
-
Save msroz/44bd82c3a054196b536605d4fb74b780 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_hash' | |
require 'awesome_print' | |
require 'graphql' | |
class Category < ActiveHash::Base | |
include ActiveHash::Associations | |
fields :title, :body, :posts, :featured | |
has_many :posts | |
create id: 1, title: 'graphql', body: 'About graghql', featured: true | |
create id: 2, title: 'ruby', body: 'About ruby', featured: false | |
create id: 3, title: 'crystal', body: 'About crystal', featured: true | |
create id: 4, title: 'swift', body: 'About swift', featured: false | |
create id: 5, title: 'python3', body: 'About python3', featured: false | |
end | |
class Post < ActiveHash::Base | |
include ActiveHash::Associations | |
fields :title, :body, :category_id, :comments | |
belongs_to :category | |
has_many :comments | |
create id: 1, title: 'Hello graphql', body: 'blah blah blah', category_id: 1 | |
create id: 2, title: 'Good Morning graphql', body: 'blah blah blah', category_id: 1 | |
create id: 3, title: 'Good Evening graphql', body: 'blah blah blah', category_id: 1 | |
create id: 4, title: 'Byebye graphql', body: 'blah blah blah', category_id: 1 | |
create id: 5, title: 'Hello ruby', body: 'blah blah blah', category_id: 2 | |
create id: 6, title: 'Hello crystal', body: 'blah blah blah', category_id: 3 | |
create id: 7, title: 'Hello swift', body: 'blah blah blah', category_id: 4 | |
end | |
class Comment < ActiveHash::Base | |
include ActiveHash::Associations | |
fields :body, :post_id | |
belongs_to :post | |
create id: 1, body: 'good', post_id: 1 | |
create id: 2, body: 'so so', post_id: 1 | |
create id: 3, body: 'so so', post_id: 2 | |
create id: 4, body: 'so so', post_id: 2 | |
create id: 5, body: 'so so', post_id: 3 | |
create id: 6, body: 'so so', post_id: 3 | |
create id: 7, body: 'so so', post_id: 3 | |
create id: 8, body: 'so so', post_id: 3 | |
create id: 9, body: 'so so', post_id: 4 | |
end | |
CategoryType = GraphQL::ObjectType.define do | |
name "Category" | |
description "A blog category" | |
field :id, !types.ID | |
field :title, !types.String | |
field :body, !types.String | |
field :featured, !types.Boolean | |
field :posts, types[!PostType] | |
field :post_count, !types.Int, "Number of posts" do | |
resolve ->(obj, args, ctx) { | |
obj.posts.size | |
} | |
end | |
end | |
PostType = GraphQL::ObjectType.define do | |
name "Post" | |
description "A blog post" | |
field :id, !types.ID # ! means not null | |
field :category_id, !types.Int | |
field :category, !CategoryType | |
field :title, !types.String | |
field :body, !types.String | |
field :comments, types[!CommentType] | |
field :teaser, !types.String, "The teaser of the post" do | |
# obj is an instance of Post | |
# args `GraphQL::Query::Arguments` instance | |
# ctx `GraphQL::Query::Context` instance | |
resolve ->(obj, args, ctx) { | |
obj.body[0, 10] | |
} | |
end | |
field :comment_count, !types.Int, "Number of comments" do | |
resolve ->(obj, args, ctx) { | |
obj.comments.size | |
} | |
end | |
end | |
CreatePostField = GraphQL::Field.define do |f| | |
f.type PostType | |
f.description "Create a post" | |
f.argument :title, !types.String | |
f.argument :body, !types.String | |
f.argument :category_id, !types.Int | |
f.resolve ->(obj, args, ctx) { | |
Post.create(category_id: args[:category_id], title: args[:title], body: args[:body]) | |
} | |
end | |
CommentType = GraphQL::ObjectType.define do | |
name "Comment" | |
field :id, !types.ID | |
field :body, !types.String | |
end | |
QueryType = GraphQL::ObjectType.define do | |
name "Query" | |
description "The query root of this schema" | |
# | |
# member | |
# | |
field :post do | |
type PostType | |
argument :id, !types.ID | |
description "Find a Post by ID" | |
resolve ->(obj, args, ctx) { Post.find(args[:id]) } | |
end | |
# | |
# member | |
# | |
field :category do | |
type CategoryType | |
argument :id, !types.ID | |
description "Find a Category by ID" | |
resolve ->(obj, args, ctx) { Category.find(args[:id]) } | |
end | |
# | |
# collection | |
# | |
field :categories, types[CategoryType] do | |
description "Find Categories" | |
resolve ->(obj, args, ctx) { Category.all } | |
end | |
# | |
# search | |
# | |
field :posts, types[PostType] do | |
description "Find Posts by Category" | |
argument :category_id, !types.Int | |
resolve ->(obj, args, ctx) { | |
Post.find_all_by_category_id(args[:category_id]) | |
} | |
end | |
end | |
MutationType = GraphQL::ObjectType.define do | |
name "Mutation" | |
field :createPost, field: CreatePostField | |
end | |
Schema = GraphQL::Schema.define do | |
query QueryType | |
mutation MutationType | |
end | |
example_queris = { | |
post: " | |
{ | |
post(id: 1) { | |
id | |
title | |
teaser | |
body | |
comment_count | |
comments { | |
id | |
body | |
} | |
} | |
}", | |
category: " | |
{ | |
category(id: 1) { | |
id | |
title | |
featured | |
post_count | |
posts { | |
id | |
title | |
} | |
} | |
} | |
", | |
categories: " | |
{ | |
categories { | |
id | |
title | |
} | |
} | |
", | |
posts: " | |
{ | |
posts(category_id: 1) { | |
id | |
title | |
} | |
} | |
", | |
createPost: ' | |
mutation { | |
createPost(title: "Hello created post", body: "This post is ceated through graphql query", category_id: 1) { | |
id | |
title | |
body | |
category_id | |
} | |
} | |
', | |
aliasPosts: ' | |
{ | |
rubyPosts: posts(category_id: 2) { | |
id | |
title | |
category { | |
id | |
title | |
} | |
}, | |
crystalPosts: posts(category_id: 3) { | |
id | |
title | |
category { | |
id | |
title | |
} | |
}, | |
swiftPosts: posts(category_id: 4) { | |
id | |
title | |
category { | |
id | |
title | |
} | |
} | |
} | |
' | |
} | |
context = { | |
application: 'grapql playground' | |
} | |
puts "######## Query ###########" | |
ap example_queris[ARGV[0].to_sym] | |
puts "######## Query Execution Result ###########" | |
ap Schema.execute(example_queris[ARGV[0].to_sym], context: context) | |
Author
msroz
commented
Oct 15, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment