Skip to content

Commit

Permalink
test: refactor recursive test cases (#2354)
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill committed Jul 5, 2024
1 parent 3e30075 commit 92455fd
Show file tree
Hide file tree
Showing 22 changed files with 580 additions and 264 deletions.
5 changes: 2 additions & 3 deletions src/core/blueprint/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,10 @@ pub fn fix_dangling_resolvers<'a>(
) -> TryFold<'a, (&'a ConfigModule, &'a Field, &'a config::Type, &'a str), FieldDefinition, String>
{
TryFold::<(&ConfigModule, &Field, &config::Type, &str), FieldDefinition, String>::new(
move |(config, field, ty, name), mut b_field| {
move |(config, field, _, name), mut b_field| {
let mut set = HashSet::new();
if !field.has_resolver()
&& validate_field_has_resolver(name, field, &config.types, ty, &mut set)
.is_succeed()
&& validate_field_has_resolver(name, field, &config.types, &mut set).is_succeed()
{
b_field = b_field.resolver(Some(IR::Dynamic(DynamicValue::Value(
ConstValue::Object(Default::default()),
Expand Down
17 changes: 4 additions & 13 deletions src/core/blueprint/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,35 @@ fn validate_type_has_resolvers(
types: &BTreeMap<String, Type>,
visited: &mut HashSet<String>,
) -> Valid<(), String> {
if visited.contains(name) {
if ty.scalar() || visited.contains(name) {
return Valid::succeed(());
}

visited.insert(name.to_string());

Valid::from_iter(ty.fields.iter(), |(name, field)| {
validate_field_has_resolver(name, field, types, ty, visited)
validate_field_has_resolver(name, field, types, visited)
})
.trace(name)
.unit()
}

#[allow(clippy::too_many_arguments)]
pub fn validate_field_has_resolver(
name: &str,
field: &Field,
types: &BTreeMap<String, Type>,
parent_ty: &Type,
visited: &mut HashSet<String>,
) -> Valid<(), String> {
Valid::<(), String>::fail("No resolver has been found in the schema".to_owned())
.when(|| {
if types.get(&field.type_of).eq(&Some(parent_ty)) {
return true;
}
if !field.has_resolver() {
let type_name = &field.type_of;
if let Some(ty) = types.get(type_name) {
if ty.scalar() {
return true;
}
let res = validate_type_has_resolvers(type_name, ty, types, visited);
return !res.is_succeed();
} else {
// It's a Scalar
return true;
}

return true;
}
false
})
Expand Down
26 changes: 0 additions & 26 deletions tests/core/snapshots/recursive-type-json.md_0.snap

This file was deleted.

51 changes: 0 additions & 51 deletions tests/core/snapshots/recursive-type.md_client.snap

This file was deleted.

17 changes: 0 additions & 17 deletions tests/core/snapshots/recursive-type.md_merged.snap

This file was deleted.

41 changes: 41 additions & 0 deletions tests/core/snapshots/recursive-types-json.md_0.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
source: tests/core/spec.rs
expression: response
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": {
"user": {
"name": "User1",
"id": 1,
"connections": [
{
"type": "friend",
"user": {
"name": "User2",
"id": 2,
"connections": [
{
"user": {
"name": "User3",
"id": 3
}
},
{
"user": {
"name": "User4",
"id": 4
}
}
]
}
}
]
}
}
}
}
27 changes: 27 additions & 0 deletions tests/core/snapshots/recursive-types-json.md_1.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: tests/core/spec.rs
expression: response
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": {
"createUser": {
"name": "NewUser",
"id": 111,
"connections": [
{
"type": "friend",
"user": {
"name": "User1",
"id": 1
}
}
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ expression: formatted
---
scalar Bytes

type Connection {
type: String
user: User
}

input ConnectionInput {
type: String
user: UserInput
}

scalar Date

scalar Email
Expand All @@ -22,6 +32,10 @@ scalar Int8

scalar JSON

type Mutation {
createUser(user: UserInput): User
}

scalar PhoneNumber

type Query {
Expand All @@ -41,11 +55,18 @@ scalar UInt8
scalar Url

type User {
friend: User
connections: [Connection]
id: Int!
name: String!
}

input UserInput {
connections: [ConnectionInput]
id: Int!
name: String!
}

schema {
query: Query
mutation: Mutation
}
38 changes: 38 additions & 0 deletions tests/core/snapshots/recursive-types-json.md_merged.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
source: tests/core/spec.rs
expression: formatter
---
schema @server @upstream(baseURL: "https://jsonplaceholder.typicode.com", httpCache: 42) {
query: Query
mutation: Mutation
}

input ConnectionInput {
type: String
user: UserInput
}

input UserInput {
connections: [ConnectionInput] @http(path: "/connections/{{.value.id}}")
id: Int!
name: String!
}

type Connection {
type: String
user: User
}

type Mutation {
createUser(user: UserInput): User @http(body: "{{.args.user}}", method: "POST", path: "/user")
}

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

type User {
connections: [Connection] @http(path: "/connections/{{.value.id}}")
id: Int!
name: String!
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ expression: errors
"message": "No resolver has been found in the schema",
"trace": [
"Query",
"foo"
"user"
],
"description": null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
source: tests/core/spec.rs
expression: formatter
---
schema @server @upstream(baseURL: "https://jsonplaceholder.typicode.com", httpCache: 42) {
schema @server @upstream(baseURL: "https://jsonplaceholder.typicode.com") {
query: Query
}

type Connection {
type: String
user: User
}

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

type User {
friend: User @http(path: "/friends/1")
connections: [Connection]
id: Int!
name: String!
name: String
}
29 changes: 22 additions & 7 deletions tests/core/snapshots/recursive-types.md_0.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,29 @@ expression: response
"user": {
"name": "User1",
"id": 1,
"friend": {
"name": "User2",
"id": 2,
"friend": {
"name": "User2",
"id": 2
"connections": [
{
"type": "friend",
"user": {
"name": "User2",
"id": 2,
"connections": [
{
"user": {
"name": "User3",
"id": 3
}
},
{
"user": {
"name": "User4",
"id": 4
}
}
]
}
}
}
]
}
}
}
Expand Down
Loading

1 comment on commit 92455fd

@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 7.33ms 3.34ms 85.56ms 72.00%
Req/Sec 3.45k 181.62 4.17k 90.67%

412126 requests in 30.02s, 2.07GB read

Requests/sec: 13730.19

Transfer/sec: 70.47MB

Please sign in to comment.