Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ gem 'sqlite3'
gem 'puma', '~> 3.0'
# Use GraphQL!
gem 'graphql', '~> 1.4.2'
# GraphiQL Interface
gem 'graphiql-rails', '~> 1.4.1'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ GEM
ffi (1.9.17)
globalid (0.3.7)
activesupport (>= 4.1.0)
graphiql-rails (1.4.1)
rails
graphql (1.4.2)
i18n (0.8.0)
listen (3.0.8)
Expand Down Expand Up @@ -127,6 +129,7 @@ PLATFORMS

DEPENDENCIES
byebug
graphiql-rails (~> 1.4.1)
graphql (~> 1.4.2)
listen (~> 3.0.5)
puma (~> 3.0)
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class GraphqlController < ApplicationController
def execute
query_string = params[:query].to_s
variables = ensure_hash(params[:variables])
result = ::Graph::Schema.execute(query_string, variables: variables)
render json: result
end

private

def ensure_hash(variables)
if variables.blank?
{}
elsif variables.is_a?(String)
JSON.parse(variables)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just had a random thought, this could be an array too.

Copy link
Copy Markdown
Collaborator Author

@xuorig xuorig Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variables.is_a?(String) no ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i see what you mean

else
variables
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do anything here? It could be an array, maybe we should Bad Request in that case?

end
end
end
8 changes: 0 additions & 8 deletions app/graph/query.rb

This file was deleted.

9 changes: 9 additions & 0 deletions app/models/graph/query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Graph
Query = GraphQL::ObjectType.define do
name "Query"
description "The query root of this schema"

# TODO(xuorig) Populate with real fields (GraphiQL crashes with no fields on root)
field :test, types.String, resolve: ->(_, _, _) { 'test' }
end
end
File renamed without changes.
1 change: 0 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
config.autoload_paths << Rails.root.join('app', 'graph')
end
end
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
if Rails.env.development?
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
end

post '/graphql', to: 'graphql#execute'
end