|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections import defaultdict |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import typer |
| 7 | +from ariadne_codegen.client_generators.package import get_package_generator |
| 8 | +from ariadne_codegen.plugins.explorer import get_plugins_types |
| 9 | +from ariadne_codegen.plugins.manager import PluginManager |
| 10 | +from ariadne_codegen.schema import ( |
| 11 | + filter_operations_definitions, |
| 12 | + get_graphql_queries, |
| 13 | + get_graphql_schema_from_path, |
| 14 | +) |
| 15 | +from ariadne_codegen.settings import ClientSettings, CommentsStrategy |
| 16 | +from rich.console import Console |
| 17 | + |
| 18 | +from ..async_typer import AsyncTyper |
| 19 | +from ..ctl.utils import catch_exception |
| 20 | +from .parameters import CONFIG_PARAM |
| 21 | + |
| 22 | +app = AsyncTyper() |
| 23 | +console = Console() |
| 24 | + |
| 25 | +ARIADNE_PLUGINS = ["infrahub_sdk.graphql.plugin.InfrahubPlugin"] |
| 26 | + |
| 27 | + |
| 28 | +def find_gql_files(query_path: Path) -> list[Path]: |
| 29 | + """ |
| 30 | + Find all files with .gql extension in the specified directory. |
| 31 | +
|
| 32 | + Args: |
| 33 | + query_path: Path to the directory to search for .gql files |
| 34 | +
|
| 35 | + Returns: |
| 36 | + List of Path objects for all .gql files found |
| 37 | + """ |
| 38 | + if not query_path.exists(): |
| 39 | + raise FileNotFoundError(f"Directory not found: {query_path}") |
| 40 | + |
| 41 | + if not query_path.is_dir() and query_path.is_file(): |
| 42 | + yield query_path |
| 43 | + |
| 44 | + else: |
| 45 | + yield from query_path.glob("*/*.gql") |
| 46 | + |
| 47 | + |
| 48 | +@app.callback() |
| 49 | +def callback() -> None: |
| 50 | + """ |
| 51 | + Various GraphQL related commands. |
| 52 | + """ |
| 53 | + |
| 54 | + |
| 55 | +@app.command() |
| 56 | +@catch_exception(console=console) |
| 57 | +async def generate_return_types( |
| 58 | + query: Path = typer.Argument(Path.cwd(), help="Location of the GraphQL query file(s)."), |
| 59 | + schema: Path = typer.Option("schema.graphql", help="Path to the GraphQL schema file."), |
| 60 | + _: str = CONFIG_PARAM, |
| 61 | +) -> None: |
| 62 | + """Create Pydantic Models for GraphQL query return types""" |
| 63 | + |
| 64 | + # Load the GraphQL schema |
| 65 | + if not schema.exists(): |
| 66 | + raise FileNotFoundError(f"GraphQL Schema file not found: {schema}") |
| 67 | + graphql_schema = get_graphql_schema_from_path(schema_path=str(schema)) |
| 68 | + |
| 69 | + # Initialize the plugin manager |
| 70 | + plugin_manager = PluginManager( |
| 71 | + schema=graphql_schema, |
| 72 | + plugins_types=get_plugins_types(plugins_strs=ARIADNE_PLUGINS), |
| 73 | + ) |
| 74 | + |
| 75 | + # Find the GraphQL files and organize them by directory |
| 76 | + gql_files = find_gql_files(query) |
| 77 | + gql_per_directory: dict[Path, list[Path]] = defaultdict(list) |
| 78 | + for gql_file in gql_files: |
| 79 | + gql_per_directory[gql_file.parent].append(gql_file) |
| 80 | + |
| 81 | + # Generate the Pydantic Models for the GraphQL queries |
| 82 | + for directory in gql_per_directory.keys(): |
| 83 | + package_generator = get_package_generator( |
| 84 | + schema=graphql_schema, |
| 85 | + fragments=[], |
| 86 | + settings=ClientSettings( |
| 87 | + schema_path=str(schema), |
| 88 | + target_package_name=directory.name, |
| 89 | + queries_path=str(directory), |
| 90 | + include_comments=CommentsStrategy.NONE, |
| 91 | + ), |
| 92 | + plugin_manager=plugin_manager, |
| 93 | + ) |
| 94 | + |
| 95 | + definitions = get_graphql_queries(queries_path=str(directory), schema=graphql_schema) |
| 96 | + queries = filter_operations_definitions(definitions) |
| 97 | + |
| 98 | + for query_operation in queries: |
| 99 | + package_generator.add_operation(query_operation) |
| 100 | + |
| 101 | + package_generator._generate_result_types() |
| 102 | + |
| 103 | + for file_name in package_generator._result_types_files.keys(): |
| 104 | + print(f"Generated {file_name} in {directory}") |
0 commit comments