Kaynağa Gözat

Add working implementation of GraphQL

Andrew Swistak 6 yıl önce
ebeveyn
işleme
ed340103c7

+ 13 - 0
app/graphql/mutations/base_mutation.rb

@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class Mutations::BaseMutation < GraphQL::Schema::Mutation
+  # Add your custom classes if you have them:
+  # This is used for generating payload types
+  # object_class Types::BaseObject
+
+  # This is used for return fields on the mutation's payload
+  # field_class Types::BaseField
+
+  # This is used for generating the `input: { ... }` object type
+  # input_object_class Types::BaseInputObject
+end

+ 25 - 0
app/graphql/mutations/create_pokemon.rb

@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+class Mutations::CreatePokemon < Mutations::BaseMutation
+  graphql_name 'CreatePokemon'
+  argument :pokedex_number, Int, required: true
+  argument :nickname, String, required: false
+
+  field :pokemon, Types::PokemonType, null: true
+  field :errors, [String], null: false
+
+  def resolve(nickname: nil, pokedex_number:)
+    pkmn = Pokemon.new(nickname: nickname, pokedex_number: pokedex_number)
+    if pkmn.save
+      {
+        pokemon: pkmn,
+        errors: [],
+      }
+    else
+      {
+        pokemon: nil,
+        errors: pkmn.errors.full_messages,
+      }
+    end
+  end
+end

+ 2 - 0
app/graphql/pokemon_trade_schema.rb

@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
 class PokemonTradeSchema < GraphQL::Schema
   mutation(Types::MutationType)
   query(Types::QueryType)

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

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

+ 6 - 0
app/graphql/types/base_field.rb

@@ -0,0 +1,6 @@
+# frozen_string_literal: true
+
+module Types
+  class BaseField < GraphQL::Schema::Field
+  end
+end

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

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

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

@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
 module Types
   module BaseInterface
     include GraphQL::Schema::Interface

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

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

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

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

+ 4 - 9
app/graphql/types/mutation_type.rb

@@ -1,10 +1,5 @@
-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
+# frozen_string_literal: true
+
+class Types::MutationType < Types::BaseObject
+  field :create_pokemon, mutation: Mutations::CreatePokemon
 end

+ 9 - 0
app/graphql/types/pokemon_type.rb

@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+class Types::PokemonType < Types::BaseObject
+  graphql_name 'PokemonType'
+
+  field :id, ID, null: false
+  field :nickname, String, null: true
+  field :pokedex_number, Int, null: true
+end

+ 22 - 11
app/graphql/types/query_type.rb

@@ -1,13 +1,24 @@
-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
+# frozen_string_literal: true
+
+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
 end