Skip to content

Commit cd8d861

Browse files
committed
Use module based tracer
Using schema_definition.tracer(self) is deprecated so we should use the new module based tracing
1 parent ba6b13b commit cd8d861

File tree

5 files changed

+77
-13
lines changed

5 files changed

+77
-13
lines changed

.circleci/config.yml

+1-11
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
RUBYOPT: '-rostruct'
1919
steps:
2020
- checkout
21-
- run: gem install bundler -v '2.4.22'
21+
- run: ruby bin/install_bundler.rb
2222
- run:
2323
name: Install dependencies
2424
command: bundle install
@@ -42,9 +42,6 @@ workflows:
4242
ruby-version:
4343
- '2.7'
4444
- '3.0'
45-
- '3.1'
46-
- '3.2'
47-
- '3.3'
4845
gemfile:
4946
- gemfiles/rails6.0_graphql1.11.gemfile
5047
- gemfiles/rails6.0_graphql1.12.gemfile
@@ -64,13 +61,6 @@ workflows:
6461
- gemfiles/rails7.1_graphql2.1.gemfile
6562
- gemfiles/rails7.1_graphql2.2.gemfile
6663
- gemfiles/rails7.1_graphql2.3.gemfile
67-
exclude:
68-
- ruby-version: '3.3'
69-
gemfile: gemfiles/rails6.0_graphql1.11.gemfile
70-
- ruby-version: '3.3'
71-
gemfile: gemfiles/rails6.0_graphql1.12.gemfile
72-
- ruby-version: '3.3'
73-
gemfile: gemfiles/rails6.0_graphql1.13.gemfile
7464
- report-coverage:
7565
requires:
7666
- test

bin/install_bundler.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!ruby
2+
3+
ruby_version = Gem::Version.new(RUBY_VERSION)
4+
5+
if ruby_version < Gem::Version.new('2.6')
6+
system('gem install bundler -v 2.3.27')
7+
elsif ruby_version >= Gem::Version.new('2.6') && ruby_version < Gem::Version.new('3.0')
8+
system('gem install bundler -v 2.4.22')
9+
else
10+
system('gem install bundler')
11+
end
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
module GraphqlDevise
4+
module FieldAuthTracer
5+
def initialize(authenticate_default:, public_introspection:, unauthenticated_proc:, **_rest)
6+
@authenticate_default = authenticate_default
7+
@public_introspection = public_introspection
8+
@unauthenticated_proc = unauthenticated_proc
9+
10+
super
11+
end
12+
13+
def execute_field(field:, query:, ast_node:, arguments:, object:)
14+
# Authenticate only root level queries
15+
return super unless query.context.current_path.count == 1
16+
17+
auth_required = authenticate_option(field)
18+
19+
if auth_required && !(public_introspection && introspection_field?(field.name))
20+
raise_on_missing_resource(query.context, field, auth_required)
21+
end
22+
23+
super
24+
end
25+
26+
private
27+
28+
attr_reader :public_introspection
29+
30+
def authenticate_option(field)
31+
auth_required = field.try(:authenticate)
32+
33+
auth_required.nil? ? @authenticate_default : auth_required
34+
end
35+
36+
def introspection_field?(field_name)
37+
SchemaPlugin::INTROSPECTION_FIELDS.include?(field_name.downcase)
38+
end
39+
40+
def raise_on_missing_resource(context, field, auth_required)
41+
@unauthenticated_proc.call(field.name) if context[:current_resource].blank?
42+
43+
if auth_required.respond_to?(:call) && !auth_required.call(context[:current_resource])
44+
@unauthenticated_proc.call(field.name)
45+
end
46+
end
47+
end
48+
end

lib/graphql_devise/schema_plugin.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ def initialize(query: nil, mutation: nil, authenticate_default: true, public_int
1919
end
2020

2121
def use(schema_definition)
22-
schema_definition.tracer(self)
22+
if Gem::Version.new(GraphQL::VERSION) >= Gem::Version.new('2.3')
23+
schema_definition.trace_with(
24+
FieldAuthTracer,
25+
authenticate_default: @authenticate_default,
26+
public_introspection: public_introspection,
27+
unauthenticated_proc: @unauthenticated_proc
28+
)
29+
else
30+
schema_definition.tracer(self)
31+
end
2332
end
2433

2534
def trace(event, trace_data)

spec/requests/queries/introspection_query_spec.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,13 @@
116116

117117
context 'and introspection is set to require auth' do
118118
before do
119-
allow_any_instance_of(GraphqlDevise::SchemaPlugin).to(
119+
target_class = if Gem::Version.new(GraphQL::VERSION) >= Gem::Version.new('2.3')
120+
GraphqlDevise::FieldAuthTracer
121+
else
122+
GraphqlDevise::SchemaPlugin
123+
end
124+
125+
allow_any_instance_of(target_class).to(
120126
receive(:public_introspection).and_return(false)
121127
)
122128
end

0 commit comments

Comments
 (0)