Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix scoped detection in Connection#initialize #4675

Merged
merged 2 commits into from
Oct 19, 2023
Merged
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
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