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
Schema#multiplex causes context.namespace(:interpreter)[:current_path] to be nil inside then
block
#3397
Comments
Hey, thanks for the details on this issue! Yes, this sounds like a bug to me, and this is the right place to report it. I would have expected those context values to be populated by this:
But evidently that's not the case! |
(derp, wrong button.) |
I've tried to add logging right after graphql-ruby/lib/graphql/execution/interpreter/runtime.rb:489 and figured out, that it's called once before resolving the lazy object (and 3 more times after):
|
Ohhh, interesting. I guess it's because the promise created by As a work-around, you might have to capture Otherwise, I'm not sure how GraphQL-Ruby could inject runtime info (for other fields) into GraphQL-batch's promise resolution 😖 Although practically irrelevant, it looks like Diff of replication example
git diff --no-index original_issue.rb modified_issue.rb
diff --git a/original_issue.rb b/modified_issue.rb
index 9cf7ec80a..36f46bbde 100644
--- a/original_issue.rb
+++ b/modified_issue.rb
@@ -54,14 +54,15 @@ class Tweet < ActiveRecord::Base
end
module Batch
- class RecordLoader < GraphQL::Batch::Loader
- def initialize(model)
- @model = model
+ class RecordSource < GraphQL::Dataloader::Source
+ def initialize(model_class)
+ @model_class = model_class
end
- def perform(ids)
- @model.where(id: ids).each { |record| fulfill(record.id, record) }
- ids.each { |id| fulfill(id, nil) unless fulfilled?(id) }
+ def fetch(ids)
+ records = @model_class.where(id: ids)
+ # return a list with `nil` for any ID that wasn't found
+ ids.map { |id| records.find { |r| r.id == id.to_i } }
end
end
end
@@ -78,10 +79,9 @@ module Types
def author
puts "Tweet:author current_path=#{context.namespace(:interpreter)[:current_path]}"
- Batch::RecordLoader.for(::User).load(object.author_id).then do |author|
- puts "Tweet:author after batch current_path=#{context.namespace(:interpreter)[:current_path]}"
- author
- end
+ author = dataloader.with(Batch::RecordSource, ::User).load(object.author_id)
+ puts "Tweet:author after batch current_path=#{context.namespace(:interpreter)[:current_path]}"
+ author
end
end
end
@@ -105,7 +105,7 @@ end
class GraphqlSchema < GraphQL::Schema
query Types::Query
- use GraphQL::Batch
+ use GraphQL::Dataloader
end
Tweet.delete_all |
Yeah, I don't see any clear way to update the context inside the promise block. Closing the issue, thank you for the help! |
Describe the bug
I found a case when
context.namespace(:interpreter)[:current_path]
isnil
(to be precise, most of the keys inside the:interpreter
hash are missing). The problem appears whenSchema#multiplex
is called and I try to get the current path inside.then
block in promise, i.e.:However, it works fine when
Schema#execute
is used.Not sure if I should go to the graphql-batch repo with this issue.
Versions
graphql
version: latestgraphql-batch
version: latestSteps to reproduce
A whole example:
Expected behavior
I want to be able to get the current path inside the promise 🙂
Actual behavior
Current path is available only inside first promise block. Here is what I see in the console:
The text was updated successfully, but these errors were encountered: