-
Notifications
You must be signed in to change notification settings - Fork 168
feat: support batch queries #550
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
Open
MedHeikelBouzayene
wants to merge
5
commits into
graphql-rust:main
Choose a base branch
from
MedHeikelBouzayene:feat/batch-queries
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
36cca4d
feat: support batch queries
MedHeikelBouzayene ae83817
fix: macro generated code
MedHeikelBouzayene e237572
fix: ci clippy and fmt
MedHeikelBouzayene b431f3e
fix: ci msrv
MedHeikelBouzayene c32f61d
fix: return a Vec of response instead of single response
MedHeikelBouzayene 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 hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
use graphql_client::*; | ||
|
||
#[derive(GraphQLQuery)] | ||
#[graphql( | ||
query_path = "tests/operation_selection/queries.graphql", | ||
schema_path = "tests/operation_selection/schema.graphql", | ||
response_derives = "Debug, PartialEq, Eq" | ||
)] | ||
pub struct Echo; | ||
|
||
#[test] | ||
fn batch_query() { | ||
let echo_variables = vec![ | ||
echo::Variables { | ||
msg: Some("hi".to_string()), | ||
}, | ||
echo::Variables { | ||
msg: Some("hello".to_string()), | ||
}, | ||
]; | ||
let echo_batch_queries: serde_json::Value = | ||
serde_json::to_value(Echo::build_batch_query(echo_variables)) | ||
.expect("Failed to serialize the query!"); | ||
assert_eq!( | ||
echo_batch_queries.to_string(), | ||
r#"[{"operationName":"Echo","query":"query Heights($buildingId: ID!, $mountainName: String) {\n mountainHeight(name: $mountainName)\n buildingHeight(id: $buildingId)\n}\n\nquery Echo($msg: String) {\n echo(msg: $msg)\n}\n","variables":{"msg":"hi"}},{"operationName":"Echo","query":"query Heights($buildingId: ID!, $mountainName: String) {\n mountainHeight(name: $mountainName)\n buildingHeight(id: $buildingId)\n}\n\nquery Echo($msg: String) {\n echo(msg: $msg)\n}\n","variables":{"msg":"hello"}}]"# | ||
); | ||
} |
Oops, something went wrong.
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.
Shouldn't this be
Result<Vec<crate::Response<...>>, _>
?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.
it will be serde_json::Array
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 think the shape of the ResponseData is from generated code, not a serde_json::Value, so I don't think serde_json::Array would appear anywhere, right?
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.
Also, this approach only works if it's a batch of the same query — in GraphQL, the queries can be different. It would not be straightforward to statically type, though.
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.
@tomhoule, If we want different queries, we should serialize the queries and have a serde_json::Value as a result since the trait GraphqlQuery is not object safe. if that's okay with you I can do that.
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.
There's little utility to that, since this crate is only about the type safety. Let's just document that it's a batch of identical queries.
My only reservation left is that I still don't understand why this doesn't return
Result<Vec<crate::Response<Q::ResponseData>>, reqwest::Error>
instead ofResult<crate::Response<Q::ResponseData>, reqwest::Error>
.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.
yes it makes more sense to have it as Vec<Q::ResponseData>, I did update it