Skip to content

Commit

Permalink
- fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
laststylebender14 committed Sep 5, 2024
1 parent 6b041de commit 476b2d2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/core/config/transformer/rename_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ impl Transform for RenameArgs {

fn transform(&self, mut config: Self::Value) -> Valid<Self::Value, Self::Error> {
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| {
Expand All @@ -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);
}
}
})
}
Expand Down Expand Up @@ -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}}")
Expand All @@ -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}}"}}])
}
"#;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}}")
}

0 comments on commit 476b2d2

Please sign in to comment.