Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export scalars in sdl #144

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion hiku/federation/scalars.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
from typing import Any
from typing import Any, Callable, List, Type

from hiku.scalar import Scalar, scalar


def federation_version(versions: List[int]) -> Callable[[Type], Type]:
def decorator(cls: Type[_Scalar]) -> Type[_Scalar]:
cls.__federation_versions__ = versions
return cls

return decorator


class _Scalar(Scalar):
"""Implements dummy `parse` and `serialize` methods for scalars."""

__federation_versions__: List[int]

@classmethod
def parse(cls, value: str) -> Any:
return value
Expand All @@ -15,15 +25,18 @@ def serialize(cls, value: str) -> Any:
return value


@federation_version([2])
class _Any(_Scalar):
...


@federation_version([1, 2])
@scalar(name="_FieldSet")
class FieldSet(_Scalar):
...


@federation_version([2])
@scalar(name="link__Import")
class LinkImport(_Scalar):
...
29 changes: 23 additions & 6 deletions hiku/federation/sdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@
GenericMeta,
)


def _name(value: t.Optional[str]) -> t.Optional[ast.NameNode]:
return ast.NameNode(value=value) if value is not None else None


_BUILTIN_DIRECTIVES_NAMES = {
directive.__directive_info__.name for directive in _BUILTIN_DIRECTIVES
}


def _name(value: t.Optional[str]) -> t.Optional[ast.NameNode]:
return ast.NameNode(value=value) if value is not None else None
_BUILTIN_SCALARS = [ast.ScalarTypeDefinitionNode(name=_name("Any"))]


@t.overload
Expand Down Expand Up @@ -237,7 +240,7 @@ def visit_graph(self, graph: Graph) -> List[ast.DefinitionNode]:
if self.mutation_graph
else []
),
self.get_any_scalar(),
*self.export_scalars(),
*self.export_enums(),
*self.export_unions(),
self.get_service_type(),
Expand Down Expand Up @@ -373,8 +376,22 @@ def get_custom_directives(self) -> t.List[ast.DirectiveDefinitionNode]:
)
return directives

def get_any_scalar(self) -> ast.ScalarTypeDefinitionNode:
return ast.ScalarTypeDefinitionNode(name=_name("Any"))
def export_scalars(self) -> t.List[ast.ScalarTypeDefinitionNode]:
scalars = []
for scalar in self.graph.scalars:
if hasattr(scalar, "__federation_versions__"):
if (
self.federation_version
not in scalar.__federation_versions__
): # noqa: E501
continue

scalars.append(
ast.ScalarTypeDefinitionNode(
name=_name(scalar.__type_name__),
)
)
return _BUILTIN_SCALARS + scalars

def export_enums(self) -> t.List[ast.EnumTypeDefinitionNode]:
enums = []
Expand Down
2 changes: 2 additions & 0 deletions tests/test_federation/test_sdl_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def execute(graph, query_string):
}

scalar Any

scalar _FieldSet

union Bucket = Cart

Expand Down
24 changes: 23 additions & 1 deletion tests/test_federation/test_sdl_v2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import textwrap
from typing import Any

from hiku.directives import Location
from hiku.enum import Enum
Expand All @@ -16,6 +17,7 @@
TypeRef,
Optional,
)
from hiku.scalar import Scalar
from hiku.graph import apply

from hiku.federation.graph import FederatedNode, Graph
Expand All @@ -41,6 +43,16 @@ class Custom(FederationSchemaDirective):
...


class Long(Scalar):
@classmethod
def parse(cls, value: Any) -> int:
return int(value)

@classmethod
def serialize(cls, value: Any) -> int:
return int(value)


SaveOrderResultNode = Node(
"SaveOrderResult",
[
Expand All @@ -62,6 +74,7 @@ class Custom(FederationSchemaDirective):
], directives=[Key('id')]),
FederatedNode('CartItem', [
Field('id', Integer, field_resolver),
Field('productId', Long, field_resolver),
], directives=[Key('id', resolvable=False)]),
Root([
Link(
Expand All @@ -83,7 +96,7 @@ class Custom(FederationSchemaDirective):
Union('Bucket', ['Cart'])
], enums=[
Enum('Currency', ['UAH', 'USD'])
])
], scalars=[Long])


MUTATION_GRAPH = Graph.from_graph(
Expand Down Expand Up @@ -135,6 +148,7 @@ class Custom(FederationSchemaDirective):

type CartItem @key(fields: "id", resolvable: false) {
id: Int!
productId: Long!
}

extend type Query {
Expand All @@ -143,6 +157,14 @@ class Custom(FederationSchemaDirective):
%s
scalar Any

scalar Long

scalar _Any

scalar _FieldSet

scalar link__Import

enum Currency {
UAH
USD
Expand Down