Skip to content

Commit

Permalink
- update rename fields logic
Browse files Browse the repository at this point in the history
  • Loading branch information
laststylebender14 committed Sep 4, 2024
1 parent c4faa43 commit 94718d0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ChatRequest {
ChatMessage {
role: System,
content: Text(
"You're master at suggesting field names for GraphQL type by looking at the URL and it's http method. Given a URL suggest 5 field names for the URL, return the response in following JSON format.\nnote: for fieldName follow camel case convention.\neg.\n\ninput:\nURL: https://jsonplaceholder.typicode.com/posts\nmethod: GET\n\noutput:\n{\n\"suggestions\": [\"posts\", \"postList\", \"articles\", \"articlesList\",\"entries\"]\n}\n\ninput:\nURL: https://jsonplaceholder.typicode.com/posts\nmethod: POST\n\noutput:\n{\n\"suggestions\": [\"createPost\", \"createArticle\", \"createEntry\", \"createNewPost\",\"createNewArticle\"]\n}\n\ninput:\nURL: https://jsonplaceholder.typicode.com/posts/1\nmethod: DELETE\n\noutput:\n{\n\"suggestions\": [\"deletePost\", \"removePost\", \"removePostById\", \"deleteEntry\",\"deleteEntryById\"]\n}\n",
"You're master at suggesting field names for GraphQL type by looking at the URL and it's http method. Given a URL suggest 5 field names for the URL, return the response in following JSON format.\nnote: for fieldName follow camel case convention.\neg.\n\ninput:\n{\"url\":\"https://jsonplaceholder.typicode.com/posts\",\"method\":\"GET\"}\n\noutput:\n{\"suggestions\":[\"posts\",\"postList\",\"articles\",\"articlesList\",\"entries\"]}\n\ninput:\n{\"url\":\"https://jsonplaceholder.typicode.com/posts\",\"method\":\"POST\"}\n\noutput:\n{\"suggestions\":[\"createPost\",\"createArticle\",\"createEntry\",\"createNewPost\",\"createNewArticle\"]}\n\ninput:\n{\"url\":\"https://jsonplaceholder.typicode.com/posts/1\",\"method\":\"DELETE\"}\n\noutput:\n{\"suggestions\":[\"deletePost\",\"removePost\",\"removePostById\",\"deleteEntry\",\"deleteEntryById\"]}\n",
),
extra: None,
},
Expand Down
36 changes: 14 additions & 22 deletions src/core/config/transformer/rename_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,26 @@ impl Transform for RenameFields {
fn transform(&self, mut value: Self::Value) -> Valid<Self::Value, Self::Error> {
Valid::from_iter(self.0.iter(), |(field_name, field_info)| {
let type_name = field_info.type_name.as_str();
let suggested_field_name = field_info.suggestions.iter().find(|suggested_field_name| {
if let Some(type_) = value.find_type(type_name) {
!type_.fields.contains_key(*suggested_field_name)
} else {
false
}
});

match suggested_field_name {
Some(suggested_name) => {
if let Some(type_def) = value.types.get_mut(type_name) {
if let Some(type_def) = value.types.get_mut(type_name) {
let suggested_field_name = field_info.suggestions.iter().find(|name| !type_def.fields.contains_key(*name));

match suggested_field_name {
Some(suggested_name) => {
if let Some(field_def) = type_def.fields.remove(field_name) {
type_def.fields.insert(suggested_name.to_owned(), field_def);
Valid::succeed(())
} else {
Valid::fail(format!(
"Field '{}' not found in type '{}'.",
field_name, type_name
))
Valid::fail(format!("Field '{}' not found in type '{}'.", field_name, type_name))
}
} else {
Valid::fail(format!("Type '{}' not found.", type_name))
}
None => Valid::fail(format!(
"Could not rename field '{}' in type '{}'. All suggested names are already in use.",
field_name, type_name
)),
}
None => Valid::fail(format!(
"Could not rename field name '{}'. All suggested names are already in use.",
field_name
)),
} else {
Valid::fail(format!("Type '{}' not found.", type_name))
}
})
.map(|_| value)
Expand Down Expand Up @@ -202,8 +194,8 @@ mod tests {

let actual = RenameFields::new(mappings).transform(config).to_result();
let expected_error = ValidationError::new(
"Could not rename field name 'old_name'. All suggested names are already in use."
.to_string(),
"Could not rename field 'old_name' in type 'User'. All suggested names are already in use."
.into(),
);

assert!(actual.is_err());
Expand Down

0 comments on commit 94718d0

Please sign in to comment.