Andrew Swistak 6 anni fa
parent
commit
0e8e3d9174

+ 2 - 0
Gemfile

@@ -8,6 +8,7 @@ ruby '2.6.3'
 gem 'bootsnap', '>= 1.4.1', require: false
 gem 'listen', '>= 3.0.5', '< 3.2' # requirement of bootsnap
 
+gem 'graphql'
 gem 'haml'
 gem 'jbuilder', '~> 2.5'
 gem 'pg'
@@ -29,6 +30,7 @@ group :development, :test do
   gem 'dotenv-rails'
   gem 'factory_bot_rails'
   gem 'faker'
+  gem 'graphiql-rails'
   gem 'pry'
   gem 'pry-rails'
   gem 'rails-controller-testing'

+ 6 - 0
Gemfile.lock

@@ -142,6 +142,10 @@ GEM
     ffi (1.10.0)
     globalid (0.4.2)
       activesupport (>= 4.2.0)
+    graphiql-rails (1.7.0)
+      railties
+      sprockets-rails
+    graphql (1.9.4)
     haml (5.0.4)
       temple (>= 0.8.0)
       tilt
@@ -314,6 +318,8 @@ DEPENDENCIES
   dotenv-rails
   factory_bot_rails
   faker
+  graphiql-rails
+  graphql
   haml
   jbuilder (~> 2.5)
   listen (>= 3.0.5, < 3.2)

+ 46 - 0
app/controllers/graphql_controller.rb

@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+class GraphqlController < ApplicationController
+  def execute
+    variables = ensure_hash(params[:variables])
+    query = params[:query]
+    operation_name = params[:operationName]
+    context = {
+      # Query context goes here, for example:
+      # current_user: current_user,
+    }
+    result = PokemonTradeSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
+    render json: result
+  rescue StandardError => e
+    raise e unless Rails.env.development?
+
+    handle_error_in_development(e)
+  end
+
+  private
+
+    # Handle form data, JSON body, or a blank value
+    def ensure_hash(ambiguous_param)
+      case ambiguous_param
+      when String
+        if ambiguous_param.present?
+          ensure_hash(JSON.parse(ambiguous_param))
+        else
+          {}
+        end
+      when Hash, ActionController::Parameters
+        ambiguous_param
+      when nil
+        {}
+      else
+        raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
+      end
+    end
+
+    def handle_error_in_development(err)
+      logger.error err.message
+      logger.error err.backtrace.join("\n")
+
+      render json: {error: {message: err.message, backtrace: err.backtrace}, data: {}}, status: :internal_server_error
+    end
+end

+ 0 - 0
app/graphql/mutations/.keep


+ 4 - 0
app/graphql/pokemon_trade_schema.rb

@@ -0,0 +1,4 @@
+class PokemonTradeSchema < GraphQL::Schema
+  mutation(Types::MutationType)
+  query(Types::QueryType)
+end

+ 0 - 0
app/graphql/types/.keep


+ 4 - 0
app/graphql/types/base_enum.rb

@@ -0,0 +1,4 @@
+module Types
+  class BaseEnum < GraphQL::Schema::Enum
+  end
+end

+ 4 - 0
app/graphql/types/base_input_object.rb

@@ -0,0 +1,4 @@
+module Types
+  class BaseInputObject < GraphQL::Schema::InputObject
+  end
+end

+ 5 - 0
app/graphql/types/base_interface.rb

@@ -0,0 +1,5 @@
+module Types
+  module BaseInterface
+    include GraphQL::Schema::Interface
+  end
+end

+ 4 - 0
app/graphql/types/base_object.rb

@@ -0,0 +1,4 @@
+module Types
+  class BaseObject < GraphQL::Schema::Object
+  end
+end

+ 4 - 0
app/graphql/types/base_scalar.rb

@@ -0,0 +1,4 @@
+module Types
+  class BaseScalar < GraphQL::Schema::Scalar
+  end
+end

+ 4 - 0
app/graphql/types/base_union.rb

@@ -0,0 +1,4 @@
+module Types
+  class BaseUnion < GraphQL::Schema::Union
+  end
+end

+ 10 - 0
app/graphql/types/mutation_type.rb

@@ -0,0 +1,10 @@
+module Types
+  class MutationType < Types::BaseObject
+    # TODO: remove me
+    field :test_field, String, null: false,
+      description: "An example field added by the generator"
+    def test_field
+      "Hello World"
+    end
+  end
+end

+ 13 - 0
app/graphql/types/query_type.rb

@@ -0,0 +1,13 @@
+module Types
+  class QueryType < Types::BaseObject
+    # Add root-level fields here.
+    # They will be entry points for queries on your schema.
+
+    # TODO: remove me
+    field :test_field, String, null: false,
+      description: "An example field added by the generator"
+    def test_field
+      "Hello World!"
+    end
+  end
+end

+ 2 - 1
config/routes.rb

@@ -1,7 +1,8 @@
 # frozen_string_literal: true
 
 Rails.application.routes.draw do
-  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
+  mount GraphiQL::Rails::Engine, at: '/graphiql', graphql_path: '/graphql' if Rails.env.development?
+  post '/graphql', to: 'graphql#execute'
 
   root to: 'welcome#root'