Skip to content

Commit ff56479

Browse files
committed
rails g graphql:install
1 parent c320348 commit ff56479

File tree

9 files changed

+70
-0
lines changed

9 files changed

+70
-0
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ gem 'exception_notification'
1212
gem 'faraday'
1313
gem 'font-awesome-rails'
1414
gem 'google-analytics-rails'
15+
gem 'graphiql-rails', group: :development
1516
gem 'graphql'
1617
gem 'hamlit'
1718
gem 'jquery-rails'

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ GEM
105105
globalid (0.4.0)
106106
activesupport (>= 4.2.0)
107107
google-analytics-rails (1.1.1)
108+
graphiql-rails (1.4.4)
109+
rails
108110
graphql (1.6.8)
109111
hamlit (2.8.4)
110112
temple (>= 0.8.0)
@@ -318,6 +320,7 @@ DEPENDENCIES
318320
font-awesome-rails
319321
foreman
320322
google-analytics-rails
323+
graphiql-rails
321324
graphql
322325
hamlit
323326
jquery-rails

app/controllers/graphql_controller.rb

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 = GithubRankingSchema.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

app/graphql/github_ranking_schema.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
GithubRankingSchema = 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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Types::MutationType = GraphQL::ObjectType.define do
2+
name "Mutation"
3+
4+
# TODO: Remove me
5+
field :testField, types.String do
6+
description "An example field added by the generator"
7+
resolve ->(obj, args, ctx) {
8+
"Hello World!"
9+
}
10+
end
11+
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,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
root to: 'top#index'
38

49
authenticate :user, ->(u) { u.admin? } do

0 commit comments

Comments
 (0)