Skip to content

Commit 8cb9791

Browse files
committed
fix: reviews comments
1 parent 0633b62 commit 8cb9791

File tree

7 files changed

+316
-76
lines changed

7 files changed

+316
-76
lines changed

docs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Required library
22
Sphinx==1.5.3
3+
jinja2<3.1.0
34
sphinx-autobuild==0.7.1
45
# Docs template
56
http://graphene-python.org/sphinx_graphene_theme.zip

docs/types/schema.rst

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,136 @@ To disable this behavior, set the ``auto_camelcase`` to ``False`` upon schema in
9292
query=MyRootQuery,
9393
auto_camelcase=False,
9494
)
95+
96+
.. _SchemaTypeNamePrefix:
97+
98+
Type name prefix
99+
--------------------------
100+
101+
You can specify a prefix for all type names in the schema by setting the ``type_name_prefix`` argument upon schema instantiation:
102+
103+
.. code:: python
104+
105+
my_schema = Schema(
106+
query=MyRootQuery,
107+
mutation=MyRootMutation,
108+
subscription=MyRootSubscription
109+
type_name_prefix='MyPrefix',
110+
)
111+
112+
This is useful in a micro-services architecture to prepend the service name to all types and avoid conflicts for example.
113+
114+
The prefix will be added to the name of:
115+
116+
* Query / Mutation / Subscription
117+
* ObjectType
118+
* InputType
119+
* Interface
120+
* Union
121+
* Enum
122+
123+
While fields and arguments name will be left untouched.
124+
125+
More specifically, the following schema:
126+
127+
.. code::
128+
129+
type Query {
130+
inner: MyType
131+
}
132+
133+
type MyType {
134+
field: String
135+
myUnion: MyUnion
136+
myBarType: MyBarType
137+
myFooType: MyFooType
138+
}
139+
140+
union MyUnion = MyBarType | MyFooType
141+
142+
type MyBarType {
143+
field(input: MyInputObjectType): String
144+
myInterface: MyInterface
145+
}
146+
147+
input MyInputObjectType {
148+
field: String
149+
}
150+
151+
interface MyInterface {
152+
field: String
153+
}
154+
155+
type MyFooType {
156+
field: String
157+
myEnum: MyEnum
158+
}
159+
160+
enum MyEnum {
161+
FOO
162+
BAR
163+
}
164+
165+
type Mutation {
166+
createUser(name: String): CreateUser
167+
}
168+
169+
type CreateUser {
170+
name: String
171+
}
172+
173+
type Subscription {
174+
countToTen: Int
175+
}
176+
177+
Will be transformed to:
178+
179+
.. code::
180+
181+
type Query {
182+
myPrefixInner: MyPrefixMyType
183+
}
184+
185+
type MyPrefixMyType {
186+
field: String
187+
myUnion: MyPrefixMyUnion
188+
myBarType: MyPrefixMyBarType
189+
myFooType: MyPrefixMyFooType
190+
}
191+
192+
union MyPrefixMyUnion = MyPrefixMyBarType | MyPrefixMyFooType
193+
194+
type MyPrefixMyBarType {
195+
field(input: MyPrefixMyInputObjectType): String
196+
myInterface: MyPrefixMyInterface
197+
}
198+
199+
input MyPrefixMyInputObjectType {
200+
field: String
201+
}
202+
203+
interface MyPrefixMyInterface {
204+
field: String
205+
}
206+
207+
type MyPrefixMyFooType {
208+
field: String
209+
myEnum: MyPrefixMyEnum
210+
}
211+
212+
enum MyPrefixMyEnum {
213+
FOO
214+
BAR
215+
}
216+
217+
type Mutation {
218+
myPrefixCreateUser(name: String): MyPrefixCreateUser
219+
}
220+
221+
type MyPrefixCreateUser {
222+
name: String
223+
}
224+
225+
type Subscription {
226+
myPrefixCountToTen: Int
227+
}

