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

Require loaders to be used within a GraphQL::Batch.batch block #36

Merged
merged 1 commit into from
Nov 22, 2016

Conversation

dylanahsmith
Copy link
Contributor

@rmosolgo, @xuorig & @cjoudrey

Problem

As pointed out in #33 (comment) and #35, by storing state in thread local variables we run the risk of state leaking between requests or tests.

Solution

Change GraphQL::Batch::Executor.current so that it no longer automatically creates an executor so that the scope of batching is done explicitly using GraphQL::Batch.batch. E.g.

product = GraphQL::Batch.batch do
    RecordLoader.for(Product).load(product_id)
end

will assign an executor to GraphQL::Batch::Executor.current, call Promise.sync on the result of the block and clear the executor at the end of the block.

The reason why Promise.sync is needed on the result is because the promise callbacks might also do batch loads using GraphQL::Batch::Loader.for to get the loader from GraphQL::Batch::Executor.current.

@rmosolgo
Copy link
Contributor

👍 In a world with no ExecutionStrategy, how do you think this could be hooked up to Schema#execute?

@dylanahsmith
Copy link
Contributor Author

I was thinking it could be called outside of graphql-ruby around the call to Schema#execute. There is a before_query and after_query instrumentation in graphql-ruby, but an exception would prevent the after_query method from being called.

Do you have any suggestions for how you would like it to integrate with graphql-ruby?

@rmosolgo
Copy link
Contributor

👍 for instrumentation, I think this ensure would do the trick: rmosolgo/graphql-ruby#412

Then you could have

GraphQL::Schema.define do 
  # ... 
  instrument(:query, GraphQL::Batch::Intstrumentation) 
end 

I'd be happy to update my open PR with that code if it looks ok to you!

@dylanahsmith
Copy link
Contributor Author

That sounds good, except the name GraphQL::Batch::Instrumentation sounds like something for #27 rather than something to start and end the batch. What do you think about instrument(:query, GraphQL::Batch::Setup) to make its purpose clearer?

@rmosolgo
Copy link
Contributor

👍 for Setup !

This is needed to avoid global state leaking between requests/tests.
@dylanahsmith
Copy link
Contributor Author

I'd be happy to update my open PR with that code if it looks ok to you!

👍

@dylanahsmith dylanahsmith merged commit d1454e9 into master Nov 22, 2016
@dylanahsmith dylanahsmith deleted the batch-block branch November 22, 2016 16:03
@dylanahsmith dylanahsmith mentioned this pull request Jan 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants