Skip to content

Commit

Permalink
fix: reviews comments
Browse files Browse the repository at this point in the history
  • Loading branch information
superlevure committed Oct 24, 2022
1 parent 0633b62 commit 8cb9791
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 76 deletions.
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Required library
Sphinx==1.5.3
jinja2<3.1.0
sphinx-autobuild==0.7.1
# Docs template
http://graphene-python.org/sphinx_graphene_theme.zip
133 changes: 133 additions & 0 deletions docs/types/schema.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,136 @@ To disable this behavior, set the ``auto_camelcase`` to ``False`` upon schema in
query=MyRootQuery,
auto_camelcase=False,
)
.. _SchemaTypeNamePrefix:

Type name prefix
--------------------------

You can specify a prefix for all type names in the schema by setting the ``type_name_prefix`` argument upon schema instantiation:

.. code:: python
my_schema = Schema(
query=MyRootQuery,
mutation=MyRootMutation,
subscription=MyRootSubscription
type_name_prefix='MyPrefix',
)
This is useful in a micro-services architecture to prepend the service name to all types and avoid conflicts for example.

The prefix will be added to the name of:

* Query / Mutation / Subscription
* ObjectType
* InputType
* Interface
* Union
* Enum

While fields and arguments name will be left untouched.

More specifically, the following schema:

.. code::
type Query {
inner: MyType
}
type MyType {
field: String
myUnion: MyUnion
myBarType: MyBarType
myFooType: MyFooType
}
union MyUnion = MyBarType | MyFooType
type MyBarType {
field(input: MyInputObjectType): String
myInterface: MyInterface
}
input MyInputObjectType {
field: String
}
interface MyInterface {
field: String
}
type MyFooType {
field: String
myEnum: MyEnum
}
enum MyEnum {
FOO
BAR
}
type Mutation {
createUser(name: String): CreateUser
}
type CreateUser {
name: String
}
type Subscription {
countToTen: Int
}
Will be transformed to:

.. code::
type Query {
myPrefixInner: MyPrefixMyType
}
type MyPrefixMyType {
field: String
myUnion: MyPrefixMyUnion
myBarType: MyPrefixMyBarType
myFooType: MyPrefixMyFooType
}
union MyPrefixMyUnion = MyPrefixMyBarType | MyPrefixMyFooType
type MyPrefixMyBarType {
field(input: MyPrefixMyInputObjectType): String
myInterface: MyPrefixMyInterface
}
input MyPrefixMyInputObjectType {
field: String
}
interface MyPrefixMyInterface {
field: String
}
type MyPrefixMyFooType {
field: String
myEnum: MyPrefixMyEnum
}
enum MyPrefixMyEnum {
FOO
BAR
}
type Mutation {
myPrefixCreateUser(name: String): MyPrefixCreateUser
}
type MyPrefixCreateUser {
name: String
}
type Subscription {
myPrefixCountToTen: Int
}
62 changes: 36 additions & 26 deletions graphene/types/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __init__(
subscription=None,
types=None,
auto_camelcase=True,
object_type_name_prefix=None,
type_name_prefix=None,
):
assert_valid_root_type(query)
assert_valid_root_type(mutation)
Expand All @@ -102,17 +102,17 @@ def __init__(
assert is_graphene_type(type_)

self.auto_camelcase = auto_camelcase
self.object_type_name_prefix = object_type_name_prefix
self.type_name_prefix = type_name_prefix

create_graphql_type = self.add_type

self.root_names = []
self.root_type_names = []
if query:
self.root_names.append(query._meta.name)
self.root_type_names.append(query._meta.name)
if mutation:
self.root_names.append(mutation._meta.name)
self.root_type_names.append(mutation._meta.name)
if subscription:
self.root_names.append(subscription._meta.name)
self.root_type_names.append(subscription._meta.name)

self.query = create_graphql_type(query) if query else None
self.mutation = create_graphql_type(mutation) if mutation else None
Expand Down Expand Up @@ -174,8 +174,7 @@ def create_scalar(graphene_type):
parse_literal=getattr(graphene_type, "parse_literal", None),
)

@staticmethod
def create_enum(graphene_type):
def create_enum(self, graphene_type):
values = {}
for name, value in graphene_type._meta.enum.__members__.items():
description = getattr(value, "description", None)
Expand Down Expand Up @@ -203,7 +202,7 @@ def create_enum(graphene_type):
return GrapheneEnumType(
graphene_type=graphene_type,
values=values,
name=graphene_type._meta.name,
name=self.add_prefix_to_type_name(graphene_type._meta.name),
description=type_description,
)

Expand All @@ -225,11 +224,10 @@ def interfaces():
else:
is_type_of = graphene_type.is_type_of

name = (
self.object_type_name_prefix.capitalize()
if self.object_type_name_prefix
else ""
) + graphene_type._meta.name
if graphene_type._meta.name in self.root_type_names:
name = graphene_type._meta.name
else:
name = self.add_prefix_to_type_name(graphene_type._meta.name)

