Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: static method
  • Loading branch information
superlevure committed Oct 27, 2022
commit 0ea463671e989b5cead493004db30600e70f27a5
32 changes: 13 additions & 19 deletions graphene/types/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from .scalars import ID, Boolean, Float, Int, Scalar, String
from .structures import List, NonNull
from .union import Union
from .utils import get_field_as
from .utils import get_field_as, get_type_name

introspection_query = get_introspection_query()
IntrospectionSchema = introspection_types["__Schema"]
Expand Down Expand Up @@ -141,17 +141,21 @@ def add_type(self, graphene_type):
elif issubclass(graphene_type, Interface):
graphql_type = self.create_interface(graphene_type)
elif issubclass(graphene_type, Scalar):
graphql_type = self.create_scalar(graphene_type)
graphql_type = self.create_scalar(graphene_type, self.type_name_prefix)
elif issubclass(graphene_type, Enum):
graphql_type = self.create_enum(graphene_type)
graphql_type = self.create_enum(graphene_type, self.type_name_prefix)
elif issubclass(graphene_type, Union):
graphql_type = self.construct_union(graphene_type)
else:
raise TypeError(f"Expected Graphene type, but received: {graphene_type}.")
self[name] = graphql_type
return graphql_type

def create_scalar(self, graphene_type):
@staticmethod
def create_scalar(
graphene_type,
type_name_prefix=None,
):
# We have a mapping to the original GraphQL types
# so there are no collisions.
_scalars = {
Expand All @@ -166,14 +170,15 @@ def create_scalar(self, graphene_type):

return GrapheneScalarType(
graphene_type=graphene_type,
name=self.get_type_name(graphene_type),
name=get_type_name(graphene_type, type_name_prefix),
description=graphene_type._meta.description,
serialize=getattr(graphene_type, "serialize", None),
parse_value=getattr(graphene_type, "parse_value", None),
parse_literal=getattr(graphene_type, "parse_literal", None),
)

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

Expand Down Expand Up @@ -408,18 +413,7 @@ def resolve_type(self, resolve_type_func, type_name, root, info, _type):
return default_type_resolver(root, info, return_type)

def get_type_name(self, graphene_type):
type_name_prefix = (
graphene_type._meta.type_name_prefix
if graphene_type._meta.type_name_prefix is not None
else self.type_name_prefix
)
if type_name_prefix:
return (
type_name_prefix[0].upper()
+ type_name_prefix[1:]
+ graphene_type._meta.name
)
return graphene_type._meta.name
return get_type_name(graphene_type, self.type_name_prefix)

def get_field_name(self, graphene_type, name):
if graphene_type._meta.name in self.root_type_names:
Expand Down
15 changes: 15 additions & 0 deletions graphene/types/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,18 @@ def get_underlying_type(_type):
while hasattr(_type, "of_type"):
_type = _type.of_type
return _type


def get_type_name(graphene_type, type_name_prefix):
type_name_prefix = (
graphene_type._meta.type_name_prefix
if graphene_type._meta.type_name_prefix is not None
else type_name_prefix
)
if type_name_prefix:
return (
type_name_prefix[0].upper()
+ type_name_prefix[1:]
+ graphene_type._meta.name
)
return graphene_type._meta.name
Comment on lines +53 to +65
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's great that we can override the prefixes for individual types using type_name_prefix. When federating a schema, using @keyon a type we could automatically disable the prefix like that (requiring an extra PR in graphene-federation). For now, this would have to be implemented manually which is also fine.

Personally I dislike disabling the prefix using type_name_prefix = "". It doesn't feel very pythonic to use an empty string to mark something as disabled.
Why do we even need a custom per-object-type prefix? It's kind of verbose to have a Meta option name that can be extended using a prefix just for this single ObjectType. Are there any reasons to not include that custom prefix in the name in the first place?
If there are specific use cases where a separate prefix is needed, it would be helpful to understand these in order to better evaluate the value of this feature. Please let me know if you have any use cases ☺️

One potential alternative could be to use a boolean flag, such as enable_type_name_prefix, with a default value of True, since by default all ObjectTypes use the prefix. This would allow the prefix to be easily enabled or disabled as needed. We could even set the default for this to False in the init_subclass_with_meta for type names Query, Mutation, Subscription so these could still be prefixed if the user wishes to do so regardless of the points made in an older review.