Skip to content

Commit

Permalink
feat: use method enum instead of string in @rest (#1379)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop authored Mar 11, 2024
1 parent 3d36096 commit e9572ef
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 7 deletions.
4 changes: 2 additions & 2 deletions examples/operations/routes.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
query posts @rest(method: "get", path: "/posts") {
query posts @rest(method: GET, path: "/posts") {
posts {
id
title
Expand All @@ -10,7 +10,7 @@ query posts @rest(method: "get", path: "/posts") {
}
}

query users @rest(method: "get", path: "/users") {
query users @rest(method: GET, path: "/users") {
users {
id
name
Expand Down
3 changes: 2 additions & 1 deletion src/rest/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ impl TryFrom<&Directive> for Rest {
rest.path = serde_json::from_str(v.node.to_string().as_str())?;
}
if k.node.as_str() == "method" {
rest.method = serde_json::from_str(v.node.to_string().to_uppercase().as_str())?;
let value = serde_json::Value::String(v.node.to_string().to_uppercase());
rest.method = serde_json::from_value(value)?;
}
if k.node.as_str() == "query" {
if let Value::Object(map) = &v.node {
Expand Down
6 changes: 3 additions & 3 deletions src/rest/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,20 @@ mod tests {

const TEST_QUERY: &str = r#"
query ($a: Int, $b: String, $c: Boolean, $d: Float, $v: String)
@rest(method: "post", path: "/foo/$a", query: {b: $b, c: $c, d: $d}, body: $v) {
@rest(method: POST, path: "/foo/$a", query: {b: $b, c: $c, d: $d}, body: $v) {
value
}
"#;

const MULTIPLE_TEST_QUERY: &str = r#"
query q1 ($a: Int)
@rest(method: "post", path: "/foo/$a") {
@rest(method: POST, path: "/foo/$a") {
value
}
query q2 ($a: Int)
@rest(method: "post", path: "/bar/$a") {
@rest(method: POST, path: "/bar/$a") {
value
}
"#;
Expand Down
55 changes: 55 additions & 0 deletions tests/execution/rest-api-post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Rest API

#### file:operation-user.graphql

```graphql
query ($id: Int!) @rest(method: POST, path: "/user/$id") {
user(id: $id) {
id
name
}
}
```

#### server:

```graphql
schema
@server
@upstream(baseURL: "http://jsonplaceholder.typicode.com")
@link(type: Operation, src: "operation-user.graphql") {
query: Query
}

type Query {
user(id: Int!): User @http(path: "/users/{{args.id}}")
}

type User {
id: Int!
name: String!
}
```

#### mock:

```yml
- request:
method: GET
url: http://jsonplaceholder.typicode.com/users/1
headers:
test: test
body: null
response:
status: 200
body:
id: 1
name: foo
```

#### assert:

```yml
- method: POST
url: http://localhost:8080/api/user/1
```
2 changes: 1 addition & 1 deletion tests/execution/rest-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#### file:operation-user.graphql

```graphql
query ($id: Int!) @rest(method: "get", path: "/user/$id") {
query ($id: Int!) @rest(method: GET, path: "/user/$id") {
user(id: $id) {
id
name
Expand Down
18 changes: 18 additions & 0 deletions tests/snapshots/execution_spec__rest-api-post.md_assert_0.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: tests/execution_spec.rs
expression: response
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": {
"user": {
"id": 1,
"name": "foo"
}
}
}
}
16 changes: 16 additions & 0 deletions tests/snapshots/execution_spec__rest-api-post.md_client.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: tests/execution_spec.rs
expression: client
---
type Query {
user(id: Int!): User
}

type User {
id: Int!
name: String!
}

schema {
query: Query
}
16 changes: 16 additions & 0 deletions tests/snapshots/execution_spec__rest-api-post.md_merged.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: tests/execution_spec.rs
expression: merged
---
schema @server @upstream(baseURL: "http://jsonplaceholder.typicode.com") @link(src: "operation-user.graphql", type: Operation) {
query: Query
}

type Query {
user(id: Int!): User @http(path: "/users/{{args.id}}")
}

type User {
id: Int!
name: String!
}

1 comment on commit e9572ef

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 6.69ms 3.09ms 88.00ms 72.66%
Req/Sec 3.79k 287.44 4.35k 86.50%

452350 requests in 30.01s, 2.27GB read

Requests/sec: 15075.09

Transfer/sec: 77.38MB

Please sign in to comment.