Skip to content

Commit 13459c9

Browse files
committed
Add ruby-graphql
1 parent bbbbd04 commit 13459c9

File tree

13 files changed

+92
-5
lines changed

13 files changed

+92
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
.byebug_history
1818
.idea/
1919
public/
20+
node_modules/

Gemfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ gem 'devise_token_auth'
3131
gem 'cancan'
3232
gem 'rolify'
3333
gem 'pry'
34+
gem 'graphql'
35+
gem 'graphiql-rails', group: :development
36+
gem 'graphql-batch'
3437

3538
group :development, :test do
3639
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
@@ -48,6 +51,3 @@ group :development do
4851
gem 'spring'
4952
gem 'spring-watcher-listen', '~> 2.0.0'
5053
end
51-
52-
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
53-
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Gemfile.lock

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ GEM
6565
thor (~> 0.19.1)
6666
globalid (0.3.7)
6767
activesupport (>= 4.1.0)
68+
graphiql-rails (1.4.1)
69+
rails
70+
graphql (1.6.3)
71+
graphql-batch (0.3.3)
72+
graphql (>= 0.8, < 2)
73+
promise.rb (~> 0.7.2)
6874
i18n (0.8.1)
6975
jsonapi-resources (0.9.0)
7076
activerecord (>= 4.1)
@@ -88,6 +94,7 @@ GEM
8894
mini_portile2 (~> 2.1.0)
8995
orm_adapter (0.5.0)
9096
pg (0.20.0)
97+
promise.rb (0.7.3)
9198
pry (0.10.4)
9299
coderay (~> 1.1.0)
93100
method_source (~> 0.8.1)
@@ -177,6 +184,9 @@ DEPENDENCIES
177184
factory_girl
178185
faker
179186
foreman
187+
graphiql-rails
188+
graphql
189+
graphql-batch
180190
jsonapi-resources
181191
listen (~> 3.0.5)
182192
pg (~> 0.18)
@@ -188,7 +198,6 @@ DEPENDENCIES
188198
rspec-rails
189199
spring
190200
spring-watcher-listen (~> 2.0.0)
191-
tzinfo-data
192201

193202
BUNDLED WITH
194203
1.14.6

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ Demo user: [email protected] / Secret123
2828
### Client
2929

3030
Add list, edit and form components in `client/src/components/` based on one of existing.
31+
32+
### [graphiql](http://localhost:3001/graphiql)
33+
34+
{
35+
categories {
36+
name
37+
}
38+
}

app/controllers/graphql_controller.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class GraphqlController < ApplicationController
2+
def execute
3+
variables = ensure_hash(params[:variables])
4+
query = params[:query]
5+
context = {
6+
# Query context goes here, for example:
7+
# current_user: current_user,
8+
}
9+
result = RailsJsonApiServerSchema.execute(query, variables: variables, context: context)
10+
render json: result
11+
end
12+
13+
private
14+
15+
# Handle form data, JSON body, or a blank value
16+
def ensure_hash(ambiguous_param)
17+
case ambiguous_param
18+
when String
19+
if ambiguous_param.present?
20+
ensure_hash(JSON.parse(ambiguous_param))
21+
else
22+
{}
23+
end
24+
when Hash, ActionController::Parameters
25+
ambiguous_param
26+
when nil
27+
{}
28+
else
29+
raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
30+
end
31+
end
32+
end

app/graphql/loaders/.keep

Whitespace-only changes.

app/graphql/mutations/.keep

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
RailsJsonApiServerSchema = GraphQL::Schema.define do
2+
query(Types::QueryType)
3+
4+
# GraphQL::Batch setup:
5+
use GraphQL::Batch
6+
end

app/graphql/types/.keep

Whitespace-only changes.

app/graphql/types/category_type.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Types::CategoryType = GraphQL::ObjectType.define do
2+
name "Category"
3+
field :id, types.Int
4+
field :name, types.String
5+
field :created_at, types.String
6+
end

app/graphql/types/query_type.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Types::QueryType = GraphQL::ObjectType.define do
2+
name "Query"
3+
description "The query root of this schema"
4+
5+
field :category do
6+
type Types::CategoryType
7+
argument :id, !types.ID
8+
description "Find a Category by ID"
9+
resolve ->(obj, args, ctx) { Category.find(args["id"]) }
10+
end
11+
12+
field :categories do
13+
type types[Types::CategoryType]
14+
argument :id, types[types.ID]
15+
description "Find Categories"
16+
resolve ->(obj, args, ctx) {
17+
args["ids"] ? Category.where(id: args["ids"]) : Category.all
18+
}
19+
end
20+
end

config/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
require "action_mailer/railtie"
1010
require "action_view/railtie"
1111
require "action_cable/engine"
12-
# require "sprockets/railtie"
12+
require "sprockets/railtie"
1313
require "rails/test_unit/railtie"
1414

1515
# Require the gems listed in Gemfile, including any gems

config/routes.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
Rails.application.routes.draw do
2+
if Rails.env.development?
3+
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
4+
end
5+
6+
post "/graphql", to: "graphql#execute"
27
mount_devise_token_auth_for 'User', at: 'auth'
38
jsonapi_resources :categories
49
jsonapi_resources :comments

0 commit comments

Comments
 (0)