Last active
October 11, 2022 12:56
-
-
Save rmosolgo/52c93aa6aa067eb8f2799024cbbc50c6 to your computer and use it in GitHub Desktop.
Spreading an inteface fragment inside a union
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bundler/inline" | |
gemfile do | |
gem "graphql", "1.11.2" | |
end | |
class Schema < GraphQL::Schema | |
class Error < GraphQL::Schema::Object | |
field :message, String, null: false | |
end | |
module Node | |
include GraphQL::Schema::Interface | |
field :id, ID, null: false | |
end | |
class Thing < GraphQL::Schema::Object | |
implements Node | |
field :name, String, null: false | |
end | |
class ThingOrError < GraphQL::Schema::Union | |
possible_types Thing, Error | |
end | |
class Query < GraphQL::Schema::Object | |
field :thing_or_error, ThingOrError, null: false | |
def thing_or_error | |
{ name: "something", id: "1" } | |
end | |
end | |
query(Query) | |
use GraphQL::Execution::Interpreter | |
use GraphQL::Analysis::AST | |
def self.resolve_type(type, obj, ctx) | |
Thing | |
end | |
end | |
pp Schema.execute(<<-GRAPHQL).to_h | |
{ | |
thingOrError { | |
... on Thing { | |
name | |
} | |
... on Node { | |
id | |
} | |
} | |
} | |
GRAPHQL | |
# {"data"=>{"thingOrError"=>{"name"=>"something", "id"=>"1"}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment