diff --git a/src/core/blueprint/blueprint.rs b/src/core/blueprint/blueprint.rs index 6787416241..decd19d822 100644 --- a/src/core/blueprint/blueprint.rs +++ b/src/core/blueprint/blueprint.rs @@ -101,6 +101,7 @@ pub struct EnumValueDefinition { pub description: Option, pub name: String, pub directives: Vec, + pub alias: BTreeSet, } #[derive(Clone, Debug, Default)] diff --git a/src/core/blueprint/definitions.rs b/src/core/blueprint/definitions.rs index 3d10ae325e..1ad469abfa 100644 --- a/src/core/blueprint/definitions.rs +++ b/src/core/blueprint/definitions.rs @@ -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(), }) diff --git a/src/core/blueprint/index.rs b/src/core/blueprint/index.rs index 3631b932ad..c32c4dd760 100644 --- a/src/core/blueprint/index.rs +++ b/src/core/blueprint/index.rs @@ -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 } diff --git a/src/core/blueprint/snapshots/tailcall__core__blueprint__index__test__from_blueprint.snap b/src/core/blueprint/snapshots/tailcall__core__blueprint__index__test__from_blueprint.snap index 838c50a7d1..52b1a8e2c9 100644 --- a/src/core/blueprint/snapshots/tailcall__core__blueprint__index__test__from_blueprint.snap +++ b/src/core/blueprint/snapshots/tailcall__core__blueprint__index__test__from_blueprint.snap @@ -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: {}, }, ], }, diff --git a/src/core/config/directives/alias.rs b/src/core/config/directives/alias.rs index 2e2a5562ff..f122448015 100644 --- a/src/core/config/directives/alias.rs +++ b/src/core/config/directives/alias.rs @@ -5,6 +5,7 @@ use tailcall_macros::{DirectiveDefinition, MergeRight}; /// The @alias directive indicates that aliases of one enum value. #[derive( + Default, Serialize, Deserialize, Clone, diff --git a/tests/core/snapshots/test-alias-on-enum.md_0.snap b/tests/core/snapshots/test-alias-on-enum.md_0.snap new file mode 100644 index 0000000000..3865a02a82 --- /dev/null +++ b/tests/core/snapshots/test-alias-on-enum.md_0.snap @@ -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" + ] + } + } + } +} diff --git a/tests/core/snapshots/test-alias-on-enum.md_client.snap b/tests/core/snapshots/test-alias-on-enum.md_client.snap new file mode 100644 index 0000000000..21dadeb53d --- /dev/null +++ b/tests/core/snapshots/test-alias-on-enum.md_client.snap @@ -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 +} diff --git a/tests/core/snapshots/test-alias-on-enum.md_merged.snap b/tests/core/snapshots/test-alias-on-enum.md_merged.snap new file mode 100644 index 0000000000..d03a191f48 --- /dev/null +++ b/tests/core/snapshots/test-alias-on-enum.md_merged.snap @@ -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"]}) +} diff --git a/tests/execution/test-alias-on-enum.md b/tests/execution/test-alias-on-enum.md new file mode 100644 index 0000000000..df29b2e942 --- /dev/null +++ b/tests/execution/test-alias-on-enum.md @@ -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 } }" +```