Skip to content

Commit

Permalink
Merge pull request #132 from evo-company/export-enums-in-sdl
Browse files Browse the repository at this point in the history
export enums in sdl
  • Loading branch information
kindermax committed Oct 6, 2023
2 parents 114926e + a9b9541 commit c900837
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ supergraph-config.yaml
supergraph.graphql
results.md
htmlcov
.DS_Store
.DS_Store
pyrightconfig.json
18 changes: 18 additions & 0 deletions hiku/federation/sdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
)
from hiku.scalar import ScalarMeta
from hiku.types import (
EnumRefMeta,
IDMeta,
IntegerMeta,
MappingMeta,
Expand Down Expand Up @@ -95,6 +96,8 @@ def _encode(
if input_type:
return f"IO{val.__type_name__}"
return val.__type_name__
elif isinstance(val, EnumRefMeta):
return val.__type_name__
elif isinstance(val, ScalarMeta):
return val.__type_name__
elif isinstance(val, IntegerMeta):
Expand Down Expand Up @@ -235,6 +238,7 @@ def visit_graph(self, graph: Graph) -> List[ast.DefinitionNode]:
else []
),
self.get_any_scalar(),
*self.export_enums(),
*self.export_unions(),
self.get_service_type(),
]:
Expand Down Expand Up @@ -372,6 +376,20 @@ def get_custom_directives(self) -> t.List[ast.DirectiveDefinitionNode]:
def get_any_scalar(self) -> ast.ScalarTypeDefinitionNode:
return ast.ScalarTypeDefinitionNode(name=_name("Any"))

def export_enums(self) -> t.List[ast.EnumTypeDefinitionNode]:
enums = []
for enum in self.graph.enums:
enums.append(
ast.EnumTypeDefinitionNode(
name=_name(enum.name),
values=[
ast.EnumValueDefinitionNode(name=_name(value.name))
for value in enum.values
],
)
)
return enums

def export_unions(self) -> t.List[ast.UnionTypeDefinitionNode]:
unions = []
for union in self.graph.unions:
Expand Down
12 changes: 11 additions & 1 deletion tests/test_federation/test_sdl_v2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import textwrap

from hiku.directives import Location
from hiku.enum import Enum
from hiku.graph import (
Node,
Field,
Expand All @@ -9,7 +10,7 @@
Root, Union,
)
from hiku.types import (
Boolean, Record,
Boolean, EnumRef, Record,
Integer,
String,
TypeRef,
Expand Down Expand Up @@ -57,6 +58,7 @@ class Custom(FederationSchemaDirective):
Field('id', Integer, field_resolver),
Field('status', TypeRef['Status'], field_resolver, description="Cart status"),
Field('_secret', String, field_resolver),
Field('currency', EnumRef['Currency'], field_resolver),
], directives=[Key('id')]),
FederatedNode('CartItem', [
Field('id', Integer, field_resolver),
Expand All @@ -79,6 +81,8 @@ class Custom(FederationSchemaDirective):
}],
}, directives=[Custom], unions=[
Union('Bucket', ['Cart'])
], enums=[
Enum('Currency', ['UAH', 'USD'])
])


Expand Down Expand Up @@ -126,6 +130,7 @@ class Custom(FederationSchemaDirective):
id: Int!
"Cart status"
status: Status!
currency: Currency!
}
type CartItem @key(fields: "id", resolvable: false) {
Expand All @@ -137,6 +142,11 @@ class Custom(FederationSchemaDirective):
}
%s
scalar Any
enum Currency {
UAH
USD
}
union Bucket = Cart
Expand Down

0 comments on commit c900837

Please sign in to comment.