Skip to content

Commit

Permalink
Add test for interface-level resolve_type
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Jul 12, 2017
1 parent 4cc83f8 commit 653e5b9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
44 changes: 44 additions & 0 deletions spec/graphql/interface_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,48 @@
assert_equal 4, interface_2.fields.size
end
end

describe "#resolve_type" do
let(:result) { Dummy::Schema.execute(query_string) }
let(:query_string) {%|
{
allEdible {
__typename
... on Milk {
milkFatContent: fatContent
}
... on Cheese {
cheeseFatContent: fatContent
}
}
allEdibleAsMilk {
__typename
... on Milk {
fatContent
}
}
}
|}

it 'returns correct types for general schema and specific interface' do
expected_result = {
# Uses schema-level resolve_type
"allEdible"=>[
{"__typename"=>"Cheese", "cheeseFatContent"=>0.19},
{"__typename"=>"Cheese", "cheeseFatContent"=>0.3},
{"__typename"=>"Cheese", "cheeseFatContent"=>0.065},
{"__typename"=>"Milk", "milkFatContent"=>0.04}
],
# Uses type-level resolve_type
"allEdibleAsMilk"=>[
{"__typename"=>"Milk", "fatContent"=>0.19},
{"__typename"=>"Milk", "fatContent"=>0.3},
{"__typename"=>"Milk", "fatContent"=>0.065},
{"__typename"=>"Milk", "fatContent"=>0.04}
]
}
assert_equal expected_result, result["data"]
end
end
end
5 changes: 5 additions & 0 deletions spec/support/dummy/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ def ==(other)
# This is buggy on purpose -- it shouldn't be called during execution.
other.id == id
end

# Alias for when this is treated as milk in EdibleAsMilkInterface
def fatContent
fat_content
end
end

CHEESES = {
Expand Down
7 changes: 2 additions & 5 deletions spec/support/dummy/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ class NoSuchDairyError < StandardError; end
EdibleAsMilkInterface = EdibleInterface.redefine do
name "EdibleAsMilk"
description "Milk :+1:"
field :fatContent, !types.Float, "Percentage which is fat"
field :origin, !types.String, "Place the edible comes from"
field :selfAsEdible, EdibleInterface, resolve: ->(o, a, c) { o }
resolve_type ->(obj, ctx) { MilkType }
end

Expand Down Expand Up @@ -57,7 +54,7 @@ class NoSuchDairyError < StandardError; end
name "Cheese"
class_names ["Cheese"]
description "Cultured dairy product"
interfaces [EdibleInterface, AnimalProductInterface, LocalProductInterface]
interfaces [EdibleInterface, EdibleAsMilkInterface, AnimalProductInterface, LocalProductInterface]

# Can have (name, type, desc)
field :id, !types.Int, "Unique identifier"
Expand Down Expand Up @@ -105,7 +102,7 @@ class NoSuchDairyError < StandardError; end
MilkType = GraphQL::ObjectType.define do
name "Milk"
description "Dairy beverage"
interfaces [EdibleInterface, AnimalProductInterface, LocalProductInterface]
interfaces [EdibleInterface, EdibleAsMilkInterface, AnimalProductInterface, LocalProductInterface]
field :id, !types.ID
field :source, !DairyAnimalEnum, "Animal which produced this milk", hash_key: :source
field :origin, !types.String, "Place the milk comes from"
Expand Down

0 comments on commit 653e5b9

Please sign in to comment.