Skip to content

Commit 44ecaf6

Browse files
committed
enhance(query-planner): avoid extra query prefix for anonymous queries
1 parent ddab754 commit 44ecaf6

File tree

15 files changed

+169
-142
lines changed

15 files changed

+169
-142
lines changed

.changeset/extra-query-avoid.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
hive-router-query-planner: patch
3+
---
4+
5+
# Avoid extra `query` prefix for anonymous queries
6+
7+
When there is no variable definitions and no operation name, GraphQL queries can be sent without the `query` prefix. For example, instead of sending:
8+
9+
```diff
10+
- query {
11+
+ {
12+
user(id: "1") {
13+
name
14+
}
15+
}
16+
```

lib/query-planner/src/ast/normalization/mod.rs

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,16 @@ mod tests {
135135
.unwrap()
136136
.to_string()
137137
),
138-
@r#"
139-
query {
140-
a: __typename
141-
words
142-
__typename
143-
__schema {
144-
__typename
145-
}
146-
}
147-
"#
138+
@r"
139+
{
140+
a: __typename
141+
words
142+
__typename
143+
__schema {
144+
__typename
145+
}
146+
}
147+
"
148148
);
149149
}
150150

@@ -179,11 +179,11 @@ mod tests {
179179
.to_string()
180180
),
181181
@r#"
182-
query {
183-
words(sep: ",")
184-
foo: words(len: 10, sep: ".")
185-
}
186-
"#
182+
{
183+
words(sep: ",")
184+
foo: words(len: 10, sep: ".")
185+
}
186+
"#
187187
);
188188
}
189189

@@ -261,14 +261,14 @@ mod tests {
261261
.unwrap()
262262
.to_string()
263263
),
264-
@r#"
265-
query {
266-
words
267-
c: words(len: 1)
268-
b: words
269-
a: words
270-
}
271-
"#
264+
@r"
265+
{
266+
words
267+
c: words(len: 1)
268+
b: words
269+
a: words
270+
}
271+
"
272272
);
273273
}
274274

