Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add ruby-graphql
  • Loading branch information
tb committed Jun 9, 2017
commit 13459c90ad389df453b7f6a0a876be76c43e8c73
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
.byebug_history
.idea/
public/
node_modules/
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ gem 'devise_token_auth'
gem 'cancan'
gem 'rolify'
gem 'pry'
gem 'graphql'
gem 'graphiql-rails', group: :development
gem 'graphql-batch'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand All @@ -48,6 +51,3 @@ group :development do
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
11 changes: 10 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ GEM
thor (~> 0.19.1)
globalid (0.3.7)
activesupport (>= 4.1.0)
graphiql-rails (1.4.1)
rails
graphql (1.6.3)
graphql-batch (0.3.3)
graphql (>= 0.8, < 2)
promise.rb (~> 0.7.2)
i18n (0.8.1)
jsonapi-resources (0.9.0)
activerecord (>= 4.1)
Expand All @@ -88,6 +94,7 @@ GEM
mini_portile2 (~> 2.1.0)
orm_adapter (0.5.0)
pg (0.20.0)
promise.rb (0.7.3)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
Expand Down Expand Up @@ -177,6 +184,9 @@ DEPENDENCIES
factory_girl
faker
foreman
graphiql-rails
graphql
graphql-batch
jsonapi-resources
listen (~> 3.0.5)
pg (~> 0.18)
Expand All @@ -188,7 +198,6 @@ DEPENDENCIES
rspec-rails
spring
spring-watcher-listen (~> 2.0.0)
tzinfo-data

BUNDLED WITH
1.14.6
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ Demo user: user1@example.com / Secret123
### Client

Add list, edit and form components in `client/src/components/` based on one of existing.

### [graphiql](http://localhost:3001/graphiql)

{
categories {
name
}
}
32 changes: 32 additions & 0 deletions app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class GraphqlController < ApplicationController
def execute
variables = ensure_hash(params[:variables])
query = params[:query]
context = {
# Query context goes here, for example:
# current_user: current_user,
}
result = RailsJsonApiServerSchema.execute(query, variables: variables, context: context)
render json: result
end

private

# Handle form data, JSON body, or a blank value
def ensure_hash(ambiguous_param)
case ambiguous_param
when String
if ambiguous_param.present?
ensure_hash(JSON.parse(ambiguous_param))
else
{}
end
when Hash, ActionController::Parameters
ambiguous_param
when nil
{}
else
raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
end
end
end
Empty file added app/graphql/loaders/.keep
Empty file.
Empty file added app/graphql/mutations/.keep
Empty file.
6 changes: 6 additions & 0 deletions app/graphql/rails_json_api_server_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RailsJsonApiServerSchema = GraphQL::Schema.define do
query(Types::QueryType)

# GraphQL::Batch setup:
use GraphQL::Batch
end
Empty file added app/graphql/types/.keep
Empty file.
6 changes: 6 additions & 0 deletions app/graphql/types/category_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Types::CategoryType = GraphQL::ObjectType.define do
name "Category"
field :id, types.Int
field :name, types.String
field :created_at, types.String
end
20 changes: 20 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Types::QueryType = GraphQL::ObjectType.define do
name "Query"
description "The query root of this schema"

field :category do
type Types::CategoryType
argument :id, !types.ID
description "Find a Category by ID"
resolve ->(obj, args, ctx) { Category.find(args["id"]) }
end

field :categories do
type types[Types::CategoryType]
argument :id, types[types.ID]
description "Find Categories"
resolve ->(obj, args, ctx) {
args["ids"] ? Category.where(id: args["ids"]) : Category.all
}
end
end
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
# require "sprockets/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
Rails.application.routes.draw do
if Rails.env.development?
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
end

post "/graphql", to: "graphql#execute"
mount_devise_token_auth_for 'User', at: 'auth'
jsonapi_resources :categories
jsonapi_resources :comments
Expand Down