Skip to content

Commit 4399564

Browse files
committed
Install graphql gem & run install generator
1 parent 79193d3 commit 4399564

File tree

9 files changed

+69
-0
lines changed

9 files changed

+69
-0
lines changed

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ gem 'jbuilder', '~> 2.5'
3333
# Use Capistrano for deployment
3434
# gem 'capistrano-rails', group: :development
3535

36+
gem 'graphql'
37+
3638
group :development, :test do
3739
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
3840
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
@@ -52,3 +54,5 @@ end
5254

5355
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
5456
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
57+
58+
gem 'graphiql-rails', group: :development

Gemfile.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ GEM
6666
ffi (1.9.18)
6767
globalid (0.4.0)
6868
activesupport (>= 4.2.0)
69+
graphiql-rails (1.4.2)
70+
rails
71+
graphql (1.6.6)
6972
i18n (0.8.6)
7073
jbuilder (2.7.0)
7174
activesupport (>= 4.2.0)
@@ -177,6 +180,8 @@ DEPENDENCIES
177180
byebug
178181
capybara (~> 2.13)
179182
coffee-rails (~> 4.2)
183+
graphiql-rails
184+
graphql
180185
jbuilder (~> 2.5)
181186
listen (>= 3.0.5, < 3.2)
182187
puma (~> 3.7)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class GraphqlController < ApplicationController
2+
def execute
3+
variables = ensure_hash(params[:variables])
4+
query = params[:query]
5+
operation_name = params[:operationName]
6+
context = {
7+
# Query context goes here, for example:
8+
# current_user: current_user,
9+
}
10+
result = GraphqlTutorialSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
11+
render json: result
12+
end
13+
14+
private
15+
16+
# Handle form data, JSON body, or a blank value
17+
def ensure_hash(ambiguous_param)
18+
case ambiguous_param
19+
when String
20+
if ambiguous_param.present?
21+
ensure_hash(JSON.parse(ambiguous_param))
22+
else
23+
{}
24+
end
25+
when Hash, ActionController::Parameters
26+
ambiguous_param
27+
when nil
28+
{}
29+
else
30+
raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
31+
end
32+
end
33+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
GraphqlTutorialSchema = GraphQL::Schema.define do
2+
mutation(Types::MutationType)
3+
query(Types::QueryType)
4+
end

app/graphql/mutations/.keep

Whitespace-only changes.

app/graphql/types/.keep

Whitespace-only changes.

app/graphql/types/mutation_type.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Types::MutationType = GraphQL::ObjectType.define do
2+
name "Mutation"
3+
4+
# TODO: Add Mutations as fields
5+
end

app/graphql/types/query_type.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Types::QueryType = GraphQL::ObjectType.define do
2+
name "Query"
3+
# Add root-level fields here.
4+
# They will be entry points for queries on your schema.
5+
6+
# TODO: remove me
7+
field :testField, types.String do
8+
description "An example field added by the generator"
9+
resolve ->(obj, args, ctx) {
10+
"Hello World!"
11+
}
12+
end
13+
end

config/routes.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
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
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
38
end

0 commit comments

Comments
 (0)