Description
Hi! I'm not sure if this is a bug, or just a case where I didn't understand the documentation properly but this is a problem I've been running into any time I use union types, though I think it would also apply for interfaces.
When I add a union type to my schema, it fails to validate until I add a resolve_type
proc to the schema definition. I would have expected that to be unnecessary as long as I defined a resolve_type
proc on the union type itself, which I did. Here's a quick failing ruby script:
require 'graphql'
module This
extend self
def hello
"Hi, from This!"
end
end
module That
extend self
def hello
"Bonjour from That!"
end
end
ThisType = GraphQL::ObjectType.define do
name "This"
field :hello, !types.String
end
ThatType = GraphQL::ObjectType.define do
name "That"
field :hello, !types.String
end
ThisOrThatType = GraphQL::UnionType.define do
name "ThisOrThat"
possible_types [-> { ThisType }, -> { ThatType }]
resolve_type -> (obj, _ctx) {
case obj
when This then ThisType
when That then ThatType
end
}
end
QueryType = GraphQL::ObjectType.define do
name "Query"
field :thisOrThat, -> { ThisOrThatType } do
resolve -> (_, _, _) {
[This, That].sample
}
end
end
Schema = GraphQL::Schema.define do
query QueryType
# resolve_type -> (_,_,_) { } # uncomment this line and the schema will validate
end
From what I can tell, the docs around the union type feature don't say that you have to define a resolve_type
proc on schema, but that a resolve_type
proc must be defined either on the schema, or on the union type.
It seems to me like the schema should validate without the resolve_type
proc being defined at the schema level, especially since it appears to function fine when you stub it with a no-op proc.
Here's a query for testing out the above schema:
{
thisOrThat {
... on This { hello }
... on That { hello }
}
}