Skip to content

Commit

Permalink
Merge pull request #66 from evo-company/typings-federation.enpoint-mo…
Browse files Browse the repository at this point in the history
…dule

typings: federation.endpoint
  • Loading branch information
kindermax committed Jul 27, 2022
2 parents c0ad2c5 + f0a8e04 commit f9d781d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
39 changes: 29 additions & 10 deletions hiku/federation/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
List,
Dict,
Any,
Optional,
Tuple,
overload,
Iterator,
)

from .utils import get_keys
Expand All @@ -28,9 +32,10 @@
from hiku.graph import Graph
from hiku.query import Node
from hiku.result import Proxy, Reference
from hiku.readers.graphql import Operation


def _process_query(graph, query):
def _process_query(graph: Graph, query: Node) -> Node:
stripped_query = _StripQuery().visit(query)
errors = validate(graph, stripped_query)
if errors:
Expand Down Expand Up @@ -67,19 +72,21 @@ def denormalize_entities(

class BaseFederatedGraphEndpoint(BaseGraphQLEndpoint):
@abstractmethod
def execute(self, graph, op, ctx):
def execute(
self, graph: Graph, op: Operation, ctx: Optional[Dict]
) -> Dict:
pass

@abstractmethod
def dispatch(self, data):
def dispatch(self, data: Dict) -> Dict:
pass

@contextmanager
def context(self, op):
def context(self, op: Operation) -> Iterator[Dict]:
yield {}

@staticmethod
def postprocess_result(result: Proxy, graph, op):
def postprocess_result(result: Proxy, graph: Graph, op: Operation) -> Dict:
if '_service' in op.query.fields_map:
return {'_service': {'sdl': result['sdl']}}
elif '_entities' in op.query.fields_map:
Expand All @@ -105,12 +112,14 @@ class FederatedGraphQLEndpoint(BaseFederatedGraphEndpoint):
"""
introspection_cls = FederatedGraphQLIntrospection

def execute(self, graph: Graph, op, ctx):
def execute(
self, graph: Graph, op: Operation, ctx: Optional[Dict]
) -> Dict:
stripped_query = _process_query(graph, op.query)
result = self.engine.execute(graph, stripped_query, ctx)
return self.postprocess_result(result, graph, op)

def dispatch(self, data):
def dispatch(self, data: Dict) -> Dict:
try:
graph, op = _switch_graph(
data, self.query_graph, self.mutation_graph,
Expand All @@ -125,12 +134,14 @@ def dispatch(self, data):
class AsyncFederatedGraphQLEndpoint(BaseFederatedGraphEndpoint):
introspection_cls = AsyncFederatedGraphQLIntrospection

async def execute(self, graph: Graph, op, ctx):
async def execute( # type: ignore[override]
self, graph: Graph, op: Operation, ctx: Optional[Dict]
) -> Dict:
stripped_query = _process_query(graph, op.query)
result = await self.engine.execute_async(graph, stripped_query, ctx)
return self.postprocess_result(result, graph, op)

async def dispatch(self, data):
async def dispatch(self, data: Dict) -> Dict: # type: ignore[override]
try:
graph, op = _switch_graph(
data, self.query_graph, self.mutation_graph,
Expand All @@ -144,7 +155,15 @@ async def dispatch(self, data):


class AsyncBatchFederatedGraphQLEndpoint(AsyncFederatedGraphQLEndpoint):
async def dispatch(self, data):
@overload # type: ignore[override]
async def dispatch(self, data: List[Dict]) -> Tuple[Dict, ...]:
...

@overload
async def dispatch(self, data: Dict) -> Dict:
...

async def dispatch(self, data): # type: ignore[no-untyped-def]
if isinstance(data, list):
return await gather(*(
super().dispatch(item)
Expand Down
9 changes: 7 additions & 2 deletions lets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ commands:
cmd: docker-compose run --rm test-base tox -e flake8

mypy:
description: Run mypy
description: |
Run mypy
Examples:
lets mypy
lets mypy hiku/graph.py
depends: [_build-tests]
cmd: docker-compose run --rm test-base tox -e mypy
cmd: |
docker-compose run --rm test-base tox -e mypy -- $LETS_COMMAND_ARGS
reqs:
description: Update requirements.txt
Expand Down
8 changes: 8 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ strict_optional = True
warn_redundant_casts = True
warn_unused_ignores = True

exclude =
tests
tests_pg

[mypy-hiku.federation.endpoint.*]
disallow_untyped_defs = True
check_untyped_defs = True

[mypy-google.*]
ignore_missing_imports = True

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ basepython = python3
deps = mypy
setenv =
MYPYPATH={toxinidir}
commands = mypy --config-file {toxinidir}/mypy.ini -p hiku
commands = mypy --config-file {toxinidir}/mypy.ini --show-error-codes {posargs:-p hiku}

[pytest]
addopts = -q --tb=native
Expand Down

0 comments on commit f9d781d

Please sign in to comment.