Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.

Commit bb04f4a

Browse files
authored
Merge pull request #6 from GraphQLAcademy/graphiql
Add GraphiQL and GraphQL Controller
2 parents b1cd1b2 + 70f9e42 commit bb04f4a

9 files changed

Lines changed: 39 additions & 10 deletions

File tree

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ gem 'sqlite3'
1313
gem 'puma', '~> 3.0'
1414
# Use GraphQL!
1515
gem 'graphql', '~> 1.4.2'
16+
# GraphiQL Interface
17+
gem 'graphiql-rails', '~> 1.4.1'
1618

1719
group :development, :test do
1820
# Call 'byebug' anywhere in the code to stop execution and get a debugger console

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ GEM
4747
ffi (1.9.17)
4848
globalid (0.3.7)
4949
activesupport (>= 4.1.0)
50+
graphiql-rails (1.4.1)
51+
rails
5052
graphql (1.4.2)
5153
i18n (0.8.0)
5254
listen (3.0.8)
@@ -127,6 +129,7 @@ PLATFORMS
127129

128130
DEPENDENCIES
129131
byebug
132+
graphiql-rails (~> 1.4.1)
130133
graphql (~> 1.4.2)
131134
listen (~> 3.0.5)
132135
puma (~> 3.0)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class GraphqlController < ApplicationController
2+
def execute
3+
query_string = params[:query].to_s
4+
variables = ensure_hash(params[:variables])
5+
result = ::Graph::Schema.execute(query_string, variables: variables)
6+
render json: result
7+
end
8+
9+
private
10+
11+
def ensure_hash(variables)
12+
if variables.blank?
13+
{}
14+
elsif variables.is_a?(String)
15+
JSON.parse(variables)
16+
else
17+
variables
18+
end
19+
end
20+
end

app/graph/query.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

app/models/graph/query.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Graph
2+
Query = GraphQL::ObjectType.define do
3+
name "Query"
4+
description "The query root of this schema"
5+
6+
# TODO(xuorig) Populate with real fields (GraphiQL crashes with no fields on root)
7+
field :test, types.String, resolve: ->(_, _, _) { 'test' }
8+
end
9+
end

config/application.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ class Application < Rails::Application
2121
# Settings in config/environments/* take precedence over those specified here.
2222
# Application configuration should go into files in config/initializers
2323
# -- all .rb files in that directory are automatically loaded.
24-
config.autoload_paths << Rails.root.join('app', 'graph')
2524
end
2625
end

config/routes.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
Rails.application.routes.draw do
2-
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
2+
if Rails.env.development?
3+
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
4+
end
5+
6+
post '/graphql', to: 'graphql#execute'
37
end

0 commit comments

Comments
 (0)