graphene/types/schema.py

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def __init__(
9191
subscription=None,
9292
types=None,
9393
auto_camelcase=True,
94-
object_type_name_prefix=None,
94+
type_name_prefix=None,
9595
):
9696
assert_valid_root_type(query)
9797
assert_valid_root_type(mutation)
@@ -102,17 +102,17 @@ def __init__(
102102
assert is_graphene_type(type_)
103103

104104
self.auto_camelcase = auto_camelcase
105-
self.object_type_name_prefix = object_type_name_prefix
105+
self.type_name_prefix = type_name_prefix
106106

107107
create_graphql_type = self.add_type
108108

109-
self.root_names = []
109+
self.root_type_names = []
110110
if query:
111-
self.root_names.append(query._meta.name)
111+
self.root_type_names.append(query._meta.name)
112112
if mutation:
113-
self.root_names.append(mutation._meta.name)
113+
self.root_type_names.append(mutation._meta.name)
114114
if subscription:
115-
self.root_names.append(subscription._meta.name)
115+
self.root_type_names.append(subscription._meta.name)
116116

117117
self.query = create_graphql_type(query) if query else None
118118
self.mutation = create_graphql_type(mutation) if mutation else None
@@ -174,8 +174,7 @@ def create_scalar(graphene_type):
174174
parse_literal=getattr(graphene_type, "parse_literal", None),
175175
)
176176

177-
@staticmethod
178-
def create_enum(graphene_type):
177+
def create_enum(self, graphene_type):
179178
values = {}
180179
for name, value in graphene_type._meta.enum.__members__.items():
181180
description = getattr(value, "description", None)
@@ -203,7 +202,7 @@ def create_enum(graphene_type):
203202
return GrapheneEnumType(
204203
graphene_type=graphene_type,
205204
values=values,
206-
name=graphene_type._meta.name,
205+
name=self.add_prefix_to_type_name(graphene_type._meta.name),
207206
description=type_description,
208207
)
209208

@@ -225,11 +224,10 @@ def interfaces():
225224
else:
226225
is_type_of = graphene_type.is_type_of
227226

228-
name = (
229-
self.object_type_name_prefix.capitalize()
230-
if self.object_type_name_prefix
231-
else ""
232-
) + graphene_type._meta.name
227+
if graphene_type._meta.name in self.root_type_names:
228+
name = graphene_type._meta.name
229+
else:
230+
name = self.add_prefix_to_type_name(graphene_type._meta.name)
233231

