Skip to content
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

feat: added object_type_name_prefix #1466

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
39 changes: 36 additions & 3 deletions graphene/types/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(
subscription=None,
types=None,
auto_camelcase=True,
object_type_name_prefix=None,
):
assert_valid_root_type(query)
assert_valid_root_type(mutation)
Expand All @@ -101,9 +102,18 @@ def __init__(
assert is_graphene_type(type_)

self.auto_camelcase = auto_camelcase
self.object_type_name_prefix = object_type_name_prefix

create_graphql_type = self.add_type

self.root_names = []
if query:
self.root_names.append(query._meta.name)
if mutation:
self.root_names.append(mutation._meta.name)
if subscription:
self.root_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
Expand Down Expand Up @@ -215,9 +225,15 @@ 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

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,
Expand Down Expand Up @@ -357,7 +373,18 @@ 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 (
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
)
else:
field_name = self.get_name(name)
fields[field_name] = _field
return fields

Expand Down Expand Up @@ -421,12 +448,18 @@ def __init__(
types=None,
directives=None,
auto_camelcase=True,
object_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,
object_type_name_prefix=object_type_name_prefix,
)
self.graphql_schema = GraphQLSchema(
type_map.query,
Expand Down
41 changes: 41 additions & 0 deletions graphene/types/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,44 @@ class Query(ObjectType):
)
assert not result.errors
assert result.data == {"createUserWithPlanet": {"name": "Peter", "planet": "earth"}}


def test_object_type_name_prefix():
class BaseCreateUser(Mutation):
class Arguments:
name = String()

name = String()

def mutate(self, info, **args):
return args

class CreateUserWithPlanet(BaseCreateUser):
class Arguments(BaseCreateUser.Arguments):
planet = String()

planet = String()

def mutate(self, info, **args):
return CreateUserWithPlanet(**args)

class MyMutation(ObjectType):
create_user_with_planet = CreateUserWithPlanet.Field()

class Query(ObjectType):
a = String()

schema = Schema(query=Query, mutation=MyMutation, object_type_name_prefix="prefix")
result = schema.execute(
""" mutation mymutation {
prefixCreateUserWithPlanet(name:"Peter", planet: "earth") {
name
planet
}
}
"""
)
assert not result.errors
assert result.data == {
"prefixCreateUserWithPlanet": {"name": "Peter", "planet": "earth"}
}
25 changes: 25 additions & 0 deletions graphene/types/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,28 @@ def resolve_user(self, *args, **kwargs):

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


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

class User(ObjectType):
name = String()
cat = Field(Cat)

def resolve_cat(self, *args, **kwargs):
return Cat(name="bar")

class Query(ObjectType):
user = Field(User)

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 } } }")

assert not result.errors
assert result.data == expected
125 changes: 124 additions & 1 deletion graphene/types/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,43 @@
from graphql.type import GraphQLObjectType, GraphQLSchema

from ..field import Field
from ..mutation import Mutation
from ..objecttype import ObjectType
from ..scalars import String
from ..scalars import Int, String
from ..schema import Schema


class MyType(ObjectType):
field = String()


class MyOtherType(ObjectType):
field = String()
my_type = Field(MyType)


class Query(ObjectType):
inner = Field(MyOtherType)


class CreateUser(Mutation):
class Arguments:
name = String()

name = String()

def mutate(self, info, name):
return CreateUser(name=name)


class Mutation(ObjectType):
create_user = CreateUser.Field()


class Subscription(ObjectType):
count_to_ten = Field(Int)


def test_schema():
schema = Schema(Query)
graphql_schema = schema.graphql_schema
Expand Down Expand Up @@ -54,6 +78,11 @@ def test_schema_str():

type MyOtherType {
field: String
myType: MyType
}

type MyType {
field: String
}
"""
).strip()
Expand All @@ -72,3 +101,97 @@ def test_schema_requires_query_type():
assert len(result.errors) == 1
error = result.errors[0]
assert error.message == "Query root type must be provided."


def test_schema_object_type_name_prefix_camelcase():
schema = Schema(
Query,
Mutation,
Subscription,
auto_camelcase=True,
object_type_name_prefix="prefix",
)
assert (
str(schema).strip()
== dedent(
"""
schema {
query: PrefixQuery
mutation: PrefixMutation
subscription: PrefixSubscription
}

type PrefixQuery {
prefixInner: PrefixMyOtherType
}

type PrefixMyOtherType {
field: String
myType: PrefixMyType
}

type PrefixMyType {
field: String
}

type PrefixMutation {
prefixCreateUser(name: String): PrefixCreateUser
}

type PrefixCreateUser {
name: String
}

type PrefixSubscription {
prefixCountToTen: Int
}
"""
).strip()
)


def test_schema_object_type_name_prefix_camelcase_disabled():
schema = Schema(
Query,
Mutation,
Subscription,
auto_camelcase=False,
object_type_name_prefix="prefix",
)
assert (
str(schema).strip()
== dedent(
"""
schema {
query: PrefixQuery
mutation: PrefixMutation
subscription: PrefixSubscription
}

type PrefixQuery {
prefix_inner: PrefixMyOtherType
}

type PrefixMyOtherType {
field: String
my_type: PrefixMyType
}

type PrefixMyType {
field: String
}

type PrefixMutation {
prefix_create_user(name: String): PrefixCreateUser
}

type PrefixCreateUser {
name: String
}

type PrefixSubscription {
prefix_count_to_ten: Int
}
"""
).strip()
)