Skip to content

Commit 8985776

Browse files
Bug Fix: Custom Enum (#15)
1 parent 14881c1 commit 8985776

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import graphene
2+
from graphene import ObjectType
3+
from graphql import graphql_sync
4+
5+
from graphene_federation import build_schema, shareable, inaccessible
6+
7+
8+
def test_custom_enum():
9+
class Episode(graphene.Enum):
10+
NEWHOPE = 4
11+
EMPIRE = 5
12+
JEDI = 6
13+
14+
@shareable
15+
class TestCustomEnum(graphene.ObjectType):
16+
test_shareable_scalar = shareable(Episode())
17+
test_inaccessible_scalar = inaccessible(Episode())
18+
19+
class Query(ObjectType):
20+
test = Episode()
21+
test2 = graphene.List(TestCustomEnum, required=True)
22+
23+
schema = build_schema(
24+
query=Query, enable_federation_2=True, types=(TestCustomEnum,)
25+
)
26+
query = """
27+
query {
28+
_service {
29+
sdl
30+
}
31+
}
32+
"""
33+
result = graphql_sync(schema.graphql_schema, query)
34+
assert (
35+
result.data["_service"]["sdl"].strip()
36+
== """extend schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible", "@shareable"])
37+
type TestCustomEnum @shareable {
38+
testShareableScalar: Episode @shareable
39+
testInaccessibleScalar: Episode @inaccessible
40+
}
41+
42+
enum Episode {
43+
NEWHOPE
44+
EMPIRE
45+
JEDI
46+
}
47+
48+
type Query {
49+
test: Episode
50+
test2: [TestCustomEnum]!
51+
}"""
52+
)

graphene_federation/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import graphene
44
from graphene import Schema, ObjectType
55
from graphene.types.definitions import GrapheneObjectType
6+
from graphene.types.enum import EnumOptions
67
from graphene.types.scalars import ScalarOptions
78
from graphene.types.union import UnionOptions
89
from graphene.utils.str_converters import to_camel_case
@@ -93,6 +94,7 @@ def get_attributed_fields(attribute: str, schema: Schema):
9394
not hasattr(type_, "graphene_type")
9495
or isinstance(type_.graphene_type._meta, UnionOptions)
9596
or isinstance(type_.graphene_type._meta, ScalarOptions)
97+
or isinstance(type_.graphene_type._meta, EnumOptions)
9698
):
9799
continue
98100
for field in list(type_.graphene_type._meta.fields):

0 commit comments

Comments
 (0)