Last active
April 19, 2019 12:08
-
-
Save benbonnet/ca889298a53df16377015471c646af1b to your computer and use it in GitHub Desktop.
Basic graphql ruby implementation, using Query, Input, Resolvers, Types and Mutations
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 'graphql' | |
require 'awesome_print' | |
######### | |
# TYPES # | |
######### | |
module Types | |
class BaseObject < GraphQL::Schema::Object | |
end | |
end | |
module Types | |
class Item < Types::BaseObject | |
description 'Resembles a Item Object Type' | |
field :id, ID, null: false | |
field :title, String, null: false | |
end | |
end | |
########## | |
# INPUTS # | |
########## | |
module Inputs | |
class ItemAttributes < GraphQL::Schema::InputObject | |
description "Attributes for creating or updating a blog post" | |
argument :title, String, required: true | |
end | |
end | |
############# | |
# RESOLVERS # | |
############# | |
module Resolvers | |
# resolvers base | |
class Base < GraphQL::Schema::Resolver | |
# argument_class Arguments::Base | |
end | |
end | |
module Resolvers | |
# item resolver | |
class Item < GraphQL::Schema::Resolver | |
type Types::Item, null: false | |
argument :id, ID, required: true | |
def resolve(id:) | |
{ title: id } | |
end | |
end | |
end | |
module Resolvers | |
# item resolver | |
class Items < GraphQL::Schema::Resolver | |
type [Types::Item], null: false | |
argument :page, Int, required: false | |
def resolve(page: nil) | |
[ | |
{ title: page }, | |
{ title: page } | |
] | |
end | |
end | |
end | |
############# | |
# MUTATIONS # | |
############# | |
module Mutations | |
class BaseMutation < GraphQL::Schema::Mutation | |
end | |
end | |
module Mutations | |
class CreateItem < Mutations::BaseMutation | |
description 'Creates an item' | |
argument :item_attributes, ::Inputs::ItemAttributes, required: true | |
field :item, ::Types::Item, null: false | |
field :errors, [String], null: false | |
def resolve(item_attributes:) | |
{ | |
item: { title: item_attributes.dig(:title) }, | |
errors: [ 'a', 'b' ] | |
} | |
end | |
end | |
end | |
######## | |
# BASE # | |
######## | |
class MutationType < GraphQL::Schema::Object | |
description "The mutation root of this schema" | |
field :createItem, mutation: Mutations::CreateItem | |
end | |
class QueryType < GraphQL::Schema::Object | |
description "The query root of this schema" | |
field :item, resolver: ::Resolvers::Item, description: 'ok item' | |
field :items, resolver: ::Resolvers::Items, description: 'ok items' | |
def speakers | |
[] | |
end | |
end | |
############## | |
# ENTRYPOINT # | |
############## | |
class AppSchema < GraphQL::Schema | |
query QueryType | |
mutation MutationType | |
end | |
############ | |
# EXECUTER # | |
############ | |
def execute(query, variables) | |
AppSchema.execute( | |
query, | |
variables: variables, | |
context: { current_user: nil }, | |
) | |
end | |
################# | |
# TEST MUTATION # | |
################# | |
mutation_query = " | |
mutation CreateItem($attributes: ItemAttributes!) { | |
createItem(itemAttributes: $attributes) { | |
item { | |
title | |
} | |
} | |
} | |
" | |
mutation_variables = { attributes: { title: 'cool' } } | |
ap execute(mutation_query, mutation_variables).to_h | |
####################### | |
# TEST RESOLVE (ITEM) # | |
####################### | |
item_query = " | |
query ($id: ID!) { | |
item(id: $id) { | |
title | |
} | |
} | |
" | |
item_variables = { id: 'xyz' } | |
ap execute(item_query, item_variables).to_h | |
######################## | |
# TEST RESOLVE (ITEMS) # | |
######################## | |
items_query = " | |
query ($page: Int) { | |
items(page: $page) { | |
title | |
} | |
} | |
" | |
items_variables = { page: 2 } | |
ap execute(items_query, items_variables).to_h | |
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
#!/bin/sh | |
FILE="./Gemfile" | |
/bin/cat <<EOM >$FILE | |
# frozen_string_literal: true | |
source 'https://rubygems.org' | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
ruby '2.5.1' | |
gem 'graphql' | |
gem 'awesome_print' | |
EOM | |
bundle install && ruby app.rb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment