Skip to content

Commit

Permalink
Merge pull request #4675 from rmosolgo/fix-connection-initialization
Browse files Browse the repository at this point in the history
Fix scoped detection in Connection#initialize
  • Loading branch information
rmosolgo authored Oct 19, 2023
2 parents ad12b34 + afe8a09 commit 0b6c6c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/graphql/pagination/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ class PaginationImplementationMissingError < GraphQL::Error
attr_reader :context

def context=(new_ctx)
current_runtime_state = Thread.current[:__graphql_runtime_info]
query_runtime_state = current_runtime_state[new_ctx.query]
@was_authorized_by_scope_items = query_runtime_state.was_authorized_by_scope_items
@context = new_ctx
if @was_authorized_by_scope_items.nil?
@was_authorized_by_scope_items = detect_was_authorized_by_scope_items
end
@context
end

# @return [Object] the object this collection belongs to
Expand Down Expand Up @@ -90,13 +91,7 @@ def initialize(items, parent: nil, field: nil, context: nil, first: nil, after:
else
default_page_size
end
@was_authorized_by_scope_items = if @context
current_runtime_state = Thread.current[:__graphql_runtime_info]
query_runtime_state = current_runtime_state[@context.query]
query_runtime_state.was_authorized_by_scope_items
else
nil
end
@was_authorized_by_scope_items = detect_was_authorized_by_scope_items
end

def was_authorized_by_scope_items?
Expand Down Expand Up @@ -226,6 +221,16 @@ def cursor_for(item)

private

def detect_was_authorized_by_scope_items
if @context &&
(current_runtime_state = Thread.current[:__graphql_runtime_info]) &&
(query_runtime_state = current_runtime_state[@context.query])
query_runtime_state.was_authorized_by_scope_items
else
nil
end
end

# @param argument [nil, Integer] `first` or `last`, as provided by the client
# @param max_page_size [nil, Integer]
# @return [nil, Integer] `nil` if the input was `nil`, otherwise a value between `0` and `max_page_size`
Expand Down
21 changes: 21 additions & 0 deletions spec/graphql/pagination/connection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Pagination::Connection do
describe "was_authorized_by_scope_ites?" do
it "doesn't raise an error for missing runtime state and it updates it if context is assigned later" do
context = GraphQL::Query.new(GraphQL::Schema, "{ __typename }").context
conn = GraphQL::Pagination::Connection.new([], context: context)
assert_nil conn.was_authorized_by_scope_items?

conn.context = context
assert_nil conn.was_authorized_by_scope_items?

Thread.current[:__graphql_runtime_info] = { context.query => OpenStruct.new(was_authorized_by_scope_items: true) }
conn.context = context
assert_equal true, conn.was_authorized_by_scope_items?
ensure
Thread.current[:__graphql_runtime_info] = nil
end
end
end

0 comments on commit 0b6c6c1

Please sign in to comment.