From 476b2d2beca0c9d2c8c239779c41e35b68842853 Mon Sep 17 00:00:00 2001 From: laststylebender Date: Thu, 5 Sep 2024 11:10:09 +0530 Subject: [PATCH] - fix tests --- src/core/config/transformer/rename_args.rs | 15 +++++++++++---- ...nsformer__rename_args__tests__rename_args.snap | 4 +++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/core/config/transformer/rename_args.rs b/src/core/config/transformer/rename_args.rs index 68f40a251c..369944cfd8 100644 --- a/src/core/config/transformer/rename_args.rs +++ b/src/core/config/transformer/rename_args.rs @@ -30,11 +30,12 @@ impl Transform for RenameArgs { fn transform(&self, mut config: Self::Value) -> Valid { Valid::from_iter(self.0.iter(), |(existing_arg_name, location)| { - // note: we can use expect on Location type as this type it's impossible to call this function without location being not set. let type_name = location.type_name.as_str(); let field_name = location.field_name.as_str(); let new_argument_name = location.new_argument_name.as_str(); if config.is_root_operation_type(type_name) { + + // We need to ensure we are doing the changes only if `existing_arg_name` is not query param type. let is_safe_operation = config.types.get(type_name) .and_then(|base_type| base_type.fields.get(field_name)) .map(|base_field| { @@ -46,14 +47,18 @@ impl Transform for RenameArgs { }) .unwrap_or(false); - // We need to ensure we are doing the changes only if `existing_arg_name` is not query param type. if is_safe_operation { config.types.values_mut().for_each(|type_| { type_.fields.values_mut().for_each(|field_| { if let Some(Resolver::Call(call)) = field_.resolver.as_mut() { call.steps.iter_mut().for_each(|step| { - if let Some(arg) = step.args.remove(existing_arg_name) { - step.args.insert(new_argument_name.to_string(), arg); + let new_f = field_name.to_owned(); + let is_field_name_matched = step.query.as_ref().eq(&Some(&new_f)) + || step.mutation.as_ref().eq(&Some(&new_f)); + if is_field_name_matched { + if let Some(arg) = step.args.remove(existing_arg_name) { + step.args.insert(new_argument_name.to_string(), arg); + } } }) } @@ -155,6 +160,7 @@ mod tests { } type Query { user(id: ID!): JSON @http(path: "https://jsonplaceholder.typicode.com/users/{{.args.id}}") + userTest(id: ID!): JSON @http(path: "https://jsonplaceholder.typicode.com/users/{{.args.id}}") post(id: ID!): JSON @http(path: "https://jsonplaceholder.typicode.com/posts", query: [{key: "id", value: "{{.args.id}}"}]) id(x: ID!): ID @expr(body: "{{.args.x}}") newsByIdBatch(input: JSON!): JSON! @grpc(method: "news.NewsService.GetMultipleNews", body: "{{args.input}}") @@ -166,6 +172,7 @@ mod tests { userId: ID! postId: ID! user: JSON @call(steps: [{query: "user", args: {id: "{{.value.userId}}"}}]) + userTest: JSON @call(steps: [{query: "userTest", args: {id: "{{.value.userId}}"}}]) post: JSON @call(steps: [{query: "post", args: {id: "{{.value.postId}}"}}]) } "#; diff --git a/src/core/config/transformer/snapshots/tailcall__core__config__transformer__rename_args__tests__rename_args.snap b/src/core/config/transformer/snapshots/tailcall__core__config__transformer__rename_args__tests__rename_args.snap index bc260a4b6f..7f871aa62b 100644 --- a/src/core/config/transformer/snapshots/tailcall__core__config__transformer__rename_args__tests__rename_args.snap +++ b/src/core/config/transformer/snapshots/tailcall__core__config__transformer__rename_args__tests__rename_args.snap @@ -8,10 +8,11 @@ schema @server @upstream { } type Foo { - post: JSON @call(steps: [{query: "post", args: {userId: "{{.value.postId}}"}}]) + post: JSON @call(steps: [{query: "post", args: {id: "{{.value.postId}}"}}]) postId: ID! user: JSON @call(steps: [{query: "user", args: {userId: "{{.value.userId}}"}}]) userId: ID! + userTest: JSON @call(steps: [{query: "userTest", args: {id: "{{.value.userId}}"}}]) } type Mutation { @@ -23,4 +24,5 @@ type Query { newsByIdBatch(newsInput: JSON!): JSON! @grpc(body: "{{args.newsInput}}", method: "news.NewsService.GetMultipleNews") post(id: ID!): JSON @http(path: "https://jsonplaceholder.typicode.com/posts", query: [{key: "id", value: "{{.args.id}}"}]) user(userId: ID!): JSON @http(path: "https://jsonplaceholder.typicode.com/users/{{.args.userId}}") + userTest(id: ID!): JSON @http(path: "https://jsonplaceholder.typicode.com/users/{{.args.id}}") }