Repository: mirumee/ariadne-codegen
Pull request #147 (2023-05-16 13:59:04)

Fix ResultTypesGenerator to call generate_result_class only once for every class
This pr fixes an issue with `generate_result_class` hook. Instead of being called once for model it was called once for each  model's field. Also it wasn't called at all for class which only has `pass` in body.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c194e09..c94e82b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,7 @@
 
 - Added support for subscriptions as async generators.
 - Changed how fragments are handled to generate separate module with fragments as mixins.
+- Fixed `ResultTypesGenerator` to trigger `generate_result_class` for each result model.
 
 
 ## 0.6.0 (2023-04-18)
diff --git a/ariadne_codegen/client_generators/result_types.py b/ariadne_codegen/client_generators/result_types.py
index a3f895d..cd3cc54 100644
--- a/ariadne_codegen/client_generators/result_types.py
+++ b/ariadne_codegen/client_generators/result_types.py
@@ -249,16 +249,16 @@ class ResultTypesGenerator:
             self._save_used_enums(field_types_names)
             self._save_used_scalars(field_types_names)
 
-            if self.plugin_manager:
-                class_def = self.plugin_manager.generate_result_class(
-                    class_def,
-                    operation_definition=self.operation_definition,
-                    selection_set=selection_set,
-                )
-
         if not class_def.body:
             class_def.body.append(generate_pass())
 
+        if self.plugin_manager:
+            class_def = self.plugin_manager.generate_result_class(
+                class_def,
+                operation_definition=self.operation_definition,
+                selection_set=selection_set,
+            )
+
         return [class_def] + extra_classes
 
     def _resolve_selection_set(


Test patch:
diff --git a/tests/client_generators/result_types_generator/test_plugin_hooks.py b/tests/client_generators/result_types_generator/test_plugin_hooks.py
index 122f14d..b97275d 100644
--- a/tests/client_generators/result_types_generator/test_plugin_hooks.py
+++ b/tests/client_generators/result_types_generator/test_plugin_hooks.py
@@ -120,3 +120,61 @@ def test_generator_triggers_process_name_hook_for_every_field(mocked_plugin_mana
         "field1",
         "fielda",
     }
+
+
+def test_generator_triggers_generate_result_class_hook_for_class_with_empty_body(
+    mocked_plugin_manager,
+):
+    query_str = "query CustomQuery { camelCaseQuery { ...CustomFragment } }"
+
+    ResultTypesGenerator(
+        schema=build_ast_schema(parse(SCHEMA_STR)),
+        operation_definition=cast(
+            OperationDefinitionNode, parse(query_str).definitions[0]
+        ),
+        enums_module_name="enums",
+        plugin_manager=mocked_plugin_manager,
+        fragments_definitions={
+            "CustomFragment": parse(
+                "fragment CustomFragment on CustomType { id }"
+            ).definitions[0]
+        },
+    )
+
+    assert "CustomQueryCamelCaseQuery" in {
+        c.args[0].name for c in mocked_plugin_manager.generate_result_class.mock_calls
+    }
+
+
+def test_generator_triggers_generate_result_class_hook_once_for_class(
+    mocked_plugin_manager,
+):
+    query_str = """
+    query CustomQuery {
+        camelCaseQuery {
+            id
+            field1 {
+                fielda
+            }
+            field2 {
+                fieldb
+            }
+            field3
+            scalarField
+        }
+    }
+    """
+
+    ResultTypesGenerator(
+        schema=build_ast_schema(parse(SCHEMA_STR)),
+        operation_definition=cast(
+            OperationDefinitionNode, parse(query_str).definitions[0]
+        ),
+        enums_module_name="enums",
+        plugin_manager=mocked_plugin_manager,
+    )
+
+    called_classes_names = [
+        c.args[0].name for c in mocked_plugin_manager.generate_result_class.mock_calls
+    ]
+    assert len(called_classes_names) == len(set(called_classes_names))