spec/support/dummy/schema.rb in graphql-1.8.18 vs spec/support/dummy/schema.rb in graphql-1.9.0.pre1

- old
+ new

@@ -268,72 +268,77 @@ def self.coerce_result(value, ctx) value.to_f end end - class FetchItem < GraphQL::Function - attr_reader :type, :description, :arguments + class FetchItem < GraphQL::Schema::Resolver + class << self + attr_accessor :data + end - def initialize(type:, data:, id_type: !GraphQL::INT_TYPE) - @type = type - @data = data - @description = "Find a #{type.name} by id" - @arguments = self.class.arguments.merge({"id" => GraphQL::Argument.define(name: "id", type: id_type)}) + def self.build(type:, data:, id_type: "Int") + Class.new(self) do + self.data = data + type(type, null: true) + description("Find a #{type.name} by id") + argument :id, id_type, required: true + end end - def call(obj, args, ctx) - id_string = args["id"].to_s # Cheese has Int type, Milk has ID type :( - _id, item = @data.find { |id, _item| id.to_s == id_string } + def resolve(id:) + id_string = id.to_s # Cheese has Int type, Milk has ID type :( + _id, item = self.class.data.find { |item_id, _item| item_id.to_s == id_string } item end end - class GetSingleton < GraphQL::Function - attr_reader :description, :type + class GetSingleton < GraphQL::Schema::Resolver + class << self + attr_accessor :data + end - def initialize(type:, data:) - @description = "Find the only #{type.name}" - @type = type - @data = data + def self.build(type:, data:) + Class.new(self) do + description("Find the only #{type.name}") + type(type, null: true) + self.data = data + end end - def call(obj, args, ctx) - @data + def resolve + self.class.data end end - FavoriteFieldDefn = GraphQL::Field.define do - name "favoriteEdible" - description "My favorite food" - type Edible - resolve ->(t, a, c) { MILKS[1] } - end - class DairyAppQuery < BaseObject graphql_name "Query" description "Query root of the system" # Returns `root_value:` field :root, String, null: true def root object end - field :cheese, function: FetchItem.new(type: Cheese, data: CHEESES) - field :milk, function: FetchItem.new(type: Milk, data: MILKS, id_type: GraphQL::Types::ID.to_non_null_type) - field :dairy, function: GetSingleton.new(type: Dairy, data: DAIRY) + field :cheese, resolver: FetchItem.build(type: Cheese, data: CHEESES) + field :milk, resolver: FetchItem.build(type: Milk, data: MILKS, id_type: "ID") + field :dairy, resolver: GetSingleton.build(type: Dairy, data: DAIRY) field :from_source, [Cheese, null: true], null: true, description: "Cheese from source" do argument :source, DairyAnimal, required: false, default_value: 1 end def from_source(source:) CHEESES.values.select { |c| c.source == source } end - field :favorite_edible, field: FavoriteFieldDefn - field :cow, function: GetSingleton.new(type: Cow, data: COWS[1]) + field :favorite_edible, Edible, null: true, description: "My favorite food" + def favorite_edible + MILKS[1] + end + + field :cow, resolver: GetSingleton.build(type: Cow, data: COWS[1]) field :search_dairy, DairyProduct, null: false do description "Find dairy products matching a description" # This is a list just for testing 😬 - argument :product, [DairyProductInput, null: true], required: false, default_value: [{"source" => "SHEEP"}] + argument :product, [DairyProductInput, null: true], required: false, default_value: [{source: "SHEEP"}] argument :expires_after, Time, required: false end def search_dairy(product:, expires_after: nil) source = product[0][:source] @@ -443,11 +448,11 @@ argument :input, ReplaceValuesInput, required: true end def replace_values(input:) GLOBAL_VALUES.clear - GLOBAL_VALUES.concat(input["values"]) + GLOBAL_VALUES.concat(input[:values]) GLOBAL_VALUES end end class Subscription < BaseObject @@ -465,8 +470,11 @@ rescue_from(NoSuchDairyError) { |err| err.message } def self.resolve_type(type, obj, ctx) Schema.types[obj.class.name.split("::").last] + end + if TESTING_INTERPRETER + use GraphQL::Execution::Interpreter end end end