Browse Source

Default to GraphQL API

Andrew Swistak 6 years ago
parent
commit
7c9c62202f

+ 1 - 1
app/controllers/api/v1/application_controller.rb → app/controllers/api/application_controller.rb

@@ -2,7 +2,7 @@
 
 require './lib/api_error/base_error'
 
-class API::V1::ApplicationController < ActionController::API
+class API::ApplicationController < ActionController::API
   before_action :set_default_response_format
 
   rescue_from StandardError, with: :handle_unhandled_api_error

+ 1 - 1
app/controllers/graphql_controller.rb → app/controllers/api/graphql_controller.rb

@@ -1,6 +1,6 @@
 # frozen_string_literal: true
 
-class GraphqlController < ApplicationController
+class API::GraphqlController < API::ApplicationController
   def execute
     variables = ensure_hash(params[:variables])
     query = params[:query]

+ 3 - 3
app/graphql/mutations/create_pokemon.rb

@@ -2,11 +2,11 @@
 
 class Mutations::CreatePokemon < Mutations::BaseMutation
   graphql_name 'CreatePokemon'
-  argument :pokedex_number, Int, required: true
-  argument :nickname, String, required: false
+  argument :pokedex_number, GraphQL::ID_TYPE, required: true
+  argument :nickname, GraphQL::STRING_TYPE, required: false
 
   field :pokemon, Types::PokemonType, null: true
-  field :errors, [String], null: false
+  field :errors, [GraphQL::STRING_TYPE], null: false
 
   def resolve(nickname: nil, pokedex_number:)
     pkmn = Pokemon.new(nickname: nickname, pokedex_number: pokedex_number)

+ 13 - 0
app/graphql/resolvers/base_resolver.rb

@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+module Resolvers
+  class BaseResolver < GraphQL::Schema::Resolver
+    def self.single
+      @single ||= Class.new(self) do
+        def resolve(**args)
+          super.first
+        end
+      end
+    end
+  end
+end

+ 13 - 0
app/graphql/resolvers/pokemon_resolver.rb

@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+module Resolvers
+  class PokemonResolver < BaseResolver
+    argument :id, GraphQL::ID_TYPE, required: false
+    argument :pokedex_number, GraphQL::INT_TYPE, required: false
+    argument :nickname, GraphQL::STRING_TYPE, required: false
+
+    def resolve(**args)
+      Pokemon.where(args)
+    end
+  end
+end

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

@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
 module Types
   class BaseUnion < GraphQL::Schema::Union
   end

+ 7 - 5
app/graphql/types/pokemon_type.rb

@@ -1,9 +1,11 @@
 # frozen_string_literal: true
 
-class Types::PokemonType < Types::BaseObject
-  graphql_name 'PokemonType'
+module Types
+  class PokemonType < Types::BaseObject
+    graphql_name 'PokemonType'
 
-  field :id, ID, null: false
-  field :nickname, String, null: true
-  field :pokedex_number, Int, null: true
+    field :id, GraphQL::ID_TYPE, null: false
+    field :nickname, GraphQL::STRING_TYPE, null: true
+    field :pokedex_number, GraphQL::INT_TYPE, null: true
+  end
 end

+ 9 - 18
app/graphql/types/query_type.rb

@@ -3,22 +3,13 @@
 class Types::QueryType < Types::BaseObject
   description 'The root of this schema'
 
-  field :pokemon, Types::PokemonType, null: true do
-    description 'Find a pokemon by ID'
-    argument :id, ID, required: false
-    argument :pokedex_number, Int, required: false
-  end
-
-  field :pokemons, [Types::PokemonType], null: true do
-    description 'Find a pokemon by ID'
-    argument :pokedex_number, Int, required: false
-  end
-
-  def pokemon(args)
-    Pokemon.find_by(args)
-  end
-
-  def pokemons(args = {})
-    Pokemon.where(args)
-  end
+  field :pokemon,
+        Types::PokemonType,
+        null: true,
+        resolver: Resolvers::PokemonResolver.single
+
+  field :many_pokemon,
+        [Types::PokemonType],
+        null: true,
+        resolver: Resolvers::PokemonResolver
 end

+ 4 - 3
config/routes.rb

@@ -1,14 +1,15 @@
 # frozen_string_literal: true
 
 Rails.application.routes.draw do
-  mount GraphiQL::Rails::Engine, at: '/graphiql', graphql_path: '/graphql' if Rails.env.development?
-  post '/graphql', to: 'graphql#execute'
+  mount GraphiQL::Rails::Engine, at: '/-/graphiql', graphql_path: '/api/graphql' if Rails.env.development?
 
   root to: 'welcome#root'
 
   namespace :api, defaults: {formats: :json} do
+    post '/graphql', to: 'graphql#execute'
+
     namespace :v1 do
-      resources :pokemon do
+      resources :pokemon, only: [] do
         collection do
           post :upload
         end