diff --git a/nextlinegraphql/plugins/dev/graphql/__init__.py b/nextlinegraphql/plugins/dev/graphql/__init__.py new file mode 100644 index 0000000..091987e --- /dev/null +++ b/nextlinegraphql/plugins/dev/graphql/__init__.py @@ -0,0 +1,19 @@ +from pathlib import Path + +from graphql import parse, print_ast + + +def read_gql(path: Path | str) -> str: + '''Load a GraphQL query from a file while checking its syntax.''' + + text = Path(path).read_text() + parsed = parse(text) + reformatted = print_ast(parsed) + return reformatted + + +pwd = Path(__file__).resolve().parent + + +sub = pwd / 'queries' +QUERY_HEADERS = read_gql(sub / 'Headers.gql') diff --git a/nextlinegraphql/plugins/dev/graphql/queries/Headers.gql b/nextlinegraphql/plugins/dev/graphql/queries/Headers.gql new file mode 100644 index 0000000..bd292ff --- /dev/null +++ b/nextlinegraphql/plugins/dev/graphql/queries/Headers.gql @@ -0,0 +1,5 @@ +query DevHeaders { + dev { + headers + } +} \ No newline at end of file diff --git a/nextlinegraphql/plugins/dev/schema/__init__.py b/nextlinegraphql/plugins/dev/schema/__init__.py index f177b73..64e8218 100644 --- a/nextlinegraphql/plugins/dev/schema/__init__.py +++ b/nextlinegraphql/plugins/dev/schema/__init__.py @@ -1,3 +1,6 @@ __all__ = ['Query'] from .query import Query + +Mutation = None +Subscription = None diff --git a/tests/plugins/ctrl/schema/test_run.py b/tests/plugins/ctrl/schema/test_run.py index a6572b0..a7297f6 100644 --- a/tests/plugins/ctrl/schema/test_run.py +++ b/tests/plugins/ctrl/schema/test_run.py @@ -100,7 +100,7 @@ async def _subscribe_state(schema: Schema, context: Any) -> list[str]: ret = [] sub = await schema.subscribe(SUBSCRIBE_STATE, context_value=context) assert hasattr(sub, '__aiter__') - async for result in sub: + async for result in sub: # pragma: no branch assert (data := result.data) state = data['ctrlState'] ret.append(state) @@ -138,7 +138,7 @@ async def _control_execution(schema: Schema, context: Any) -> None: agen = agen_with_wait(sub) prev_ids = set[int]() - async for result in agen: + async for result in agen: # pragma: no branch assert isinstance(result, ExecutionResult) assert (data := result.data) trace_ids: list[int] = data['ctrlTraceIds'] diff --git a/tests/plugins/dev/__init__.py b/tests/plugins/dev/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/plugins/dev/schema/__init__.py b/tests/plugins/dev/schema/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/plugins/dev/schema/conftest.py b/tests/plugins/dev/schema/conftest.py new file mode 100644 index 0000000..4e3db38 --- /dev/null +++ b/tests/plugins/dev/schema/conftest.py @@ -0,0 +1,14 @@ +import pytest +from strawberry import Schema + +from nextlinegraphql.plugins.dev.schema import Mutation, Query, Subscription + + +@pytest.fixture(scope='session') +def schema() -> Schema: + '''GraphQL schema + + The scope is `session` because Hypothesis doesn't allow function-scoped + fixtures. This is fine as the schema is stateless. + ''' + return Schema(query=Query, mutation=Mutation, subscription=Subscription) diff --git a/tests/plugins/dev/schema/queries/__init__.py b/tests/plugins/dev/schema/queries/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/plugins/dev/schema/queries/test_headers.py b/tests/plugins/dev/schema/queries/test_headers.py new file mode 100644 index 0000000..3a14e87 --- /dev/null +++ b/tests/plugins/dev/schema/queries/test_headers.py @@ -0,0 +1,18 @@ +import json +from unittest.mock import Mock + +from starlette.requests import Headers, Request + +from nextlinegraphql.plugins.dev.graphql import QUERY_HEADERS +from tests.plugins.dev.schema.conftest import Schema + + +async def test_schema(schema: Schema) -> None: + request = Mock(spec=Request) + request.headers = Headers({'foo': 'bar'}) + context = {'request': request} + result = await schema.execute(QUERY_HEADERS, context_value=context) + assert (data := result.data) + expected = {'dev': {'headers': json.dumps({'foo': 'bar'})}} + assert data == expected +