-
Notifications
You must be signed in to change notification settings - Fork 106
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
feat(GraphQL::Batch::SetupMultiplex) use multiplex instrumentation to share a batching context #57
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module GraphQL::Batch | ||
module SetupMultiplex | ||
extend self | ||
|
||
def before_multiplex(multiplex) | ||
raise NestedError if GraphQL::Batch::Executor.current | ||
GraphQL::Batch::Executor.current = GraphQL::Batch::Executor.new | ||
end | ||
|
||
def after_multiplex(multiplex) | ||
GraphQL::Batch::Executor.current = nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require_relative 'test_helper' | ||
|
||
class GraphQL::MultiplexTest < Minitest::Test | ||
attr_reader :queries | ||
|
||
def setup | ||
@queries = [] | ||
QueryNotifier.subscriber = ->(query) { @queries << query } | ||
end | ||
|
||
def teardown | ||
QueryNotifier.subscriber = nil | ||
end | ||
|
||
def test_batched_find_by_id | ||
query_string = <<-GRAPHQL | ||
query FetchTwoProducts($id1: ID!, $id2: ID!){ | ||
first: product(id: $id1) { id, title } | ||
second: product(id: $id2) { id, title } | ||
} | ||
GRAPHQL | ||
|
||
results = Schema.multiplex([ | ||
{ query: query_string, variables: { "id1" => "1", "id2" => "2"} }, | ||
{ query: query_string, variables: { "id1" => "1", "id2" => "3"}}, | ||
]) | ||
|
||
expected_1 = { | ||
"data" => { | ||
"first" => { "id" => "1", "title" => "Shirt" }, | ||
"second" => { "id" => "2", "title" => "Pants" }, | ||
} | ||
} | ||
|
||
expected_2 = { | ||
"data" => { | ||
"first" => { "id" => "1", "title" => "Shirt" }, | ||
"second" => { "id" => "3", "title" => "Sweater" }, | ||
} | ||
} | ||
|
||
assert_equal expected_1, results.first | ||
assert_equal expected_2, results.last | ||
assert_equal ["Product/1,2,3"], queries | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried making the following changes to the test
and it still passed, even though there is a mutation in between the queries.
What should be the expectations around the ordering of the operations? The graphql-ruby multiplex documentation says it will "run queries concurrently", but also says this feature is for Apollo Client’s query batching which "is a transport-level mechanism" where I would expect the order of operations would be preserved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great point, and it's something I overlooked. It makes no special treatment for mutations, but it could. (Apparently the JS server doesn't make special treatment either: apollographql/apollo-server#176 (comment))
Maybe we could do a flow like this:
I think that would maintain the expected ordering for mutations: queries before mutations would not reflect their changes, but queries after mutations would reflect mutations.
However, now that I write it, I don't know if the incoming queries are ordered. Maybe they come as an unordered set.
If that's the case, maybe a better flow is mutate-then-read:
That way, all reads in the multiplex reflect the changes of all mutations in the multiplex.
Hmm, what if the mutations are order-dependent? :head_explodes:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, so it looks like they intentionally have the queries running in parallel according to the apollo server implementation. Given that, and the fact that graphql-ruby already follows that approach and documents it more clearly, I guess there is no need to be concerned about it here.
I suppose a client or a server could always choose to re-order the operations and multiplex/group them based on their desired semantics.
In that case I would put all the mutations in the same mutation query as separate field selections, although that is easier when using dynamic queries.