-
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 3 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 | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -164,8 +174,7 @@ def create_scalar(graphene_type): | |||||||||||||||||||||||||||||||||||||||||
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(self, graphene_type): | ||||||||||||||||||||||||||||||||||||||||||
values = {} | ||||||||||||||||||||||||||||||||||||||||||
for name, value in graphene_type._meta.enum.__members__.items(): | ||||||||||||||||||||||||||||||||||||||||||
description = getattr(value, "description", None) | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -193,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, | ||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
|
@@ -215,9 +224,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.add_prefix_to_type_name(graphene_type._meta.name) | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
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 +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, | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -253,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( | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -282,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, | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -357,7 +371,13 @@ 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: | ||||||||||||||||||||||||||||||||||||||||||
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) | ||||||||||||||||||||||||||||||||||||||||||
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. I'm not completely sure but I think that even if the name comes from
Suggested change
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. Looking again at the code I see that we bypass I'm not sure neither to be honest 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. yeah I saw that we bypass the auto camel case but I am not sure it makes sense to remove the prefixing... 🤷♂️ 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. Should this really be part of The drawback is having to configure the same prefix in different places. But we are more exact as the If we go for |
||||||||||||||||||||||||||||||||||||||||||
fields[field_name] = _field | ||||||||||||||||||||||||||||||||||||||||||
return fields | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
|
@@ -391,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 | ||||||||||||||||||||||||||||||||||||||||||
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. This is to go from Ideally we'd need a It feels a bit overkill though, any thoughts on this? 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.
Suggested change
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. 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. mmmm that's a fair point! (that also means that if we have auto camel case on and we have 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. @tcleonard great catch! I'm not sure about changing that since For prefixes, we should expect the use of proper |
||||||||||||||||||||||||||||||||||||||||||
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) | ||||||||||||||||||||||||||||||||||||||||||
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. I don't think there is a need to deal with the
Suggested change
also note that in your current implementation where you don't
indeed that would be 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.
By default 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. yes but my point was if
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. I'm confused, what would be more intuitive in your view? Because
Feels the most natural to me 🤔 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. Gentle ping on that one @tcleonard |
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
class Schema: | ||||||||||||||||||||||||||||||||||||||||||
"""Schema Definition. | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -421,12 +458,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.