Skip to content

Commit

Permalink
add result field to ExecutionResult
Browse files Browse the repository at this point in the history
  • Loading branch information
m.kindritskiy committed Sep 19, 2024
1 parent 3a7a0ba commit 0bc307e
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 83 deletions.
14 changes: 8 additions & 6 deletions hiku/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Generic,
)

from hiku.result import Proxy
from hiku.cache import CacheSettings
from hiku.context import (
ExecutionContext,
Expand Down Expand Up @@ -58,6 +59,7 @@ def _run_validation(
class ExecutionResult:
data: Optional[Dict[str, Any]]
errors: Optional[List[GraphQLError]]
result: Optional[Proxy]


class Schema(Generic[_ExecutorType]):
Expand Down Expand Up @@ -158,13 +160,13 @@ def execute_sync(
execution_context.operation_type_name,
).process(execution_context.query)

return ExecutionResult(data, None)
return ExecutionResult(data, None, result)
except ValidationError as e:
return ExecutionResult(
None, [GraphQLError(message) for message in e.errors]
None, [GraphQLError(message) for message in e.errors], None
)
except GraphQLError as e:
return ExecutionResult(None, [e])
return ExecutionResult(None, [e], None)

async def execute(
self: "Schema[BaseAsyncExecutor]",
Expand Down Expand Up @@ -213,13 +215,13 @@ async def execute(
execution_context.operation_type_name,
).process(execution_context.query)

return ExecutionResult(data, None)
return ExecutionResult(data, None, result)
except ValidationError as e:
return ExecutionResult(
None, [GraphQLError(message) for message in e.errors]
None, [GraphQLError(message) for message in e.errors], None
)
except GraphQLError as e:
return ExecutionResult(None, [e])
return ExecutionResult(None, [e], None)

def _validate(
self,
Expand Down
12 changes: 3 additions & 9 deletions tests/test_endpoint_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,13 @@ async def answer(ctx, fields):


def test_endpoint(sync_graph):
endpoint = GraphQLEndpoint(
Schema(SyncExecutor(), sync_graph)
)
endpoint = GraphQLEndpoint(Schema(SyncExecutor(), sync_graph))
result = endpoint.dispatch({"query": "{answer}"})
assert result == {"data": {"answer": "42"}}


def test_batch_endpoint(sync_graph):
endpoint = BatchGraphQLEndpoint(
Schema(SyncExecutor(), sync_graph)
)
endpoint = BatchGraphQLEndpoint(Schema(SyncExecutor(), sync_graph))

assert endpoint.dispatch([]) == []

Expand All @@ -63,9 +59,7 @@ def test_batch_endpoint(sync_graph):

@pytest.mark.asyncio
async def test_async_endpoint(async_graph):
endpoint = AsyncGraphQLEndpoint(
Schema(AsyncIOExecutor(), async_graph)
)
endpoint = AsyncGraphQLEndpoint(Schema(AsyncIOExecutor(), async_graph))
result = await endpoint.dispatch(
{"query": "{answer}"}, context={"default_answer": "52"}
)
Expand Down
223 changes: 155 additions & 68 deletions tests/test_validate_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,14 @@ def test_field_complex(field_name):

def test_field_complex_with_typename():
check_errors(
q.Node([
q.Link('val', q.Node([q.Field('__typename'), q.Field('attr')], []))
]), []
q.Node(
[
q.Link(
"val", q.Node([q.Field("__typename"), q.Field("attr")], [])
)
]
),
[],
)


Expand Down Expand Up @@ -609,75 +614,157 @@ def test_distinct_by_name_fields():
]


@pytest.mark.parametrize("query", [
pytest.param(
q.Node([q.Link("x", q.Node([
q.Field("a"),
q.Field("a", options={"e": 1})
]))]),
id="only fields"
),
pytest.param(
q.Node([q.Link("x", q.Node(
[],
[
q.Fragment('Fragment', 'X', q.Node([
q.Field("a"),
q.Field("a", options={"e": 1}),
]))
]))]),
id="only fields inside fragment"
),
pytest.param(
q.Node([q.Link("x", q.Node(
[],
[
q.Fragment('FragmentA', 'X', q.Node([
q.Field("a"),
])),
q.Fragment('FragmentB', 'X', q.Node([
q.Field("a", options={"e": 1}),
])),
]))]),
id="fields inside neighbour fragment"
),
pytest.param(
q.Node([q.Link("x", q.Node(
[
q.Field("a"),
],
[
q.Fragment('Fragment', 'X', q.Node([
q.Field("a", options={"e": 1}),
])),
]))]),
id="field + neighbour fragment"
),
pytest.param(
q.Node([
q.Link("x", q.Node(
[],
@pytest.mark.parametrize(
"query",
[
pytest.param(
q.Node(
[
q.Link(
"x",
q.Node([q.Field("a"), q.Field("a", options={"e": 1})]),
)
]
),
id="only fields",
),
pytest.param(
q.Node(
[
q.Link(
"x",
q.Node(
[],
[
q.Fragment(
"Fragment",
"X",
q.Node(
[
q.Field("a"),
q.Field("a", options={"e": 1}),
]
),
)
],
),
)
]
),
id="only fields inside fragment",
),
pytest.param(
q.Node(
[
q.Fragment('FragmentB', 'X', q.Node([
q.Field("a", options={"e": 1}),
])),
q.Link(
"x",
q.Node(
[],
[
q.Fragment(
"FragmentA",
"X",
q.Node(
[
q.Field("a"),
]
),
),
q.Fragment(
"FragmentB",
"X",
q.Node(
[
q.Field("a", options={"e": 1}),
]
),
),
],
),
)
]
))
], [
q.Fragment('FragmentA', 'Query', q.Node([
q.Link("x", q.Node(
[],
),
id="fields inside neighbour fragment",
),
pytest.param(
q.Node(
[
q.Fragment('FragmentA', 'X', q.Node([
q.Field("a"),
])),
q.Link(
"x",
q.Node(
[
q.Field("a"),
],
[
q.Fragment(
"Fragment",
"X",
q.Node(
[
q.Field("a", options={"e": 1}),
]
),
),
],
),
)
]
))
])),
]),
id="different level fragment, same link"
),
])
),
id="field + neighbour fragment",
),
pytest.param(
q.Node(
[
q.Link(
"x",
q.Node(
[],
[
q.Fragment(
"FragmentB",
"X",
q.Node(
[
q.Field("a", options={"e": 1}),
]
),
),
],
),
)
],
[
q.Fragment(
"FragmentA",
"Query",
q.Node(
[
q.Link(
"x",
q.Node(
[],
[
q.Fragment(
"FragmentA",
"X",
q.Node(
[
q.Field("a"),
]
),
),
],
),
)
]
),
),
],
),
id="different level fragment, same link",
),
],
)
def test_distinct_by_options_fields(query):
graph = Graph(
[
Expand Down

0 comments on commit 0bc307e

Please sign in to comment.