Skip to content

Commit d5dfd1e

Browse files
feat: accept queries in the format of application/graphql (#3293)
1 parent a649c6c commit d5dfd1e

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

src/core/http/request_handler.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ pub async fn graphql_request<T: DeserializeOwned + GraphQLRequestLike>(
9191
let req_ctx = Arc::new(create_request_context(&req, app_ctx));
9292
let (req, body) = req.into_parts();
9393
let bytes = hyper::body::to_bytes(body).await?;
94+
let bytes = if req.headers.get("content-type")
95+
== Some(&HeaderValue::from_str("application/graphql")?)
96+
{
97+
let decoded_str = String::from_utf8_lossy(&bytes).clone();
98+
let enriched_str = if decoded_str.contains("\\") {
99+
format!(r#"{{"query": {} }}"#, decoded_str)
100+
} else {
101+
format!(r#"{{"query": {:?} }}"#, decoded_str)
102+
};
103+
hyper::body::to_bytes(enriched_str).await?
104+
} else {
105+
bytes
106+
};
94107
let graphql_request = serde_json::from_slice::<T>(&bytes);
95108
match graphql_request {
96109
Ok(request) => {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
source: tests/core/spec.rs
3+
expression: response
4+
snapshot_kind: text
5+
---
6+
{
7+
"status": 200,
8+
"headers": {
9+
"content-type": "application/json"
10+
},
11+
"body": {
12+
"data": {
13+
"user": {
14+
"city": "Globe",
15+
"name": "Tailcall"
16+
}
17+
}
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
source: tests/core/spec.rs
3+
expression: formatted
4+
snapshot_kind: text
5+
---
6+
type Query {
7+
user(id: ID!): User!
8+
}
9+
10+
type User {
11+
city: String
12+
id: ID!
13+
name: String!
14+
}
15+
16+
schema {
17+
query: Query
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: tests/core/spec.rs
3+
expression: formatter
4+
snapshot_kind: text
5+
---
6+
schema
7+
@server(hostname: "0.0.0.0", port: 8001, queryValidation: false)
8+
@upstream(httpCache: 42)
9+
@link(src: "schema_0.graphql", type: Config) {
10+
query: Query
11+
}
12+
13+
type Query {
14+
user(id: ID!): User!
15+
@graphQL(args: [{key: "id", value: "{{.args.id}}"}], url: "http://upstream/graphql", name: "user")
16+
}
17+
18+
type User {
19+
city: String
20+
id: ID!
21+
name: String!
22+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Basic queries with field ordering check
2+
3+
```yaml @config
4+
server:
5+
port: 8001
6+
hostname: "0.0.0.0"
7+
queryValidation: false
8+
upstream:
9+
httpCache: 42
10+
```
11+
12+
```graphql @schema
13+
schema {
14+
query: Query
15+
}
16+
17+
type Query {
18+
user(id: ID!): User!
19+
@graphQL(url: "http://upstream/graphql", name: "user", args: [{key: "id", value: "{{.args.id}}"}])
20+
}
21+
22+
type User {
23+
id: ID!
24+
name: String!
25+
city: String
26+
}
27+
```
28+
29+
```yml @mock
30+
- request:
31+
method: POST
32+
url: http://upstream/graphql
33+
textBody: '{ "query": "query { user(id: 4) { city name } }" }'
34+
expectedHits: 1
35+
response:
36+
status: 200
37+
body:
38+
data:
39+
user:
40+
city: Globe
41+
name: Tailcall
42+
```
43+
44+
```yml @test
45+
# Positive: basic 1
46+
- method: POST
47+
url: http://localhost:8080/graphql
48+
headers:
49+
content-type: application/graphql
50+
body: |
51+
query {
52+
user(id: 4) {
53+
city
54+
name
55+
}
56+
}
57+
```

0 commit comments

Comments
 (0)