Skip to content

Commit a0e273d

Browse files
committed
Add parameter to sort graphql endpoint
1 parent cdcb9c2 commit a0e273d

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

backend/infrahub/graphql/api/endpoints.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
from typing import TYPE_CHECKING
44

5-
from fastapi import APIRouter, Depends
5+
from fastapi import APIRouter, Depends, Query
66
from fastapi.responses import PlainTextResponse
7-
from graphql import print_schema
7+
from graphql import parse, print_ast, print_schema
88
from starlette.routing import Route, WebSocketRoute
99

1010
from infrahub.api.dependencies import get_branch_dep, get_current_user
1111
from infrahub.core import registry
1212
from infrahub.graphql.registry import registry as graphql_registry
13+
from infrahub.graphql.schema_sort import sort_schema_ast
1314

1415
from .dependencies import build_graphql_app
1516

@@ -27,11 +28,20 @@
2728
router.routes.append(WebSocketRoute(path="/graphql/{branch_name:str}", endpoint=graphql_app))
2829

2930

30-
@router.get("/schema.graphql", include_in_schema=False)
31+
@router.get("/schema.graphql")
3132
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."),
3336
) -> PlainTextResponse:
3437
schema_branch = registry.schema.get_schema_branch(name=branch.name)
3538
gqlm = graphql_registry.get_manager_for_branch(branch=branch, schema_branch=schema_branch)
3639
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+
3747
return PlainTextResponse(content=print_schema(graphql_schema))

backend/tests/unit/api/test_20_graphql.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,16 @@ async def test_download_schema_anonymous_account(
230230
with client:
231231
response = client.get("/schema.graphql")
232232
assert response.status_code == 200 if allow_anonymous_access else 401
233+
234+
235+
@pytest.mark.parametrize("allow_anonymous_access", [False, True])
236+
async def test_download_graphql_schema_sorted(
237+
db: InfrahubDatabase, client, client_headers, allow_anonymous_access: bool
238+
):
239+
config.SETTINGS.main.allow_anonymous_access = allow_anonymous_access
240+
241+
# Must execute in a with block to execute the startup/shutdown events
242+
with client:
243+
response = client.get("/schema.graphql?sorted=true")
244+
assert response.text
245+
assert response.status_code == 200 if allow_anonymous_access else 401

0 commit comments

Comments
 (0)