@@ -386,7 +386,7 @@ mod tests {
386386
.to_string()
387387
),
388388
@r"
389-
query {
389+
{
390390
products {
391391
... on Toaster {
392392
t1
@@ -529,12 +529,12 @@ mod tests {
529529

530530
insta::assert_snapshot!(
531531
pretty_query(r.to_string()),
532-
@r###"
533-
query {
532+
@r#"
533+
{
534534
one: words(len: 10, sep: ".")
535535
two: words(len: 10, sep: ".")
536536
}
537-
"###
537+
"#
538538
);
539539
}
540540

@@ -697,7 +697,7 @@ mod tests {
697697
.to_string()
698698
),
699699
@r"
700-
query {
700+
{
701701
userFromA {
702702
profile {
703703
displayName
@@ -792,7 +792,7 @@ mod tests {
792792
.to_string()
793793
),
794794
@r"
795-
query {
795+
{
796796
products {
797797
id
798798
... on Book {
@@ -841,13 +841,13 @@ mod tests {
841841
.to_string()
842842
),
843843
@r"
844-
query {
845-
anotherUsers {
846-
id
847-
name
848-
username
849-
}
850-
}
844+
{
845+
anotherUsers {
846+
id
847+
name
848+
username
849+
}
850+
}
851851
",
852852
);
853853
}
@@ -885,19 +885,19 @@ mod tests {
885885
.to_string()
886886
),
887887
@r"
888-
query {
889-
userFromA {
890-
profile {
891-
... on AdminAccount {
892-
accountType
893-
}
894-
... on GuestAccount {
895-
accountType
888+
{
889+
userFromA {
890+
profile {
891+
... on AdminAccount {
892+
accountType
893+
}
894+
... on GuestAccount {
895+
accountType
896+
}
896897
}
897898
}
898899
}
899-
}
900-
",
900+
",
901901
);
902902
}
903903

@@ -940,20 +940,20 @@ mod tests {
940940
.to_string()
941941
),
942942
@r"
943-
query {
944-
userFromA {
945-
profile {
946-
... on AdminAccount {
947-
accountType
948-
}
949-
... on GuestAccount {
950-
accountType
951-
guestToken
943+
{
944+
userFromA {
945+
profile {
946+
... on AdminAccount {
947+
accountType
948+
}
949+
... on GuestAccount {
950+
accountType
951+
guestToken
952+
}
952953
}
953954
}
954955
}
955-
}
956-
",
956+
",
957957
);
958958
}
959959

@@ -1185,7 +1185,7 @@ mod tests {
11851185
.to_string()
11861186
),
11871187
@r"
1188-
query {
1188+
{
11891189
results {
11901190
__typename
11911191
... on MultipleColor {
@@ -1265,27 +1265,27 @@ mod tests {
12651265
.to_string()
12661266
),
12671267
@r"
1268-
query {
1269-
results {
1270-
__typename
1271-
... on MultipleColor {
1272-
id
1273-
name
1274-
colorOptions {
1275-
id
1276-
color
1277-
}
1278-
}
1279-
... on SingleColor {
1280-
id
1281-
name
1282-
colorOption {
1283-
id
1284-
color
1285-
}
1286-
}
1268+
{
1269+
results {
1270+
__typename
1271+
... on MultipleColor {
1272+
id
1273+
name
1274+
colorOptions {
1275+
id
1276+
color
12871277
}
12881278
}
1279+
... on SingleColor {
1280+
id
1281+
name
1282+
colorOption {
1283+
id
1284+
color
1285+
}
1286+
}
1287+
}
1288+
}
12891289
"
12901290
);
12911291
}

lib/query-planner/src/ast/operation.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ impl PrettyDisplay for SubgraphFetchOperation {
129129

130130
impl Display for OperationDefinition {
131131
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132+
if (self.operation_kind.is_none() || self.operation_kind.as_ref().map_or(true, |k| matches!(k, OperationKind::Query)))
133+
&& (self.name.is_none() || self.name.as_ref().map_or(true, |n| n.is_empty()))
134+
&& (self.variable_definitions.is_none()
135+
|| self
136+
.variable_definitions
137+
.as_ref()
138+
.map_or(true, |v| v.is_empty()))
139+
{
140+
// Short form for anonymous query
141+
return self.selection_set.fmt(f);
142+
}
132143
if let Some(operation_kind) = &self.operation_kind {
133144
write!(f, "{}", operation_kind)?;
134145
}

lib/query-planner/src/tests/alias.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ fn conflict_list_type_in_interface() -> Result<(), Box<dyn Error>> {
138138
},
139139
"###);
140140

141-
insta::assert_snapshot!(format!("{}", sonic_rs::to_string_pretty(&query_plan).unwrap_or_default()), @r###"
141+
insta::assert_snapshot!(format!("{}", sonic_rs::to_string_pretty(&query_plan).unwrap_or_default()), @r#"
142142
{
143143
"kind": "QueryPlan",
144144
"node": {
145145
"kind": "Fetch",
146146
"serviceName": "a",
147147
"operationKind": "query",
148-
"operation": "query{i{__typename ...on TypeA{strField} ...on TypeB{_internal_qp_alias_0: strField}}}",
148+
"operation": "{i{__typename ...on TypeA{strField} ...on TypeB{_internal_qp_alias_0: strField}}}",
149149
"outputRewrites": [
150150
{
151151
"KeyRenamer": {
@@ -166,7 +166,7 @@ fn conflict_list_type_in_interface() -> Result<(), Box<dyn Error>> {
166166
]
167167
}
168168
}
169-
"###);
169+
"#);
170170

171171
Ok(())
172172
}
@@ -212,14 +212,14 @@ fn multiple_mismtaches_same_level() -> Result<(), Box<dyn Error>> {
212212
},
213213
"###);
214214

215-
insta::assert_snapshot!(format!("{}", sonic_rs::to_string_pretty(&query_plan).unwrap_or_default()), @r###"
215+
insta::assert_snapshot!(format!("{}", sonic_rs::to_string_pretty(&query_plan).unwrap_or_default()), @r#"
216216
{
217217
"kind": "QueryPlan",
218218
"node": {
219219
"kind": "Fetch",
220220
"serviceName": "a",
221221
"operationKind": "query",
222-
"operation": "query{i{__typename ...on TypeA{strField strField2} ...on TypeB{_internal_qp_alias_0: strField _internal_qp_alias_1: strField2}}}",
222+
"operation": "{i{__typename ...on TypeA{strField strField2} ...on TypeB{_internal_qp_alias_0: strField _internal_qp_alias_1: strField2}}}",
223223
"outputRewrites": [
224224
{
225225
"KeyRenamer": {
@@ -256,7 +256,7 @@ fn multiple_mismtaches_same_level() -> Result<(), Box<dyn Error>> {
256256
]
257257
}
258258
}
259-
"###);
259+
"#);
260260

261261
Ok(())
262262
}
@@ -426,7 +426,7 @@ fn simple_mismatch_between_union_fields() -> Result<(), Box<dyn Error>> {
426426
"kind": "Fetch",
427427
"serviceName": "b",
428428
"operationKind": "query",
429-
"operation": "query{accounts{__typename ...on User{id name} ...on Admin{_internal_qp_alias_0: id name}}}",
429+
"operation": "{accounts{__typename ...on User{id name} ...on Admin{_internal_qp_alias_0: id name}}}",
430430
"outputRewrites": [
431431
{
432432
"KeyRenamer": {
@@ -450,7 +450,7 @@ fn simple_mismatch_between_union_fields() -> Result<(), Box<dyn Error>> {
450450
"kind": "Fetch",
451451
"serviceName": "a",
452452
"operationKind": "query",
453-
"operation": "query{users{__typename id}}"
453+
"operation": "{users{__typename id}}"
454454
}
455455
]
456456
},
@@ -616,7 +616,7 @@ fn nested_internal_mismatch_between_fields() -> Result<(), Box<dyn Error>> {
616616
"kind": "Fetch",
617617
"serviceName": "b",
618618
"operationKind": "query",
619-
"operation": "query{accounts{__typename ...on User{id name similarAccounts{...a}} ...on Admin{_internal_qp_alias_0: id name similarAccounts{...a}}}}\n\nfragment a on Account {__typename ...on User{id name} ...on Admin{_internal_qp_alias_0: id name}}\n",
619+
"operation": "{accounts{__typename ...on User{id name similarAccounts{...a}} ...on Admin{_internal_qp_alias_0: id name similarAccounts{...a}}}}\n\nfragment a on Account {__typename ...on User{id name} ...on Admin{_internal_qp_alias_0: id name}}\n",
620620
"outputRewrites": [
621621
{
622622
"KeyRenamer": {
@@ -684,7 +684,7 @@ fn nested_internal_mismatch_between_fields() -> Result<(), Box<dyn Error>> {
684684
"kind": "Fetch",
685685
"serviceName": "a",
686686
"operationKind": "query",
687-
"operation": "query{users{__typename id}}"
687+
"operation": "{users{__typename id}}"
688688
}
689689
]
690690
},
@@ -905,7 +905,7 @@ fn deeply_nested_internal_mismatch_between_fields() -> Result<(), Box<dyn Error>
905905
"kind": "Fetch",
906906
"serviceName": "b",
907907
"operationKind": "query",
908-
"operation": "query{accounts{__typename ...on User{id name similarAccounts{...a}} ...on Admin{_internal_qp_alias_0: id name similarAccounts{...a}}}}\n\nfragment a on Account {__typename ...on User{id name similarAccounts{...b}} ...on Admin{_internal_qp_alias_0: id name similarAccounts{...b}}}\nfragment b on Account {__typename ...on User{id name} ...on Admin{_internal_qp_alias_0: id name}}\n",
908+
"operation": "{accounts{__typename ...on User{id name similarAccounts{...a}} ...on Admin{_internal_qp_alias_0: id name similarAccounts{...a}}}}\n\nfragment a on Account {__typename ...on User{id name similarAccounts{...b}} ...on Admin{_internal_qp_alias_0: id name similarAccounts{...b}}}\nfragment b on Account {__typename ...on User{id name} ...on Admin{_internal_qp_alias_0: id name}}\n",
909909
"outputRewrites": [
910910
{
911911
"KeyRenamer": {
@@ -1085,7 +1085,7 @@ fn deeply_nested_internal_mismatch_between_fields() -> Result<(), Box<dyn Error>
10851085
"kind": "Fetch",
10861086
"serviceName": "a",
10871087
"operationKind": "query",
1088-
"operation": "query{users{__typename id}}"
1088+
"operation": "{users{__typename id}}"
10891089
}
10901090
]
10911091
},
@@ -1249,7 +1249,7 @@ fn deeply_nested_no_conflicts() -> Result<(), Box<dyn Error>> {
12491249
"kind": "Fetch",
12501250
"serviceName": "b",
12511251
"operationKind": "query",
1252-
"operation": "query{accounts{__typename ...on User{name similarAccounts{...a}} ...on Admin{name similarAccounts{...a}}}}\n\nfragment a on Account {__typename ...on User{name similarAccounts{...b}} ...on Admin{name similarAccounts{...b}}}\nfragment b on Account {__typename ...on User{name} ...on Admin{name}}\n"
1252+
"operation": "{accounts{__typename ...on User{name similarAccounts{...a}} ...on Admin{name similarAccounts{...a}}}}\n\nfragment a on Account {__typename ...on User{name similarAccounts{...b}} ...on Admin{name similarAccounts{...b}}}\nfragment b on Account {__typename ...on User{name} ...on Admin{name}}\n"
12531253
}
12541254
}
12551255
"#);

0 commit comments

Comments
 (0)