-
Notifications
You must be signed in to change notification settings - Fork 35
Clean up GraphQL schema export #7389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
77da14c
Add function to sort a graphql schema
dgarros 8327446
Add command to export infrahub graphql schema
dgarros 9169274
Cleanup old task to generate schema and remove deps to npm
dgarros e29bae8
Update GraphQL Schema
dgarros 9d8b672
Add dev command
dgarros 2ffe965
Ensure all type of nodes are supported
dgarros 84d278a
Add parameter to sort graphql endpoint
dgarros 144e38d
Update commands and schema
dgarros 0009df3
Update infrahub cli doc
dgarros fff20b3
Remove include statement when calling gqlm.generate
dgarros 4528617
Update graphql rule for vale
dgarros bbd7ec0
Fix sidebar
dgarros a077aeb
Update description
dgarros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import importlib | ||
| import logging | ||
| from pathlib import Path # noqa: TC003 | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import typer | ||
| from graphql import parse, print_ast, print_schema | ||
| from infrahub_sdk.async_typer import AsyncTyper | ||
| from rich.logging import RichHandler | ||
|
|
||
| from infrahub import config | ||
| from infrahub.core.initialization import ( | ||
| first_time_initialization, | ||
| initialization, | ||
| ) | ||
| from infrahub.core.schema import SchemaRoot, core_models, internal_schema | ||
| from infrahub.core.schema.schema_branch import SchemaBranch | ||
| from infrahub.core.utils import delete_all_nodes | ||
| from infrahub.graphql.manager import GraphQLSchemaManager | ||
| from infrahub.graphql.schema_sort import sort_schema_ast | ||
| from infrahub.log import get_logger | ||
|
|
||
| if TYPE_CHECKING: | ||
| from infrahub.cli.context import CliContext | ||
|
|
||
| app = AsyncTyper() | ||
|
|
||
|
|
||
| @app.command(name="export-graphql-schema") | ||
| async def export_graphql_schema( | ||
| ctx: typer.Context, # noqa: ARG001 | ||
| config_file: str = typer.Option("infrahub.toml", envvar="INFRAHUB_CONFIG"), | ||
| out: Path = typer.Option("schema.graphql"), # noqa: B008 | ||
| ) -> None: | ||
| """Export the GraphQL schema to a file.""" | ||
|
|
||
| config.load_and_exit(config_file_name=config_file) | ||
|
|
||
| schema = SchemaRoot(**internal_schema) | ||
| full_schema = schema.merge(schema=SchemaRoot(**core_models)) | ||
|
|
||
| schema_branch = SchemaBranch(cache={}, name="default") | ||
| schema_branch.load_schema(schema=full_schema) | ||
|
|
||
| schema_branch.process() | ||
|
|
||
| gqlm = GraphQLSchemaManager(schema=schema_branch) | ||
| gql_schema = gqlm.generate() | ||
|
|
||
| schema_str = print_schema(gql_schema) | ||
| schema_ast = parse(schema_str) | ||
| sorted_schema_ast = sort_schema_ast(schema_ast) | ||
| sorted_schema_str = print_ast(sorted_schema_ast) | ||
|
|
||
| out.write_text(sorted_schema_str) | ||
|
|
||
|
|
||
| @app.command(name="db-init") | ||
| async def database_init( | ||
| ctx: typer.Context, | ||
| config_file: str = typer.Option( | ||
| "infrahub.toml", envvar="INFRAHUB_CONFIG", help="Location of the configuration file to use for Infrahub" | ||
| ), | ||
| ) -> None: | ||
| """Erase the content of the database and initialize it with the core schema.""" | ||
|
|
||
| log = get_logger() | ||
|
|
||
| # -------------------------------------------------- | ||
| # CLEANUP | ||
| # - For now we delete everything in the database | ||
| # TODO, if possible try to implement this in an idempotent way | ||
| # -------------------------------------------------- | ||
|
|
||
| logging.getLogger("neo4j").setLevel(logging.ERROR) | ||
| config.load_and_exit(config_file_name=config_file) | ||
|
|
||
| context: CliContext = ctx.obj | ||
| dbdriver = await context.init_db(retry=1) | ||
| async with dbdriver.start_transaction() as db: | ||
| log.info("Delete All Nodes") | ||
| await delete_all_nodes(db=db) | ||
| await first_time_initialization(db=db) | ||
|
|
||
| await dbdriver.close() | ||
|
|
||
|
|
||
| @app.command(name="load-test-data") | ||
| async def load_test_data( | ||
| ctx: typer.Context, | ||
| config_file: str = typer.Option( | ||
| "infrahub.toml", envvar="INFRAHUB_CONFIG", help="Location of the configuration file to use for Infrahub" | ||
| ), | ||
| dataset: str = "dataset01", | ||
| ) -> None: | ||
| """Load test data into the database from the `test_data` directory.""" | ||
|
|
||
| logging.getLogger("neo4j").setLevel(logging.ERROR) | ||
| config.load_and_exit(config_file_name=config_file) | ||
|
|
||
| context: CliContext = ctx.obj | ||
| dbdriver = await context.init_db(retry=1) | ||
|
|
||
| async with dbdriver.start_session() as db: | ||
| await initialization(db=db) | ||
|
|
||
| log_level = "DEBUG" | ||
|
|
||
| FORMAT = "%(message)s" | ||
| logging.basicConfig(level=log_level, format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]) | ||
| logging.getLogger("infrahub") | ||
|
|
||
| dataset_module = importlib.import_module(f"infrahub.test_data.{dataset}") | ||
| await dataset_module.load_data(db=db) | ||
|
|
||
| await dbdriver.close() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For clarity I think we should still say "Export the core GraphQL schema to a file."