Skip to content

Commit bb670b9

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

File tree

15 files changed

+173
-142
lines changed

15 files changed

+173
-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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ 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()
133+
|| self
134+
.operation_kind
135+
.as_ref()
136+
.is_none_or(|k| matches!(k, OperationKind::Query)))
137+
&& (self.name.is_none() || self.name.as_ref().is_none_or(|n| n.is_empty()))
138+
&& (self.variable_definitions.is_none()
139+
|| self
140+
.variable_definitions
141+
.as_ref()
142+
.is_none_or(|v| v.is_empty()))
143+
{
144+
// Short form for anonymous query
145+
return self.selection_set.fmt(f);
146+
}
132147
if let Some(operation_kind) = &self.operation_kind {
133148
write!(f, "{}", operation_kind)?;
134149
}

0 commit comments

Comments
 (0)