Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
Last active October 11, 2022 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmosolgo/52c93aa6aa067eb8f2799024cbbc50c6 to your computer and use it in GitHub Desktop.
Save rmosolgo/52c93aa6aa067eb8f2799024cbbc50c6 to your computer and use it in GitHub Desktop.
Spreading an inteface fragment inside a union
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