Skip to content

Commit

Permalink
fix: handle plural singular type name as one. (#2178)
Browse files Browse the repository at this point in the history
Co-authored-by: Ranjit Mahadik <43403528+ranjitmahadik@users.noreply.github.com>
  • Loading branch information
laststylebender14 and laststylebender14 committed Jun 14, 2024
1 parent c4dfd8b commit 3d91f58
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: src/core/config/transformer/type_name_generator.rs
expression: transformed_config.to_sdl()
---
schema @server(hostname: "0.0.0.0", port: 8000) @upstream(baseURL: "http://example.typicode.com", httpCache: 42) {
query: Query
}

type Color {
colors: [T3]
isColorPageExists: Boolean
isColorsImageAvailable: Boolean
}

type F1 {
color: Color
}

type Query {
f1: F1 @http(path: "/colors")
}

type T3 {
hexCode: String
name: String
}
22 changes: 18 additions & 4 deletions src/core/config/transformer/type_name_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ impl<'a> CandidateConvergence<'a> {
if let Some((candidate_name, _)) = candidate_list
.iter()
.filter(|(candidate_name, _)| {
!converged_candidate_set.contains(candidate_name)
&& !self.config.types.contains_key(*candidate_name)
let singularized_candidate_name = candidate_name.to_singular().to_pascal_case();
!converged_candidate_set.contains(&singularized_candidate_name)
&& !self.config.types.contains_key(&singularized_candidate_name)
})
.max_by_key(|&(_, candidate)| (candidate.frequency, candidate.priority))
{
let singularized_candidate_name = candidate_name.to_singular().to_pascal_case();
finalized_candidates.insert(type_name.to_owned(), singularized_candidate_name);
converged_candidate_set.insert(candidate_name);
finalized_candidates
.insert(type_name.to_owned(), singularized_candidate_name.clone());
converged_candidate_set.insert(singularized_candidate_name);
}
}

Expand Down Expand Up @@ -178,4 +180,16 @@ mod test {

Ok(())
}

#[test]
fn test_type_name_generator() -> anyhow::Result<()> {
let config = Config::from_sdl(read_fixture(configs::NAME_GENERATION).as_str())
.to_result()
.unwrap();

let transformed_config = TypeNameGenerator.transform(config).to_result().unwrap();
insta::assert_snapshot!(transformed_config.to_sdl());

Ok(())
}
}
21 changes: 21 additions & 0 deletions tailcall-fixtures/fixtures/configs/name-generation.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
schema @server(port: 8000, hostname: "0.0.0.0") @upstream(baseURL: "http://example.typicode.com", httpCache: 42) {
query: Query
}
type T3 {
name: String
hexCode: String
}

type T2 {
colors: [T3]
isColorPageExists: Boolean
isColorsImageAvailable: Boolean
}

type T1 {
color: T2
}

type Query {
f1: T1 @http(path: "/colors")
}

1 comment on commit 3d91f58

@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.13ms 80.56ms 72.17%
Req/Sec 3.79k 356.21 14.29k 95.75%

452541 requests in 30.10s, 2.27GB read

Requests/sec: 15034.52

Transfer/sec: 77.17MB

Please sign in to comment.