-
Notifications
You must be signed in to change notification settings - Fork 822
feat: added object_type_name_prefix #1466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2dee4f2
0633b62
8cb9791
8423706
97dd7e5
b0e179f
673a282
10c53d3
0ea4636
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"] | ||
|
@@ -91,6 +91,7 @@ def __init__( | |
subscription=None, | ||
types=None, | ||
auto_camelcase=True, | ||
type_name_prefix=None, | ||
): | ||
assert_valid_root_type(query) | ||
assert_valid_root_type(mutation) | ||
|
@@ -101,9 +102,18 @@ def __init__( | |
assert is_graphene_type(type_) | ||
|
||
self.auto_camelcase = auto_camelcase | ||
self.type_name_prefix = type_name_prefix | ||
|
||
create_graphql_type = self.add_type | ||
|
||
self.root_type_names = [] | ||
if query: | ||
self.root_type_names.append(query._meta.name) | ||
if mutation: | ||
self.root_type_names.append(mutation._meta.name) | ||
if subscription: | ||
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 | ||
self.subscription = create_graphql_type(subscription) if subscription else None | ||
|
@@ -131,9 +141,9 @@ 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: | ||
|
@@ -142,7 +152,10 @@ def add_type(self, graphene_type): | |
return graphql_type | ||
|
||
@staticmethod | ||
def create_scalar(graphene_type): | ||
def create_scalar( | ||
graphene_type, | ||
type_name_prefix=None, | ||
): | ||
# We have a mapping to the original GraphQL types | ||
# so there are no collisions. | ||
_scalars = { | ||
|
@@ -157,15 +170,15 @@ def create_scalar(graphene_type): | |
|
||
return GrapheneScalarType( | ||
graphene_type=graphene_type, | ||
name=graphene_type._meta.name, | ||
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), | ||
tcleonard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
@staticmethod | ||
erikwrede marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def create_enum(graphene_type): | ||
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) | ||
|
@@ -193,7 +206,7 @@ def create_enum(graphene_type): | |
return GrapheneEnumType( | ||
graphene_type=graphene_type, | ||
values=values, | ||
name=graphene_type._meta.name, | ||
name=get_type_name(graphene_type, type_name_prefix), | ||
description=type_description, | ||
) | ||
|
||
|
@@ -215,9 +228,14 @@ def interfaces(): | |
else: | ||
is_type_of = graphene_type.is_type_of | ||
|
||
if graphene_type._meta.name in self.root_type_names: | ||
name = graphene_type._meta.name | ||
else: | ||
name = self.get_type_name(graphene_type) | ||
|
||
return GrapheneObjectType( | ||
graphene_type=graphene_type, | ||
name=graphene_type._meta.name, | ||
name=name, | ||
description=graphene_type._meta.description, | ||
fields=partial(self.create_fields_for_type, graphene_type), | ||
is_type_of=is_type_of, | ||
|
@@ -243,7 +261,7 @@ def interfaces(): | |
|
||
return GrapheneInterfaceType( | ||
graphene_type=graphene_type, | ||
name=graphene_type._meta.name, | ||
name=self.get_type_name(graphene_type), | ||
description=graphene_type._meta.description, | ||
fields=partial(self.create_fields_for_type, graphene_type), | ||
interfaces=interfaces, | ||
|
@@ -253,7 +271,7 @@ def interfaces(): | |
def create_inputobjecttype(self, graphene_type): | ||
return GrapheneInputObjectType( | ||
graphene_type=graphene_type, | ||
name=graphene_type._meta.name, | ||
name=self.get_type_name(graphene_type), | ||
description=graphene_type._meta.description, | ||
out_type=graphene_type._meta.container, | ||
fields=partial( | ||
|
@@ -282,7 +300,7 @@ def types(): | |
|
||
return GrapheneUnionType( | ||
graphene_type=graphene_type, | ||
name=graphene_type._meta.name, | ||
name=self.get_type_name(graphene_type), | ||
description=graphene_type._meta.description, | ||
types=types, | ||
resolve_type=resolve_type, | ||
|
@@ -357,7 +375,10 @@ def create_fields_for_type(self, graphene_type, is_input_type=False): | |
deprecation_reason=field.deprecation_reason, | ||
description=field.description, | ||
) | ||
field_name = field.name or self.get_name(name) | ||
if field.name: | ||
field_name = field.name | ||
else: | ||
field_name = self.get_field_name(graphene_type, name) | ||
fields[field_name] = _field | ||
return fields | ||
|
||
|
@@ -391,6 +412,26 @@ 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 get_type_name(self, graphene_type): | ||
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: | ||
# We only add the prefix to the root types and types defined prefixes take precedence | ||
# over schema defined prefix. | ||
type_name_prefix = ( | ||
Comment on lines
+415
to
+422
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both methods should either be defined here or in utils. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @erikwrede sorry I haven't gotten back to you yet, I'm busy at work at the moment but I haven't forgotten about this PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @superlevure great to hear from u! no worries, feel free to tag me once you get to it 🙂 |
||
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: | ||
if self.auto_camelcase: | ||
return to_camel_case( | ||
type_name_prefix[0].lower() + type_name_prefix[1:] + "_" + name | ||
) | ||
return type_name_prefix + name | ||
return self.get_name(name) | ||
|
||
|
||
class Schema: | ||
"""Schema Definition. | ||
|
@@ -421,12 +462,18 @@ def __init__( | |
types=None, | ||
directives=None, | ||
auto_camelcase=True, | ||
type_name_prefix=None, | ||
): | ||
self.query = query | ||
self.mutation = mutation | ||
self.subscription = subscription | ||
type_map = TypeMap( | ||
query, mutation, subscription, types, auto_camelcase=auto_camelcase | ||
query, | ||
mutation, | ||
subscription, | ||
types, | ||
auto_camelcase=auto_camelcase, | ||
type_name_prefix=type_name_prefix, | ||
) | ||
self.graphql_schema = GraphQLSchema( | ||
type_map.query, | ||
|
Uh oh!
There was an error while loading. Please reload this page.