Offset is 0 in `after_resolve`
Describe the bug
When value.nodes is called in after_resolve offset is always 0 not the specified in the query.
Simplified example:
Query:
query {
products(first: 5, offset: 10) {
id
name
}
}
Field:
field :products, Types::Product.connection_type, extensions: [MyExtension]
Extension:
class MyExtension < GraphQL::Schema::FieldExtension
def after_resolve(value:, **rest)
records = value.nodes
# ^^ This generate a query SELECT products.* FROM products LIMIT 5 OFFSET 0
# Specified offset in GraphQL query is 10, not 0.
value
end
end
Versions
graphql version: 2.0.13
rails: 7.0.4.3
Hi! Thanks for reporting this bug. How strange ... I'm not exactly sure where GraphQL-Ruby is getting OFFSET 0 but I guess it's from some part of the connection behavior. I'll take a look soon and follow up back here.
Connection pagination is also implemented with an extension, perhaps that's related:
https://github.com/rmosolgo/graphql-ruby/blob/c120256d63fefe3eb9857ddce03960a06dbf6cd7/lib/graphql/schema/field/connection_extension.rb#L24-L75
I check the tests and found one for exactly this behavior:
https://github.com/rmosolgo/graphql-ruby/blob/fdfc892ae34cbc02c8ecb55bee67a96677541fba/spec/graphql/pagination/active_record_relation_connection_spec.rb#L42-L65
And I wrote up a little script to double-check, and it works for me:
A script with manually-provided offset in GraphQL connections
require "bundler/inline"
gemfile do
gem "graphql"
gem "sqlite3"
gem "rails", require: "rails/all"
end
# Define and populate the database
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.define do
self.verbose = false
create_table :books, force: true do |t|
t.column :author, :string
t.column :title, :string
end
end
class Book < ActiveRecord::Base; end
Book.create!(title: "Remembering", author: "Wendell Berry")
Book.create!(title: "The Going to Bed Book", author: "Sandra Boynton")
Book.create!(title: "Gilead", author: "Marilynne Robinson")
Book.create!(title: "Historical Brewing Techniques", author: "Lars Marius Garshol")
Book.create!(title: "The Tale of Tom Kitten", author: "Beatrix Potter")
class MySchema < GraphQL::Schema
class CheckOffsetExtension < GraphQL::Schema::FieldExtension
def after_resolve(value:, context:, **_rest)
# Which book in the database is the first among the nodes?
context[:observed_offset] = ::Book.all.to_a.find_index { |b| b == value.nodes.first }
value
end
end
class Book < GraphQL::Schema::Object
field :author, String
field :title, String
end
class Query < GraphQL::Schema::Object
field :books, Book.connection_type, extensions: [CheckOffsetExtension] do
argument :offset, Integer, required: false
end
def books(offset: 0)
::Book.offset(offset)
end
end
query(Query)
end
ActiveRecord::Base.logger = Logger.new(STDOUT)
res = MySchema.execute("{ books(first: 2) { nodes { title } } }")
# D, [2023-10-20T14:32:18.462245 #79631] DEBUG -- : Book Load (0.1ms) SELECT "books".* FROM "books"
# D, [2023-10-20T14:32:18.462660 #79631] DEBUG -- : Book Load (0.0ms) SELECT "books".* FROM "books" LIMIT ? OFFSET ? [["LIMIT", 2], ["OFFSET", 0]]
pp res.context.to_h
# {:observed_offset=>0}
pp res.to_h
# {"data"=>{"books"=>{"nodes"=>[{"title"=>"Remembering"}, {"title"=>"The Going to Bed Book"}]}}}
res = MySchema.execute("{ books(first: 2, offset: 2) { nodes { title } } }")
# D, [2023-10-20T14:32:18.469144 #79631] DEBUG -- : Book Load (0.0ms) SELECT "books".* FROM "books"
# D, [2023-10-20T14:32:18.469387 #79631] DEBUG -- : Book Load (0.0ms) SELECT "books".* FROM "books" LIMIT ? OFFSET ? [["LIMIT", 2], ["OFFSET", 2]]
pp res.context.to_h
# {:observed_offset=>2}
pp res.to_h
# {"data"=>{"books"=>{"nodes"=>[{"title"=>"Gilead"}, {"title"=>"Historical Brewing Techniques"}]}}}
Does that script give you the expected output?
Do you see any ways your application differs from that script? Maybe we can add something to it to replicate this bug.
Hi, I'd be happy to debug this further if you can help me replicate the bug somehow. I hope you found something that works for you!