Skip to content

Commit 82a8164

Browse files
Remove lookahead hack (#109)
1 parent 8849784 commit 82a8164

File tree

10 files changed

+94
-66
lines changed

10 files changed

+94
-66
lines changed

.github/workflows/rspec.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ jobs:
1919
strategy:
2020
fail-fast: false
2121
matrix:
22-
ruby: ['3.0', 3.1, 3.2]
22+
ruby: ['3.0', 3.1, 3.3]
2323
gemfile: [
2424
"gemfiles/graphql_1_12_0.gemfile",
2525
"gemfiles/graphql_1_13_7.gemfile",
2626
"gemfiles/graphql_2_0_0.gemfile",
2727
"gemfiles/graphql_2_0_14.gemfile",
2828
"gemfiles/graphql_2_1_0.gemfile",
2929
"gemfiles/graphql_2_1_4.gemfile",
30+
"gemfiles/graphql_2_3_0.gemfile",
3031
"gemfiles/graphql_master.gemfile"
3132
]
3233

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Gemfile.lock
1010
.DS_Store
1111
.rbnext/
1212
*.sqlite3
13+
*.sqlite3-*
1314
spec/internal/log/*.log
1415
.idea/
1516
.ruby-version

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## master
44

5+
- [PR#109](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/109) Remove `Lookahead` patch in modern versions of graphql-ruby ([@DmitryTsepelev][])
6+
57
## 1.20.0 (2024-03-02)
68

79
- [PR#108](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/108) Use trace_with instead of deprecated instrument method ([@camero2734][])

gemfiles/graphql_2_3_0.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source "https://rubygems.org"
2+
3+
gem "graphql", "~> 2.3.0"
4+
5+
gemspec path: "../"

lib/graphql/fragment_cache.rb

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require "graphql"
44

5+
require "graphql/fragment_cache/graphql_ruby_version"
6+
57
require "graphql/fragment_cache/ext/context_fragments"
68
require "graphql/fragment_cache/ext/graphql_cache_key"
79
require "graphql/fragment_cache/object"
@@ -33,7 +35,7 @@ def use(schema_defn, options = {})
3335

3436
schema_defn.tracer(Schema::Tracer)
3537

36-
if graphql_ruby_after_2_2_5?
38+
if GraphRubyVersion.after_2_2_5?
3739
schema_defn.trace_with(GraphQL::Tracing::LegacyHooksTrace)
3840
schema_defn.instance_exec { own_instrumenters[:query] << Schema::Instrumentation }
3941
else
@@ -64,30 +66,14 @@ def cache_store=(store)
6466

6567
alias_method :skip_cache_when_query_has_errors?, :skip_cache_when_query_has_errors
6668

67-
def graphql_ruby_before_2_0?
68-
check_graphql_version "< 2.0.0"
69-
end
70-
71-
def graphql_ruby_after_2_0_13?
72-
check_graphql_version "> 2.0.13"
73-
end
74-
75-
def graphql_ruby_before_2_1_4?
76-
check_graphql_version "< 2.1.4"
77-
end
78-
79-
def graphql_ruby_after_2_2_5?
80-
check_graphql_version "> 2.2.5"
81-
end
82-
8369
private
8470

8571
def check_graphql_version(predicate)
8672
Gem::Dependency.new("graphql", predicate).match?("graphql", GraphQL::VERSION)
8773
end
8874

8975
def verify_interpreter_and_analysis!(schema_defn)
90-
if graphql_ruby_before_2_0?
76+
if GraphRubyVersion.before_2_0?
9177
unless schema_defn.interpreter?
9278
raise StandardError,
9379
"GraphQL::Execution::Interpreter should be enabled for fragment caching"

lib/graphql/fragment_cache/cache_key_builder.rb

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -53,64 +53,66 @@ def selection_with_alias(name, **kwargs)
5353
alias_selection(name, **kwargs)
5454
end
5555

56-
def alias_selection(name, selected_type: @selected_type, arguments: nil)
57-
return alias_selections[name] if alias_selections.key?(name)
56+
if GraphRubyVersion.before_2_1_4?
57+
def alias_selection(name, selected_type: @selected_type, arguments: nil)
58+
return alias_selections[name] if alias_selections.key?(name)
5859

59-
alias_node = lookup_alias_node(ast_nodes, name)
60-
return ::GraphQL::Execution::Lookahead::NULL_LOOKAHEAD unless alias_node
60+
alias_node = lookup_alias_node(ast_nodes, name)
61+
return ::GraphQL::Execution::Lookahead::NULL_LOOKAHEAD unless alias_node
6162

62-
next_field_name = alias_node.name
63+
next_field_name = alias_node.name
6364

64-
# From https://github.com/rmosolgo/graphql-ruby/blob/1a9a20f3da629e63ea8e5ee8400be82218f9edc3/lib/graphql/execution/lookahead.rb#L91
65-
next_field_defn =
66-
if GraphQL::FragmentCache.graphql_ruby_before_2_0?
67-
get_class_based_field(selected_type, next_field_name)
68-
else
69-
@query.get_field(selected_type, next_field_name)
70-
end
65+
# From https://github.com/rmosolgo/graphql-ruby/blob/1a9a20f3da629e63ea8e5ee8400be82218f9edc3/lib/graphql/execution/lookahead.rb#L91
66+
next_field_defn =
67+
if GraphRubyVersion.before_2_0?
68+
get_class_based_field(selected_type, next_field_name)
69+
else
70+
@query.get_field(selected_type, next_field_name)
71+
end
7172

72-
alias_selections[name] =
73-
if next_field_defn
74-
next_nodes = []
75-
arguments = @query.arguments_for(alias_node, next_field_defn)
76-
arguments = arguments.is_a?(::GraphQL::Execution::Interpreter::Arguments) ? arguments.keyword_arguments : arguments
77-
@ast_nodes.each do |ast_node|
78-
ast_node.selections.each do |selection|
79-
if GraphQL::FragmentCache.graphql_ruby_after_2_0_13? && GraphQL::FragmentCache.graphql_ruby_before_2_1_4?
80-
find_selected_nodes(selection, next_field_defn, arguments: arguments, matches: next_nodes)
81-
else
82-
find_selected_nodes(selection, next_field_name, next_field_defn, arguments: arguments, matches: next_nodes)
73+
alias_selections[name] =
74+
if next_field_defn
75+
next_nodes = []
76+
arguments = @query.arguments_for(alias_node, next_field_defn)
77+
arguments = arguments.is_a?(::GraphQL::Execution::Interpreter::Arguments) ? arguments.keyword_arguments : arguments
78+
@ast_nodes.each do |ast_node|
79+
ast_node.selections.each do |selection|
80+
if GraphRubyVersion.after_2_0_13? && GraphRubyVersion.before_2_1_4?
81+
find_selected_nodes(selection, next_field_defn, arguments: arguments, matches: next_nodes)
82+
else
83+
find_selected_nodes(selection, next_field_name, next_field_defn, arguments: arguments, matches: next_nodes)
84+
end
8385
end
8486
end
85-
end
8687

87-
if next_nodes.any?
88-
::GraphQL::Execution::Lookahead.new(query: @query, ast_nodes: next_nodes, field: next_field_defn, owner_type: selected_type)
88+
if next_nodes.any?
89+
::GraphQL::Execution::Lookahead.new(query: @query, ast_nodes: next_nodes, field: next_field_defn, owner_type: selected_type)
90+
else
91+
::GraphQL::Execution::Lookahead::NULL_LOOKAHEAD
92+
end
8993
else
9094
::GraphQL::Execution::Lookahead::NULL_LOOKAHEAD
9195
end
92-
else
93-
::GraphQL::Execution::Lookahead::NULL_LOOKAHEAD
94-
end
95-
end
96+
end
9697

97-
def alias_selections
98-
return @alias_selections if defined?(@alias_selections)
99-
@alias_selections ||= {}
100-
end
98+
def alias_selections
99+
return @alias_selections if defined?(@alias_selections)
100+
@alias_selections ||= {}
101+
end
101102

102-
def lookup_alias_node(nodes, name)
103-
return if nodes.empty?
103+
def lookup_alias_node(nodes, name)
104+
return if nodes.empty?
104105

105-
nodes.find do |node|
106-
if node.is_a?(GraphQL::Language::Nodes::FragmentSpread)
107-
node = @query.fragments[node.name]
108-
raise("Invariant: Can't look ahead to nonexistent fragment #{node.name} (found: #{@query.fragments.keys})") unless node
109-
end
106+
nodes.find do |node|
107+
if node.is_a?(GraphQL::Language::Nodes::FragmentSpread)
108+
node = @query.fragments[node.name]
109+
raise("Invariant: Can't look ahead to nonexistent fragment #{node.name} (found: #{@query.fragments.keys})") unless node
110+
end
110111

111-
return node if node.alias?(name)
112-
child = lookup_alias_node(node.children, name)
113-
return child if child
112+
return node if node.alias?(name)
113+
child = lookup_alias_node(node.children, name)
114+
return child if child
115+
end
114116
end
115117
end
116118
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
using RubyNext
4+
5+
module GraphQL
6+
module FragmentCache
7+
module GraphRubyVersion
8+
module_function
9+
10+
def before_2_0?
11+
check_graphql_version "< 2.0.0"
12+
end
13+
14+
def after_2_0_13?
15+
check_graphql_version "> 2.0.13"
16+
end
17+
18+
def before_2_1_4?
19+
check_graphql_version "< 2.1.4"
20+
end
21+
22+
def after_2_2_5?
23+
check_graphql_version "> 2.2.5"
24+
end
25+
26+
def check_graphql_version(predicate)
27+
Gem::Dependency.new("graphql", predicate).match?("graphql", GraphQL::VERSION)
28+
end
29+
end
30+
end
31+
end

spec/graphql/fragment_cache_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
describe GraphQL::FragmentCache do
66
describe ".use" do
7-
if GraphQL::FragmentCache.graphql_ruby_before_2_0?
7+
if GraphQL::FragmentCache::GraphRubyVersion.before_2_0?
88
it "raises if interpreter is not used" do
99
expect {
1010
Class.new(GraphQL::Schema) {

spec/support/schema_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module SchemaHelper
44
def build_schema(&block)
55
Class.new(GraphQL::Schema) do
6-
if GraphQL::FragmentCache.graphql_ruby_before_2_0?
6+
if GraphQL::FragmentCache::GraphRubyVersion.before_2_0?
77
use GraphQL::Execution::Interpreter
88
use GraphQL::Analysis::AST
99

spec/support/test_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def cached_post_by_complex_input(complex_post_input:)
116116
end
117117

118118
class TestSchema < GraphQL::Schema
119-
if GraphQL::FragmentCache.graphql_ruby_before_2_0?
119+
if GraphQL::FragmentCache::GraphRubyVersion.before_2_0?
120120
use GraphQL::Execution::Interpreter
121121
use GraphQL::Analysis::AST
122122

0 commit comments

Comments
 (0)