234232
return GrapheneObjectType(
235233
graphene_type=graphene_type,
@@ -259,7 +257,7 @@ def interfaces():
259257

260258
return GrapheneInterfaceType(
261259
graphene_type=graphene_type,
262-
name=graphene_type._meta.name,
260+
name=self.add_prefix_to_type_name(graphene_type._meta.name),
263261
description=graphene_type._meta.description,
264262
fields=partial(self.create_fields_for_type, graphene_type),
265263
interfaces=interfaces,
@@ -269,7 +267,7 @@ def interfaces():
269267
def create_inputobjecttype(self, graphene_type):
270268
return GrapheneInputObjectType(
271269
graphene_type=graphene_type,
272-
name=graphene_type._meta.name,
270+
name=self.add_prefix_to_type_name(graphene_type._meta.name),
273271
description=graphene_type._meta.description,
274272
out_type=graphene_type._meta.container,
275273
fields=partial(
@@ -298,7 +296,7 @@ def types():
298296

299297
return GrapheneUnionType(
300298
graphene_type=graphene_type,
301-
name=graphene_type._meta.name,
299+
name=self.add_prefix_to_type_name(graphene_type._meta.name),
302300
description=graphene_type._meta.description,
303301
types=types,
304302
resolve_type=resolve_type,
@@ -376,13 +374,8 @@ def create_fields_for_type(self, graphene_type, is_input_type=False):
376374
if field.name:
377375
field_name = field.name
378376
else:
379-
if (
380-
self.object_type_name_prefix
381-
and graphene_type._meta.name in self.root_names
382-
):
383-
field_name = self.get_name(
384-
self.object_type_name_prefix + "_" + name
385-
)
377+
if graphene_type._meta.name in self.root_type_names:
378+
field_name = self.add_prefix_to_field_name(name)
386379
else:
387380
field_name = self.get_name(name)
388381
fields[field_name] = _field
@@ -418,6 +411,23 @@ def resolve_type(self, resolve_type_func, type_name, root, info, _type):
418411
return_type = self[type_name]
419412
return default_type_resolver(root, info, return_type)
420413

414+
def add_prefix_to_type_name(self, name):
415+
if self.type_name_prefix:
416+
return self.type_name_prefix[0].upper() + self.type_name_prefix[1:] + name
417+
return name
418+
419+
def add_prefix_to_field_name(self, name):
420+
if self.type_name_prefix:
421+
if self.auto_camelcase:
422+
return self.get_name(
423+
self.type_name_prefix[0].lower()
424+
+ self.type_name_prefix[1:]
425+
+ "_"
426+
+ name
427+
)
428+
return self.get_name(self.type_name_prefix + name)
429+
return self.get_name(name)
430+
421431

422432
class Schema:
423433
"""Schema Definition.
@@ -448,7 +458,7 @@ def __init__(
448458
types=None,
449459
directives=None,
450460
auto_camelcase=True,
451-
object_type_name_prefix=None,
461+
type_name_prefix=None,
452462
):
453463
self.query = query
454464
self.mutation = mutation
@@ -459,7 +469,7 @@ def __init__(
459469
subscription,
460470
types,
461471
auto_camelcase=auto_camelcase,
462-
object_type_name_prefix=object_type_name_prefix,
472+
type_name_prefix=type_name_prefix,
463473
)
464474
self.graphql_schema = GraphQLSchema(
465475
type_map.query,

graphene/types/tests/test_mutation.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class Query(ObjectType):
220220
assert result.data == {"createUserWithPlanet": {"name": "Peter", "planet": "earth"}}
221221

222222

223-
def test_object_type_name_prefix():
223+
def test_type_name_prefix():
224224
class BaseCreateUser(Mutation):
225225
class Arguments:
226226
name = String()
@@ -245,10 +245,14 @@ class MyMutation(ObjectType):
245245
class Query(ObjectType):
246246
a = String()
247247

248-
schema = Schema(query=Query, mutation=MyMutation, object_type_name_prefix="prefix")
248+
schema = Schema(
249+
query=Query,
250+
mutation=MyMutation,
251+
type_name_prefix="MyPrefix",
252+
)
249253
result = schema.execute(
250254
""" mutation mymutation {
251-
prefixCreateUserWithPlanet(name:"Peter", planet: "earth") {
255+
myPrefixCreateUserWithPlanet(name:"Peter", planet: "earth") {
252256
name
253257
planet
254258
}
@@ -257,5 +261,5 @@ class Query(ObjectType):
257261
)
258262
assert not result.errors
259263
assert result.data == {
260-
"prefixCreateUserWithPlanet": {"name": "Peter", "planet": "earth"}
264+
"myPrefixCreateUserWithPlanet": {"name": "Peter", "planet": "earth"}
261265
}

graphene/types/tests/test_query.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ def resolve_user(self, *args, **kwargs):
499499
assert result.data == expected
500500

501501

502-
def test_object_type_name_prefix():
502+
def test_type_name_prefix():
503503
class Cat(ObjectType):
504504
name = String()
505505

@@ -516,9 +516,9 @@ class Query(ObjectType):
516516
def resolve_user(self, *args, **kwargs):
517517
return User(name="foo")
518518

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 } } }")
519+
schema = Schema(query=Query, type_name_prefix="MyPrefix")
520+
expected = {"myPrefixUser": {"name": "foo", "cat": {"name": "bar"}}}
521+
result = schema.execute("{ myPrefixUser { name cat { name } } }")
522522

523523
assert not result.errors
524524
assert result.data == expected

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy