Skip to content

Commit

Permalink
chore: @alias Directive for Enum Field (#2969)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop authored Oct 5, 2024
1 parent 1511db7 commit 0a431f9
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/core/blueprint/blueprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub struct EnumValueDefinition {
pub description: Option<String>,
pub name: String,
pub directives: Vec<Directive>,
pub alias: BTreeSet<String>,
}

#[derive(Clone, Debug, Default)]
Expand Down
1 change: 1 addition & 0 deletions src/core/blueprint/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ fn to_enum_type_definition((name, eu): (&String, &Enum)) -> Definition {
description: None,
name: variant.name.clone(),
directives: vec![],
alias: variant.alias.clone().unwrap_or_default().options,
})
.collect(),
})
Expand Down
5 changes: 4 additions & 1 deletion src/core/blueprint/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ impl Index {
let def = self.map.get(type_name).map(|(def, _)| def);

if let Some(Definition::Enum(enum_)) = def {
enum_.enum_values.iter().any(|v| v.name == value)
enum_
.enum_values
.iter()
.any(|v| v.name == value || v.alias.contains(value))
} else {
false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,16 +1192,19 @@ Index {
description: None,
name: "ACTIVE",
directives: [],
alias: {},
},
EnumValueDefinition {
description: None,
name: "INACTIVE",
directives: [],
alias: {},
},
EnumValueDefinition {
description: None,
name: "PENDING",
directives: [],
alias: {},
},
],
},
Expand Down
1 change: 1 addition & 0 deletions src/core/config/directives/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use tailcall_macros::{DirectiveDefinition, MergeRight};

/// The @alias directive indicates that aliases of one enum value.
#[derive(
Default,
Serialize,
Deserialize,
Clone,
Expand Down
21 changes: 21 additions & 0 deletions tests/core/snapshots/test-alias-on-enum.md_0.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: tests/core/spec.rs
expression: response
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": {
"color": {
"departments": [
"ENGINEERING",
"MARKETING",
"HR"
]
}
}
}
}
57 changes: 57 additions & 0 deletions tests/core/snapshots/test-alias-on-enum.md_client.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
source: tests/core/spec.rs
expression: formatted
---
scalar Bytes

type DTA {
departments: [Department]
}

scalar Date

scalar DateTime

enum Department {
ENGINEERING
HUMAN_RESOURCE
MARKETING
}

scalar Email

scalar Empty

scalar Int128

scalar Int16

scalar Int32

scalar Int64

scalar Int8

scalar JSON

scalar PhoneNumber

type Query {
color: DTA
}

scalar UInt128

scalar UInt16

scalar UInt32

scalar UInt64

scalar UInt8

scalar Url

schema {
query: Query
}
21 changes: 21 additions & 0 deletions tests/core/snapshots/test-alias-on-enum.md_merged.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: tests/core/spec.rs
expression: formatter
---
schema @server(batchRequests: true) @upstream(batch: {delay: 1, headers: [], maxSize: 100}) {
query: Query
}

enum Department {
ENGINEERING
HUMAN_RESOURCE @alias(options: ["HR"])
MARKETING
}

type DTA {
departments: [Department]
}

type Query {
color: DTA @expr(body: {departments: ["ENGINEERING", "MARKETING", "HR"]})
}
32 changes: 32 additions & 0 deletions tests/execution/test-alias-on-enum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# test-alias-on-enum

```graphql @config
schema @server(batchRequests: true) @upstream(batch: {delay: 1, headers: [], maxSize: 100}) {
query: Query
}

schema @server(enableJIT: false) {
query: Query
}

enum Department {
ENGINEERING
MARKETING
HUMAN_RESOURCE @alias(options: ["HR"])
}

type Query {
color: DTA @expr(body: {departments: ["ENGINEERING", "MARKETING", "HR"]})
}

type DTA {
departments: [Department]
}
```

```yml @test
- method: POST
url: http://localhost:8080/graphql
body:
query: "query { color { departments } }"
```

1 comment on commit 0a431f9

@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.01ms 3.04ms 45.81ms 72.23%
Req/Sec 3.61k 385.12 4.06k 95.08%

431882 requests in 30.03s, 805.68MB read

Requests/sec: 14380.25

Transfer/sec: 26.83MB

Please sign in to comment.