Skip to content

Commit

Permalink
introduce dedupe.graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop committed Oct 1, 2024
1 parent 2bdf6a6 commit 99854c2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type Query {
GEN__news__NewsService__GetNews(newsId: Id!): News @grpc(body: "{{.args.newsId}}", method: "news.NewsService.GetNews")
inCompatibleProperties: InCompatibleProperty @http(path: "/")
post(id: Int! = 1): Post @http(path: "/posts/{{.args.id}}")
posts: [Post] @http(path: "/posts?_limit=11", dedupe: true)
posts: [Post] @http(path: "/posts?_limit=11")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
users: [User] @http(path: "/users")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Post {

type Query {
post(id: Int! = 1): Post @http(path: "/posts/{{.args.id}}")
posts: [Post] @http(path: "/posts?_limit=11", dedupe: true)
posts: [Post] @http(path: "/posts?_limit=11")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
users: [User] @http(path: "/users")
}
Expand Down
25 changes: 17 additions & 8 deletions src/core/jit/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,17 +411,28 @@ impl<Input> OperationPlan<Input> {
.map(|f| f.into_nested(&fields))
.collect::<Vec<_>>();

// by default dedupe should be set to false
// then we check if there is any ir with dedupe set to true
// in that case we set is_dedupe to true
let mut is_dedupe = false;

// if there is any ir with dedupe set to false
// in that case we set is_not_dedupe to true
let mut is_not_dedupe = false;
for field in fields.iter() {
if let Some(val) = field.ir.as_ref() {
if let IR::IO(io) = val {
is_dedupe = is_dedupe && !io.dedupe();
if let Some(IR::IO(io)) = field.ir.as_ref() {
if io.dedupe() {
is_dedupe = true;
} else {
is_not_dedupe = true;
}
}
}

let dedupe = !is_dedupe;
// now we check if all the IRs have dedupe set to true
// this method is more accurate then `filter.all(...)`
// because this could handle a list with no elements
let dedupe = is_dedupe && !is_not_dedupe;

Self {
root_name: root_name.to_string(),
Expand Down Expand Up @@ -715,8 +726,7 @@ mod test {
#[test]
fn test_operation_plan_dedupe() {
let config =
include_config!("../../../tailcall-fixtures/fixtures/configs/jsonplaceholder.graphql")
.unwrap();
include_config!("../../../tailcall-fixtures/fixtures/configs/dedupe.graphql").unwrap();
let module = ConfigModule::from(config);
let bp = Blueprint::try_from(&module).unwrap();

Expand All @@ -730,8 +740,7 @@ mod test {
#[test]
fn test_operation_plan_dedupe_false() {
let config =
include_config!("../../../tailcall-fixtures/fixtures/configs/jsonplaceholder.graphql")
.unwrap();
include_config!("../../../tailcall-fixtures/fixtures/configs/dedupe.graphql").unwrap();
let module = ConfigModule::from(config);
let bp = Blueprint::try_from(&module).unwrap();

Expand Down
54 changes: 54 additions & 0 deletions tailcall-fixtures/fixtures/configs/dedupe.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
schema
@server(port: 8000, hostname: "0.0.0.0")
@upstream(baseURL: "http://jsonplaceholder.typicode.com", httpCache: 42) {
query: Query
}

type Query {
posts: [Post] @http(path: "/posts?_limit=11", dedupe: true)
users: [User] @http(path: "/users")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
post(id: Int! = 1): Post @http(path: "/posts/{{.args.id}}")
}

type User {
id: Int!
name: String!
username: String!
email: String!
phone: String
blog: String @expr(body: "https://test.blog/users/website/{{.value.username}}")
albums: [Album] @http(path: "/users/{{.value.id}}/albums?_limit=2")
}

type Post {
id: Int!
userId: Int!
title: String!
body: String!
user: User @http(path: "/users/{{.value.userId}}")
users: [User] @http(path: "/users")
comments: [Comment] @http(path: "/posts/{{.value.id}}/comments")
}

type Comment {
id: Int!
name: String!
email: String!
body: String!
title: String! @expr(body: "{{.value.email}}: {{.value.name}}")
}

type Photo {
albumId: Int!
id: Int!
title: String!
combinedId: String! @expr(body: "Album: {{.value.albumId}}, photo: {{.value.id}}")
}

type Album {
userId: Int!
id: Int!
title: Int
photos: [Photo] @http(path: "/albums/{{.value.id}}/photos?_limit=3")
}
2 changes: 1 addition & 1 deletion tailcall-fixtures/fixtures/configs/jsonplaceholder.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ schema
}

type Query {
posts: [Post] @http(path: "/posts?_limit=11", dedupe: true)
posts: [Post] @http(path: "/posts?_limit=11")
users: [User] @http(path: "/users")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
post(id: Int! = 1): Post @http(path: "/posts/{{.args.id}}")
Expand Down

1 comment on commit 99854c2

@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 3.77ms 6.40ms 126.71ms 98.82%
Req/Sec 7.62k 1.27k 11.39k 73.79%

911242 requests in 30.09s, 187.71MB read

Requests/sec: 30284.58

Transfer/sec: 6.24MB

Please sign in to comment.