Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Add entity interface support #265

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ source 'https://rubygems.org'

gemspec

# Fixes an issue in ruby 3.2, remove when a new version is published
gem 'appraisal', git: 'https://github.com/thoughtbot/appraisal', ref: 'ea2baff57bc7f9e5f9e7a648916c9ae536325116'
gem 'appraisal', '~> 2.5.0'
gem 'graphql', '~> 2.0.0'
27 changes: 12 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
GIT
remote: https://github.com/thoughtbot/appraisal
revision: ea2baff57bc7f9e5f9e7a648916c9ae536325116
ref: ea2baff57bc7f9e5f9e7a648916c9ae536325116
specs:
appraisal (2.4.1)
bundler
rake
thor (>= 0.14.0)

PATH
remote: .
specs:
Expand Down Expand Up @@ -36,6 +26,10 @@ GEM
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
appraisal (2.5.0)
bundler
rake
thor (>= 0.14.0)
ast (2.4.2)
builder (3.2.4)
byebug (11.1.3)
Expand All @@ -47,9 +41,7 @@ GEM
debase-ruby_core_source (3.2.0)
diff-lcs (1.5.0)
erubi (1.12.0)
google-protobuf (3.22.2-arm64-darwin)
google-protobuf (3.22.2-x86_64-darwin)
google-protobuf (3.22.2-x86_64-linux)
google-protobuf (3.22.2)
graphql (2.0.19)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
Expand All @@ -58,7 +50,11 @@ GEM
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
method_source (1.0.0)
mini_portile2 (2.8.4)
minitest (5.18.0)
nokogiri (1.14.2)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
nokogiri (1.14.2-arm64-darwin)
racc (~> 1.4)
nokogiri (1.14.2-x86_64-darwin)
Expand Down Expand Up @@ -110,7 +106,7 @@ GEM
ruby-debug-ide (0.7.3)
rake (>= 0.8.1)
ruby-progressbar (1.13.0)
thor (1.2.1)
thor (1.2.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (1.6.1)
Expand All @@ -119,6 +115,7 @@ GEM
PLATFORMS
arm64-darwin-21
arm64-darwin-22
ruby
x86_64-darwin-20
x86_64-darwin-21
x86_64-darwin-22
Expand All @@ -127,7 +124,7 @@ PLATFORMS
DEPENDENCIES
actionpack
apollo-federation!
appraisal!
appraisal (~> 2.5.0)
debase (= 0.2.5.beta2)
graphql (~> 2.0.0)
pry-byebug
Expand Down
11 changes: 7 additions & 4 deletions lib/apollo-federation/entities_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@ def _entities(representations:)

# TODO: Use warden or schema?
type = context.warden.get_type(typename)
if type.nil? || type.kind != GraphQL::TypeKinds::OBJECT
if type.nil? || ![GraphQL::TypeKinds::OBJECT, GraphQL::TypeKinds::INTERFACE].include?(type.kind)
# TODO: Raise a specific error class?
raise "The _entities resolver tried to load an entity for type \"#{typename}\"," \
' but no object type of that name was found in the schema'
end

# TODO: What if the type is an interface?
type_class = class_of_type(type)

if type_class.underscore_reference_keys
if type_class.respond_to?(:underscore_reference_keys) && type_class.underscore_reference_keys
references.map! do |reference|
reference.transform_keys do |key|
GraphQL::Schema::Member::BuildType.underscore(key.to_s).to_sym
Expand All @@ -72,7 +71,11 @@ def _entities(representations:)
# calls return the same object, it might not have the right type
# Right now, apollo-federation just adds a __typename property to the result,
# but I don't really like the idea of modifying the resolved object
context[resolved_value] = type
if type.kind == GraphQL::TypeKinds::INTERFACE
context[resolved_value] = context.schema.resolve_type(type, resolved_value, context)
else
context[resolved_value] = type
end
resolved_value
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/apollo-federation/entities_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def self.resolve_type(_abstract_type, _obj, _ctx)
let(:typename) { 'TypeNotInSchema' }

it 'raises' do
expect(-> { execute_query }).to raise_error(
expect { execute_query }.to raise_error(
/The _entities resolver tried to load an entity for type "TypeNotInSchema"/,
)
end
Expand Down