Skip to content

Commit

Permalink
Merge pull request #67 from evo-company/refactor-endpoint-classes-inh…
Browse files Browse the repository at this point in the history
…eritance

refactor endpoints classes inheritance
  • Loading branch information
kindermax committed Jul 28, 2022
2 parents 3394a02 + 2ad2482 commit c15b9a1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
32 changes: 26 additions & 6 deletions hiku/endpoint/graphql.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
from abc import ABC, abstractmethod
from asyncio import gather
from typing import Dict

from ..graph import apply
from ..graph import (
apply,
Graph,
)
from ..query import QueryTransformer
from ..validate.query import validate
from ..readers.graphql import read_operation, OperationType
from ..readers.graphql import (
read_operation,
OperationType,
Operation,
)
from ..denormalize.graphql import DenormalizeGraphQL
from ..introspection.graphql import AsyncGraphQLIntrospection, QUERY_ROOT_NAME
from ..introspection.graphql import GraphQLIntrospection, MUTATION_ROOT_NAME
Expand Down Expand Up @@ -76,16 +84,28 @@ def __init__(self, engine, query_graph, mutation_graph=None):
else:
self.mutation_graph = None


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

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


class BaseAsyncGraphQLEndpoint(BaseGraphQLEndpoint):
@abstractmethod
async def execute(self, graph: Graph, op: Operation, ctx: Dict) -> Dict:
pass

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


class GraphQLEndpoint(BaseGraphQLEndpoint):
class GraphQLEndpoint(BaseSyncGraphQLEndpoint):
introspection_cls = GraphQLIntrospection

def execute(self, graph, op, ctx):
Expand Down Expand Up @@ -117,7 +137,7 @@ def dispatch(self, data):
return super(BatchGraphQLEndpoint, self).dispatch(data)


class AsyncGraphQLEndpoint(BaseGraphQLEndpoint):
class AsyncGraphQLEndpoint(BaseAsyncGraphQLEndpoint):
introspection_cls = AsyncGraphQLIntrospection

async def execute(self, graph, op, ctx):
Expand Down
37 changes: 18 additions & 19 deletions hiku/federation/endpoint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from abc import abstractmethod
from abc import ABC
from asyncio import gather
from contextlib import contextmanager
from typing import (
Expand All @@ -9,6 +9,7 @@
Tuple,
overload,
Iterator,
Union,
)

from .utils import get_keys
Expand All @@ -23,11 +24,13 @@
from hiku.denormalize.graphql import DenormalizeGraphQL
from hiku.federation.denormalize import DenormalizeEntityGraphQL
from hiku.endpoint.graphql import (
BaseGraphQLEndpoint,
BaseSyncGraphQLEndpoint,
BaseAsyncGraphQLEndpoint,
_type_names,
_switch_graph,
GraphQLError,
_StripQuery,
BaseGraphQLEndpoint,
)
from hiku.graph import Graph
from hiku.query import Node
Expand Down Expand Up @@ -70,17 +73,7 @@ def denormalize_entities(
return entities


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

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

class BaseFederatedGraphEndpoint(BaseGraphQLEndpoint, ABC):
@contextmanager
def context(self, op: Operation) -> Iterator[Dict]:
yield {}
Expand All @@ -104,7 +97,9 @@ def postprocess_result(result: Proxy, graph: Graph, op: Operation) -> Dict:
return data


class FederatedGraphQLEndpoint(BaseFederatedGraphEndpoint):
class FederatedGraphQLEndpoint(
BaseSyncGraphQLEndpoint, BaseFederatedGraphEndpoint
):
"""Can execute either regular or federated queries.
Handles following fields of federated query:
- _service
Expand All @@ -131,17 +126,19 @@ def dispatch(self, data: Dict) -> Dict:
return {'errors': [{'message': e} for e in e.errors]}


class AsyncFederatedGraphQLEndpoint(BaseFederatedGraphEndpoint):
class AsyncFederatedGraphQLEndpoint(
BaseAsyncGraphQLEndpoint, BaseFederatedGraphEndpoint
):
introspection_cls = AsyncFederatedGraphQLIntrospection

async def execute( # type: ignore[override]
async def execute(
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: Dict) -> Dict: # type: ignore[override]
async def dispatch(self, data: Dict) -> Dict:
try:
graph, op = _switch_graph(
data, self.query_graph, self.mutation_graph,
Expand All @@ -155,15 +152,17 @@ async def dispatch(self, data: Dict) -> Dict: # type: ignore[override]


class AsyncBatchFederatedGraphQLEndpoint(AsyncFederatedGraphQLEndpoint):
@overload # type: ignore[override]
@overload
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]
async def dispatch(
self, data: Union[Dict, List[Dict]]
) -> Union[Dict, Tuple[Dict, ...]]:
if isinstance(data, list):
return await gather(*(
super().dispatch(item)
Expand Down
9 changes: 4 additions & 5 deletions hiku/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@


class AbstractBase(ABC):

@abstractmethod
def accept(self, visitor):
def accept(self, visitor: 'AbstractGraphVisitor') -> None:
pass


class AbstractOption(AbstractBase):
class AbstractOption(AbstractBase, ABC):
pass


Expand Down Expand Up @@ -80,11 +79,11 @@ def __init__(
self.default = default
self.description = description

def __repr__(self):
def __repr__(self) -> str:
return '{}({!r}, {!r}, ...)'.format(self.__class__.__name__,
self.name, self.type)

def accept(self, visitor):
def accept(self, visitor: 'AbstractGraphVisitor') -> t.Any:
return visitor.visit_option(self)


Expand Down

0 comments on commit c15b9a1

Please sign in to comment.