Skip to content

Commit fb14b42

Browse files
BajenaBajena
andauthored
graphql-ruby 2.3 compatibility (#282)
Co-authored-by: Bajena <[email protected]>
1 parent 5bc0775 commit fb14b42

File tree

14 files changed

+541
-284
lines changed

14 files changed

+541
-284
lines changed

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.7.5
1+
3.2.0

Gemfile.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ PLATFORMS
138138
arm64-darwin-21
139139
arm64-darwin-22
140140
arm64-darwin-23
141+
arm64-darwin-24
141142
x86_64-darwin-20
142143
x86_64-darwin-21
143144
x86_64-darwin-22
@@ -151,7 +152,7 @@ DEPENDENCIES
151152
mutex_m
152153
ostruct
153154
pry-byebug
154-
rack
155+
rack (~> 2.0)
155156
rackup
156157
rake
157158
rspec

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ end
362362

363363
To support [federated tracing](https://www.apollographql.com/docs/apollo-server/federation/metrics/):
364364

365-
1. Add `use ApolloFederation::Tracing` to your schema class.
365+
1. Add `trace_with ApolloFederation::Tracing::Tracer` to your schema class.
366366
2. Change your controller to add `tracing_enabled: true` to the execution context based on the presence of the "include trace" header:
367367
```ruby
368368
def execute

apollo-federation.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Gem::Specification.new do |spec|
3333
spec.add_development_dependency 'actionpack'
3434
spec.add_development_dependency 'debug'
3535
spec.add_development_dependency 'pry-byebug'
36-
spec.add_development_dependency 'rack'
36+
spec.add_development_dependency 'rack', '~> 2.0'
3737
spec.add_development_dependency 'rackup'
3838
spec.add_development_dependency 'rake'
3939
spec.add_development_dependency 'rspec'

example/accounts.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ class AccountSchema < GraphQL::Schema
5252
use GraphQL::Execution::Interpreter
5353
use GraphQL::Analysis::AST
5454
end
55-
use ApolloFederation::Tracing
55+
56+
if Gem::Version.new(GraphQL::VERSION) < Gem::Version.new('2.3.0')
57+
use ApolloFederation::Tracing
58+
else
59+
trace_with ApolloFederation::Tracing::Tracer
60+
end
5661

5762
include ApolloFederation::Schema
5863

example/inventory.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ class InventorySchema < GraphQL::Schema
4646
use GraphQL::Execution::Interpreter
4747
use GraphQL::Analysis::AST
4848
end
49-
use ApolloFederation::Tracing
49+
50+
if Gem::Version.new(GraphQL::VERSION) < Gem::Version.new('2.3.0')
51+
use ApolloFederation::Tracing
52+
else
53+
trace_with ApolloFederation::Tracing::Tracer
54+
end
5055

5156
include ApolloFederation::Schema
5257

example/products.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ class ProductSchema < GraphQL::Schema
6262
use GraphQL::Execution::Interpreter
6363
use GraphQL::Analysis::AST
6464
end
65-
use ApolloFederation::Tracing
65+
66+
if Gem::Version.new(GraphQL::VERSION) < Gem::Version.new('2.3.0')
67+
use ApolloFederation::Tracing
68+
else
69+
trace_with ApolloFederation::Tracing::Tracer
70+
end
6671

6772
include ApolloFederation::Schema
6873

example/reviews.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ class ReviewSchema < GraphQL::Schema
100100
use GraphQL::Execution::Interpreter
101101
use GraphQL::Analysis::AST
102102
end
103-
use ApolloFederation::Tracing
103+
104+
if Gem::Version.new(GraphQL::VERSION) < Gem::Version.new('2.3.0')
105+
use ApolloFederation::Tracing
106+
else
107+
trace_with ApolloFederation::Tracing::Tracer
108+
end
104109

105110
include ApolloFederation::Schema
106111

lib/apollo-federation/federated_document_from_schema_definition.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def build_field_node(field_type)
6969
def build_type_definition_nodes(types)
7070
non_federation_types = types.select do |type|
7171
if query_type?(type)
72-
!warden.fields(type).all? { |field| FEDERATION_QUERY_FIELDS.include?(field.graphql_name) }
72+
!fields_for_type(type).all? { |field| FEDERATION_QUERY_FIELDS.include?(field.graphql_name) }
7373
else
7474
!FEDERATION_TYPES.include?(type.graphql_name)
7575
end
@@ -79,8 +79,23 @@ def build_type_definition_nodes(types)
7979

8080
private
8181

82+
def fields_for_type(type)
83+
return warden.fields(type) if use_warden?
84+
85+
@types.fields(type)
86+
end
87+
8288
def query_type?(type)
83-
type == warden.root_type_for_operation('query')
89+
return type == warden.root_type_for_operation('query') if use_warden?
90+
91+
type == @types.query_root
92+
end
93+
94+
# graphql-ruby 2.3.8 removed the warden definition from a number of places:
95+
# https://github.com/rmosolgo/graphql-ruby/compare/v2.3.7...v2.3.8#diff-e9dd0d295a58b66fabd1cf717040de5a78ade0feac6aeabae5247b5785c029ab
96+
# The internal usage of `warden` was replaced with `types` in many cases.
97+
def use_warden?
98+
Gem::Version.new(GraphQL::VERSION) < Gem::Version.new('2.3.8')
8499
end
85100

86101
def merge_directives(node, type)

lib/apollo-federation/tracing/tracer.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,40 @@ module Tracer
4040
EXECUTE_FIELD = 'execute_field'
4141
EXECUTE_FIELD_LAZY = 'execute_field_lazy'
4242

43+
def execute_multiplex(multiplex:)
44+
ApolloFederation::Tracing::Tracer.execute_multiplex(multiplex: multiplex) { yield }
45+
end
46+
47+
def execute_query_lazy(query:, multiplex:)
48+
ApolloFederation::Tracing::Tracer.execute_query_lazy(query: query, multiplex: multiplex) { yield }
49+
end
50+
51+
def execute_field(field:, query:, ast_node:, arguments:, object:)
52+
ApolloFederation::Tracing::Tracer.execute_field(
53+
field: field,
54+
query: query,
55+
ast_node: ast_node,
56+
arguments: arguments,
57+
object: object,
58+
owner: field.owner,
59+
path: query.context[:current_path],
60+
) { yield }
61+
end
62+
63+
def execute_field_lazy(field:, query:, ast_node:, arguments:, object:)
64+
ApolloFederation::Tracing::Tracer.execute_field_lazy(
65+
field: field,
66+
query: query,
67+
ast_node: ast_node,
68+
arguments: arguments,
69+
object: object,
70+
owner: field.owner,
71+
path: query.context[:current_path],
72+
) { yield }
73+
end
74+
75+
# The method below was used by older versions of graphql-ruby.
76+
# After graphq-ruby 2.3.0 the trace modules should respond to specific methods such as `execute_field` above.
4377
def self.trace(key, data, &block)
4478
case key
4579
when EXECUTE_MULTIPLEX

0 commit comments

Comments
 (0)