Skip to content

Commit 163ca01

Browse files
authored
Add plugin hooks for custom_operation modules and functions. (#426)
* allow plugins to hook into creation of custom operations Signed-off-by: Max Chesterfield <max.chesterfield@zepben.com> * tests * formatting * formatting fixes --------- Signed-off-by: Max Chesterfield <max.chesterfield@zepben.com>
1 parent bfe6e4c commit 163ca01

4 files changed

Lines changed: 56 additions & 1 deletion

File tree

ariadne_codegen/client_generators/custom_operation.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ def generate(self) -> ast.Module:
103103
+ cast(list[ast.stmt], self._type_imports)
104104
+ [self._class_def],
105105
)
106+
if self.plugin_manager:
107+
return self.plugin_manager.generate_custom_module(module)
106108
return module
107109

108110
def _add_import(self, import_: Optional[ast.ImportFrom] = None):
@@ -155,7 +157,7 @@ def _generate_method(
155157
return_arguments_keys, return_arguments_values
156158
)
157159

158-
return generate_method_definition(
160+
method = generate_method_definition(
159161
name=process_name(
160162
operation_name,
161163
convert_to_snake_case=self.convert_to_snake_case,
@@ -183,6 +185,10 @@ def _generate_method(
183185
decorator_list=[generate_name("classmethod")],
184186
)
185187

188+
if self.plugin_manager:
189+
return self.plugin_manager.generate_custom_method(method)
190+
return method
191+
186192
def _get_return_type_and_from(self, final_type):
187193
"""
188194
Determines the return type name and its import path based on the final type.

ariadne_codegen/plugins/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,9 @@ def get_file_comment(
145145
self, comment: str, code: str, source: Optional[str] = None
146146
) -> str:
147147
return comment
148+
149+
def generate_custom_module(self, module) -> ast.Module:
150+
return module
151+
152+
def generate_custom_method(self, method_def: ast.FunctionDef) -> ast.FunctionDef:
153+
return method_def

ariadne_codegen/plugins/manager.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,9 @@ def get_file_comment(
213213
return self._apply_plugins_on_object(
214214
"get_file_comment", comment, code=code, source=source
215215
)
216+
217+
def generate_custom_module(self, module: ast.Module) -> ast.Module:
218+
return self._apply_plugins_on_object("generate_custom_module", module)
219+
220+
def generate_custom_method(self, method_def: ast.FunctionDef) -> ast.FunctionDef:
221+
return self._apply_plugins_on_object("generate_custom_method", method_def)

tests/plugins/test_manager.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,40 @@ def test_get_file_comment_calls_plugins_get_file_comment(
411411
plugin1, plugin2 = plugin_manager_with_mocked_plugins.plugins
412412
assert plugin1.get_file_comment.called
413413
assert plugin2.get_file_comment.called
414+
415+
416+
def test_generate_custom_module_calls_plugins_generate_custom_module(
417+
plugin_manager_with_mocked_plugins,
418+
):
419+
plugin_manager_with_mocked_plugins.generate_custom_module(
420+
ast.Module(body=[], type_ignores=[]),
421+
)
422+
423+
plugin1, plugin2 = plugin_manager_with_mocked_plugins.plugins
424+
assert plugin1.generate_custom_module.called
425+
assert plugin2.generate_custom_module.called
426+
427+
428+
def test_generate_custom_method_calls_plugins_generate_custom_method(
429+
plugin_manager_with_mocked_plugins,
430+
):
431+
plugin_manager_with_mocked_plugins.generate_custom_method(
432+
ast.FunctionDef(
433+
name="",
434+
args=ast.arguments(
435+
posonlyargs=[],
436+
args=[],
437+
vararg=None,
438+
kwonlyargs=[],
439+
kw_defaults=[],
440+
kwarg=None,
441+
defaults=[],
442+
),
443+
body=[],
444+
decorator_list=[],
445+
)
446+
)
447+
448+
plugin1, plugin2 = plugin_manager_with_mocked_plugins.plugins
449+
assert plugin1.generate_custom_method.called
450+
assert plugin2.generate_custom_method.called

0 commit comments

Comments
 (0)