|
2 | 2 |
|
3 | 3 | from typing import TYPE_CHECKING |
4 | 4 |
|
5 | | -from fastapi import APIRouter, Depends |
| 5 | +from fastapi import APIRouter, Depends, Query |
6 | 6 | from fastapi.responses import PlainTextResponse |
7 | | -from graphql import print_schema |
| 7 | +from graphql import parse, print_ast, print_schema |
8 | 8 | from starlette.routing import Route, WebSocketRoute |
9 | 9 |
|
10 | 10 | from infrahub.api.dependencies import get_branch_dep, get_current_user |
11 | 11 | from infrahub.core import registry |
12 | 12 | from infrahub.graphql.registry import registry as graphql_registry |
| 13 | +from infrahub.graphql.schema_sort import sort_schema_ast |
13 | 14 |
|
14 | 15 | from .dependencies import build_graphql_app |
15 | 16 |
|
|
27 | 28 | router.routes.append(WebSocketRoute(path="/graphql/{branch_name:str}", endpoint=graphql_app)) |
28 | 29 |
|
29 | 30 |
|
30 | | -@router.get("/schema.graphql", include_in_schema=False) |
| 31 | +@router.get("/schema.graphql") |
31 | 32 | async def get_graphql_schema( |
32 | | - branch: Branch = Depends(get_branch_dep), _: AccountSession = Depends(get_current_user) |
| 33 | + branch: Branch = Depends(get_branch_dep), |
| 34 | + _: AccountSession = Depends(get_current_user), |
| 35 | + sort_schema: bool = Query(default=False, alias="sorted", description="Whether to sort the schema alphabetically."), |
33 | 36 | ) -> PlainTextResponse: |
34 | 37 | schema_branch = registry.schema.get_schema_branch(name=branch.name) |
35 | 38 | gqlm = graphql_registry.get_manager_for_branch(branch=branch, schema_branch=schema_branch) |
36 | 39 | graphql_schema = gqlm.get_graphql_schema() |
| 40 | + |
| 41 | + if sort_schema: |
| 42 | + schema_str = print_schema(graphql_schema) |
| 43 | + schema_ast = parse(schema_str) |
| 44 | + sorted_schema_ast = sort_schema_ast(schema_ast) |
| 45 | + return PlainTextResponse(content=print_ast(sorted_schema_ast)) |
| 46 | + |
37 | 47 | return PlainTextResponse(content=print_schema(graphql_schema)) |
0 commit comments