From b1184fe590be795412e234078c7ba2d61326e800 Mon Sep 17 00:00:00 2001 From: Thomas Marshall Date: Sun, 7 Apr 2024 04:53:08 +0100 Subject: [PATCH] Fix hydration of boundaries with abstract types (#130) --- lib/graphql/stitching/supergraph.rb | 3 ++- .../stitching/supergraph/resolver_directive.rb | 1 + test/graphql/stitching/supergraph_test.rb | 14 +++++++++----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/graphql/stitching/supergraph.rb b/lib/graphql/stitching/supergraph.rb index 88247841..aa3c70d5 100644 --- a/lib/graphql/stitching/supergraph.rb +++ b/lib/graphql/stitching/supergraph.rb @@ -31,7 +31,7 @@ def from_definition(schema, executables:) kwargs = directive.arguments.keyword_arguments boundary_map[type_name] ||= [] boundary_map[type_name] << Boundary.new( - type_name: type_name, + type_name: kwargs.fetch(:type_name, type_name), location: kwargs[:location], key: kwargs[:key], field: kwargs[:field], @@ -134,6 +134,7 @@ def to_definition end type.directive(ResolverDirective, **{ + type_name: (boundary.type_name if boundary.type_name != type_name), location: boundary.location, key: boundary.key, field: boundary.field, diff --git a/lib/graphql/stitching/supergraph/resolver_directive.rb b/lib/graphql/stitching/supergraph/resolver_directive.rb index eeaa9501..4214325b 100644 --- a/lib/graphql/stitching/supergraph/resolver_directive.rb +++ b/lib/graphql/stitching/supergraph/resolver_directive.rb @@ -5,6 +5,7 @@ class Supergraph class ResolverDirective < GraphQL::Schema::Directive graphql_name "resolver" locations OBJECT, INTERFACE, UNION + argument :type_name, String, required: false argument :location, String, required: true argument :key, String, required: true argument :field, String, required: true diff --git a/test/graphql/stitching/supergraph_test.rb b/test/graphql/stitching/supergraph_test.rb index dde7a45b..f0bf0777 100644 --- a/test/graphql/stitching/supergraph_test.rb +++ b/test/graphql/stitching/supergraph_test.rb @@ -314,8 +314,9 @@ def test_route_type_to_locations_returns_nil_for_unreachable_locations describe "#to_definition / #from_definition" do def setup alpha = %| - type T { id:ID! a:String } - type Query { a(id:ID!):T @stitch(key: "id") } + type T implements A { id:ID! a:String } + interface A { id:ID! } + type Query { a(id:ID!):A @stitch(key: "id") } | bravo = %| type T { id:ID! b:String } @@ -331,13 +332,16 @@ def test_to_definition_annotates_schema assert @schema_sdl.include?("directive @resolver") assert @schema_sdl.include?("directive @source") assert @schema_sdl.include?(squish_string(%| - type T @resolver(location: "alpha", key: "id", field: "a", arg: "id") - @resolver(location: "bravo", key: "id", field: "b", arg: "id") { + interface A @resolver(location: "alpha", key: "id", field: "a", arg: "id") { + |)) + assert @schema_sdl.include?(squish_string(%| + type T implements A @resolver(location: "bravo", key: "id", field: "b", arg: "id") + @resolver(typeName: "A", location: "alpha", key: "id", field: "a", arg: "id") { |)) assert @schema_sdl.include?(%|id: ID! @source(location: "alpha") @source(location: "bravo")|) assert @schema_sdl.include?(%|a: String @source(location: "alpha")|) assert @schema_sdl.include?(%|b: String @source(location: "bravo")|) - assert @schema_sdl.include?(%|a(id: ID!): T @source(location: "alpha")|) + assert @schema_sdl.include?(%|a(id: ID!): A @source(location: "alpha")|) assert @schema_sdl.include?(%|b(id: ID!): T @source(location: "bravo")|) end