Skip to content

Commit 2dee4f2

Browse files
committed
feat: added object_type_name_prefix
1 parent ee1ff97 commit 2dee4f2

File tree

4 files changed

+225
-4
lines changed

4 files changed

+225
-4
lines changed

graphene/types/schema.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def __init__(
9191
subscription=None,
9292
types=None,
9393
auto_camelcase=True,
94+
object_type_name_prefix=None,
9495
):
9596
assert_valid_root_type(query)
9697
assert_valid_root_type(mutation)
@@ -101,9 +102,18 @@ def __init__(
101102
assert is_graphene_type(type_)
102103

103104
self.auto_camelcase = auto_camelcase
105+
self.object_type_name_prefix = object_type_name_prefix
104106

105107
create_graphql_type = self.add_type
106108

109+
self.root_names = []
110+
if query:
111+
self.root_names.append(query._meta.name)
112+
if mutation:
113+
self.root_names.append(mutation._meta.name)
114+
if subscription:
115+
self.root_names.append(subscription._meta.name)
116+
107117
self.query = create_graphql_type(query) if query else None
108118
self.mutation = create_graphql_type(mutation) if mutation else None
109119
self.subscription = create_graphql_type(subscription) if subscription else None
@@ -215,9 +225,15 @@ def interfaces():
215225
else:
216226
is_type_of = graphene_type.is_type_of
217227

228+
name = (
229+
self.object_type_name_prefix.capitalize()
230+
if self.object_type_name_prefix
231+
else ""
232+
) + graphene_type._meta.name
233+
218234
return GrapheneObjectType(
219235
graphene_type=graphene_type,
220-
name=graphene_type._meta.name,
236+
name=name,
221237
description=graphene_type._meta.description,
222238
fields=partial(self.create_fields_for_type, graphene_type),
223239
is_type_of=is_type_of,
@@ -357,7 +373,17 @@ def create_fields_for_type(self, graphene_type, is_input_type=False):
357373
deprecation_reason=field.deprecation_reason,
358374
description=field.description,
359375
)
360-
field_name = field.name or self.get_name(name)
376+
if field.name:
377+
field_name = field.name
378+
else:
379+
if (
380+
self.object_type_name_prefix
381+
and graphene_type._meta.name in self.root_names
382+
):
383+
object_type_name_prefix = self.object_type_name_prefix + "_"
384+
field_name = self.get_name(object_type_name_prefix + name)
385+
else:
386+
field_name = self.get_name(name)
361387
fields[field_name] = _field
362388
return fields
363389

@@ -421,12 +447,18 @@ def __init__(
421447
types=None,
422448
directives=None,
423449
auto_camelcase=True,
450+
object_type_name_prefix=None,
424451
):
425452
self.query = query
426453
self.mutation = mutation
427454
self.subscription = subscription
428455
type_map = TypeMap(
429-
query, mutation, subscription, types, auto_camelcase=auto_camelcase
456+
query,
457+
mutation,
458+
subscription,
459+
types,
460+
auto_camelcase=auto_camelcase,
461+
object_type_name_prefix=object_type_name_prefix,
430462
)
431463
self.graphql_schema = GraphQLSchema(
432464
type_map.query,

graphene/types/tests/test_mutation.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,44 @@ class Query(ObjectType):
218218
)
219219
assert not result.errors
220220
assert result.data == {"createUserWithPlanet": {"name": "Peter", "planet": "earth"}}
221+
222+
223+
def test_object_type_name_prefix():
224+
class BaseCreateUser(Mutation):
225+
class Arguments:
226+
name = String()
227+
228+
name = String()
229+
230+
def mutate(self, info, **args):
231+
return args
232+
233+
class CreateUserWithPlanet(BaseCreateUser):
234+
class Arguments(BaseCreateUser.Arguments):
235+
planet = String()
236+
237+
planet = String()
238+
239+
def mutate(self, info, **args):
240+
return CreateUserWithPlanet(**args)
241+
242+
class MyMutation(ObjectType):
243+
create_user_with_planet = CreateUserWithPlanet.Field()
244+
245+
class Query(ObjectType):
246+
a = String()
247+
248+
schema = Schema(query=Query, mutation=MyMutation, object_type_name_prefix="prefix")
249+
result = schema.execute(
250+
""" mutation mymutation {
251+
prefixCreateUserWithPlanet(name:"Peter", planet: "earth") {
252+
name
253+
planet
254+
}
255+
}
256+
"""
257+
)
258+
assert not result.errors
259+
assert result.data == {
260+
"prefixCreateUserWithPlanet": {"name": "Peter", "planet": "earth"}
261+
}

graphene/types/tests/test_query.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,28 @@ def resolve_user(self, *args, **kwargs):
497497

498498
assert not result.errors
499499
assert result.data == expected
500+
501+
502+
def test_object_type_name_prefix():
503+
class Cat(ObjectType):
504+
name = String()
505+
506+
class User(ObjectType):
507+
name = String()
508+
cat = Field(Cat)
509+
510+
def resolve_cat(self, *args, **kwargs):
511+
return Cat(name="bar")
512+
513+
class Query(ObjectType):
514+
user = Field(User)
515+
516+
def resolve_user(self, *args, **kwargs):
517+
return User(name="foo")
518+
519+
schema = Schema(query=Query, object_type_name_prefix="prefix")
520+
expected = {"prefixUser": {"name": "foo", "cat": {"name": "bar"}}}
521+
result = schema.execute("{ prefixUser { name cat { name } } }")
522+
523+
assert not result.errors
524+
assert result.data == expected

graphene/types/tests/test_schema.py

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,43 @@
55
from graphql.type import GraphQLObjectType, GraphQLSchema
66

77
from ..field import Field
8+
from ..mutation import Mutation
89
from ..objecttype import ObjectType
9-
from ..scalars import String
10+
from ..scalars import Int, String
1011
from ..schema import Schema
1112

1213

14+
class MyType(ObjectType):
15+
field = String()
16+
17+
1318
class MyOtherType(ObjectType):
1419
field = String()
20+
my_type = Field(MyType)
1521

1622

1723
class Query(ObjectType):
1824
inner = Field(MyOtherType)
1925

2026

27+
class CreateUser(Mutation):
28+
class Arguments:
29+
name = String()
30+
31+
name = String()
32+
33+
def mutate(self, info, name):
34+
return CreateUser(name=name)
35+
36+
37+
class Mutation(ObjectType):
38+
create_user = CreateUser.Field()
39+
40+
41+
class Subscription(ObjectType):
42+
count_to_ten = Field(Int)
43+
44+
2145
def test_schema():
2246
schema = Schema(Query)
2347
graphql_schema = schema.graphql_schema
@@ -54,6 +78,11 @@ def test_schema_str():
5478
5579
type MyOtherType {
5680
field: String
81+
myType: MyType
82+
}
83+
84+
type MyType {
85+
field: String
5786
}
5887
"""
5988
).strip()
@@ -72,3 +101,97 @@ def test_schema_requires_query_type():
72101
assert len(result.errors) == 1
73102
error = result.errors[0]
74103
assert error.message == "Query root type must be provided."
104+
105+
106+
def test_schema_object_type_name_prefix_camelcase():
107+
schema = Schema(
108+
Query,
109+
Mutation,
110+
Subscription,
111+
auto_camelcase=True,
112+
object_type_name_prefix="prefix",
113+
)
114+
assert (
115+
str(schema).strip()
116+
== dedent(
117+
"""
118+
schema {
119+
query: PrefixQuery
120+
mutation: PrefixMutation
121+
subscription: PrefixSubscription
122+
}
123+
124+
type PrefixQuery {
125+
prefixInner: PrefixMyOtherType
126+
}
127+
128+
type PrefixMyOtherType {
129+
field: String
130+
myType: PrefixMyType
131+
}
132+
133+
type PrefixMyType {
134+
field: String
135+
}
136+
137+
type PrefixMutation {
138+
prefixCreateUser(name: String): PrefixCreateUser
139+
}
140+
141+
type PrefixCreateUser {
142+
name: String
143+
}
144+
145+
type PrefixSubscription {
146+
prefixCountToTen: Int
147+
}
148+
"""
149+
).strip()
150+
)
151+
152+
153+
def test_schema_object_type_name_prefix_camelcase_disabled():
154+
schema = Schema(
155+
Query,
156+
Mutation,
157+
Subscription,
158+
auto_camelcase=False,
159+
object_type_name_prefix="prefix",
160+
)
161+
assert (
162+
str(schema).strip()
163+
== dedent(
164+
"""
165+
schema {
166+
query: PrefixQuery
167+
mutation: PrefixMutation
168+
subscription: PrefixSubscription
169+
}
170+
171+
type PrefixQuery {
172+
prefix_inner: PrefixMyOtherType
173+
}
174+
175+
type PrefixMyOtherType {
176+
field: String
177+
my_type: PrefixMyType
178+
}
179+
180+
type PrefixMyType {
181+
field: String
182+
}
183+
184+
type PrefixMutation {
185+
prefix_create_user(name: String): PrefixCreateUser
186+
}
187+
188+
type PrefixCreateUser {
189+
name: String
190+
}
191+
192+
type PrefixSubscription {
193+
prefix_count_to_ten: Int
194+
}
195+
"""
196+
).strip()
197+
)

0 commit comments

Comments
 (0)