return GrapheneObjectType(
graphene_type=graphene_type,
Expand Down Expand Up @@ -259,7 +257,7 @@ def interfaces():

return GrapheneInterfaceType(
graphene_type=graphene_type,
name=graphene_type._meta.name,
name=self.add_prefix_to_type_name(graphene_type._meta.name),
description=graphene_type._meta.description,
fields=partial(self.create_fields_for_type, graphene_type),
interfaces=interfaces,
Expand All @@ -269,7 +267,7 @@ def interfaces():
def create_inputobjecttype(self, graphene_type):
return GrapheneInputObjectType(
graphene_type=graphene_type,
name=graphene_type._meta.name,
name=self.add_prefix_to_type_name(graphene_type._meta.name),
description=graphene_type._meta.description,
out_type=graphene_type._meta.container,
fields=partial(
Expand Down Expand Up @@ -298,7 +296,7 @@ def types():

return GrapheneUnionType(
graphene_type=graphene_type,
name=graphene_type._meta.name,
name=self.add_prefix_to_type_name(graphene_type._meta.name),
description=graphene_type._meta.description,
types=types,
resolve_type=resolve_type,
Expand Down Expand Up @@ -376,13 +374,8 @@ def create_fields_for_type(self, graphene_type, is_input_type=False):
if field.name:
field_name = field.name
else:
if (
self.object_type_name_prefix
and graphene_type._meta.name in self.root_names
):
field_name = self.get_name(
self.object_type_name_prefix + "_" + name
)
if graphene_type._meta.name in self.root_type_names:
field_name = self.add_prefix_to_field_name(name)
else:
field_name = self.get_name(name)
fields[field_name] = _field
Expand Down Expand Up @@ -418,6 +411,23 @@ def resolve_type(self, resolve_type_func, type_name, root, info, _type):
return_type = self[type_name]
return default_type_resolver(root, info, return_type)

def add_prefix_to_type_name(self, name):
if self.type_name_prefix:
return self.type_name_prefix[0].upper() + self.type_name_prefix[1:] + name
return name

def add_prefix_to_field_name(self, name):
if self.type_name_prefix:
if self.auto_camelcase:
return self.get_name(
self.type_name_prefix[0].lower()
+ self.type_name_prefix[1:]
+ "_"
+ name
)
return self.get_name(self.type_name_prefix + name)
return self.get_name(name)


class Schema:
"""Schema Definition.
Expand Down Expand Up @@ -448,7 +458,7 @@ def __init__(
types=None,
directives=None,
auto_camelcase=True,
object_type_name_prefix=None,
type_name_prefix=None,
):
self.query = query
self.mutation = mutation
Expand All @@ -459,7 +469,7 @@ def __init__(
subscription,
types,
auto_camelcase=auto_camelcase,
object_type_name_prefix=object_type_name_prefix,
type_name_prefix=type_name_prefix,
)
self.graphql_schema = GraphQLSchema(
type_map.query,
Expand Down
12 changes: 8 additions & 4 deletions graphene/types/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class Query(ObjectType):
assert result.data == {"createUserWithPlanet": {"name": "Peter", "planet": "earth"}}


def test_object_type_name_prefix():
def test_type_name_prefix():
class BaseCreateUser(Mutation):
class Arguments:
name = String()
Expand All @@ -245,10 +245,14 @@ class MyMutation(ObjectType):
class Query(ObjectType):
a = String()

schema = Schema(query=Query, mutation=MyMutation, object_type_name_prefix="prefix")
schema = Schema(
query=Query,
mutation=MyMutation,
type_name_prefix="MyPrefix",
)
result = schema.execute(
""" mutation mymutation {
prefixCreateUserWithPlanet(name:"Peter", planet: "earth") {
myPrefixCreateUserWithPlanet(name:"Peter", planet: "earth") {
name
planet
}
Expand All @@ -257,5 +261,5 @@ class Query(ObjectType):
)
assert not result.errors
assert result.data == {
"prefixCreateUserWithPlanet": {"name": "Peter", "planet": "earth"}
"myPrefixCreateUserWithPlanet": {"name": "Peter", "planet": "earth"}
}
8 changes: 4 additions & 4 deletions graphene/types/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ def resolve_user(self, *args, **kwargs):
assert result.data == expected


def test_object_type_name_prefix():
def test_type_name_prefix():
class Cat(ObjectType):
name = String()

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

schema = Schema(query=Query, object_type_name_prefix="prefix")
expected = {"prefixUser": {"name": "foo", "cat": {"name": "bar"}}}
result = schema.execute("{ prefixUser { name cat { name } } }")
schema = Schema(query=Query, type_name_prefix="MyPrefix")
expected = {"myPrefixUser": {"name": "foo", "cat": {"name": "bar"}}}
result = schema.execute("{ myPrefixUser { name cat { name } } }")

assert not result.errors
assert result.data == expected
Loading

0 comments on commit 8cb9791

Please sign in to comment.