本文整理匯總了Python中graphene_django.DjangoObjectType方法的典型用法代碼示例。如果您正苦於以下問題:Python graphene_django.DjangoObjectType方法的具體用法?Python graphene_django.DjangoObjectType怎麽用?Python graphene_django.DjangoObjectType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類graphene_django
的用法示例。
在下文中一共展示了graphene_django.DjangoObjectType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_filter_filterset_information_on_meta_related
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def test_filter_filterset_information_on_meta_related():
class ReporterFilterNode(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
filter_fields = ["first_name", "articles"]
class ArticleFilterNode(DjangoObjectType):
class Meta:
model = Article
interfaces = (Node,)
filter_fields = ["headline", "reporter"]
class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterFilterNode)
all_articles = DjangoFilterConnectionField(ArticleFilterNode)
reporter = Field(ReporterFilterNode)
article = Field(ArticleFilterNode)
schema = Schema(query=Query)
articles_field = ReporterFilterNode._meta.fields["articles"].get_type()
assert_arguments(articles_field, "headline", "reporter")
assert_not_orderable(articles_field)
示例2: test_recursive_filter_connection
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def test_recursive_filter_connection():
class ReporterFilterNode(DjangoObjectType):
child_reporters = DjangoFilterConnectionField(lambda: ReporterFilterNode)
def resolve_child_reporters(self, **args):
return []
class Meta:
model = Reporter
interfaces = (Node,)
class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterFilterNode)
assert (
ReporterFilterNode._meta.fields["child_reporters"].node_type
== ReporterFilterNode
)
示例3: test_should_query_list
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def test_should_query_list():
r1 = Reporter(last_name="ABA")
r1.save()
r2 = Reporter(last_name="Griffin")
r2.save()
class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
class Query(graphene.ObjectType):
all_reporters = graphene.List(ReporterType)
debug = graphene.Field(DjangoDebug, name="__debug")
def resolve_all_reporters(self, info, **args):
return Reporter.objects.all()
query = """
query ReporterQuery {
allReporters {
lastName
}
__debug {
sql {
rawSql
}
}
}
"""
expected = {
"allReporters": [{"lastName": "ABA"}, {"lastName": "Griffin"}],
"__debug": {"sql": [{"rawSql": str(Reporter.objects.all().query)}]},
}
schema = graphene.Schema(query=Query)
result = schema.execute(
query, context_value=context(), middleware=[DjangoDebugMiddleware()]
)
assert not result.errors
assert result.data == expected
示例4: pet_type
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def pet_type():
class PetType(DjangoObjectType):
class Meta:
model = Pet
fields = "__all__"
return PetType
示例5: test_filter_filterset_class_filter_fields_exception
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def test_filter_filterset_class_filter_fields_exception():
with pytest.raises(Exception):
class ReporterFilter(FilterSet):
class Meta:
model = Reporter
fields = ["first_name", "articles"]
class ReporterFilterNode(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
filterset_class = ReporterFilter
filter_fields = ["first_name", "articles"]
示例6: test_filter_filterset_class_information_on_meta
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def test_filter_filterset_class_information_on_meta():
class ReporterFilter(FilterSet):
class Meta:
model = Reporter
fields = ["first_name", "articles"]
class ReporterFilterNode(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
filterset_class = ReporterFilter
field = DjangoFilterConnectionField(ReporterFilterNode)
assert_arguments(field, "first_name", "articles")
assert_not_orderable(field)
示例7: test_filter_filterset_class_information_on_meta_related
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def test_filter_filterset_class_information_on_meta_related():
class ReporterFilter(FilterSet):
class Meta:
model = Reporter
fields = ["first_name", "articles"]
class ArticleFilter(FilterSet):
class Meta:
model = Article
fields = ["headline", "reporter"]
class ReporterFilterNode(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
filterset_class = ReporterFilter
class ArticleFilterNode(DjangoObjectType):
class Meta:
model = Article
interfaces = (Node,)
filterset_class = ArticleFilter
class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterFilterNode)
all_articles = DjangoFilterConnectionField(ArticleFilterNode)
reporter = Field(ReporterFilterNode)
article = Field(ArticleFilterNode)
schema = Schema(query=Query)
articles_field = ReporterFilterNode._meta.fields["articles"].get_type()
assert_arguments(articles_field, "headline", "reporter")
assert_not_orderable(articles_field)
示例8: test_filter_filterset_related_results_with_filter
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def test_filter_filterset_related_results_with_filter():
class ReporterFilterNode(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
filter_fields = {"first_name": ["icontains"]}
class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterFilterNode)
Reporter.objects.create(
first_name="A test user", last_name="Last Name", email="test1@test.com"
)
Reporter.objects.create(
first_name="Other test user",
last_name="Other Last Name",
email="test2@test.com",
)
Reporter.objects.create(
first_name="Random", last_name="RandomLast", email="random@test.com"
)
query = """
query {
allReporters(firstName_Icontains: "test") {
edges {
node {
id
}
}
}
}
"""
schema = Schema(query=Query)
result = schema.execute(query)
assert not result.errors
# We should only get two reporters
assert len(result.data["allReporters"]["edges"]) == 2
示例9: test_annotation_with_only
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def test_annotation_with_only():
class ReporterType(DjangoObjectType):
full_name = String()
class Meta:
model = Reporter
interfaces = (Node,)
filter_fields = ()
class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterType)
def resolve_all_reporters(self, info, **args):
return Reporter.objects.only("first_name", "last_name").annotate(
full_name=Concat(
"first_name", Value(" "), "last_name", output_field=TextField()
)
)
Reporter.objects.create(first_name="John", last_name="Doe")
schema = Schema(query=Query)
query = """
query NodeFilteringQuery {
allReporters(first: 1) {
edges {
node {
fullName
}
}
}
}
"""
expected = {"allReporters": {"edges": [{"node": {"fullName": "John Doe"}}]}}
result = schema.execute(query)
assert not result.errors
assert result.data == expected
示例10: _add_page
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def _add_page(cls: Type[wagtailPage], node: str, dict_params: dict) -> Type[DjangoObjectType]:
if cls in registry.pages: # pragma: no cover
return registry.pages[cls]
registry.page_prefetch_fields.add(cls.__name__.lower())
dict_params['Meta'].interfaces += (Page,)
tp = type(node, (DjangoObjectType,), dict_params) # type: Type[DjangoObjectType]
registry.pages[cls] = tp
return tp
示例11: _add_setting
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def _add_setting(cls: Type[BaseSetting], node: str, dict_params: dict) -> Type[DjangoObjectType]:
if not hasattr(cls, 'name'): # we always need a name field
cls.name = cls.__name__
dict_params['Meta'].interfaces += (Settings,)
tp = type(node, (DjangoObjectType,), dict_params) # type: Type[DjangoObjectType]
registry.settings[node] = (tp, cls)
return tp
示例12: _add_snippet
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def _add_snippet(cls: type, node: str, dict_params: dict) -> Type[DjangoObjectType]:
if cls in registry.snippets: # pragma: no cover
return registry.snippets[cls]
tp = type(node, (DjangoObjectType,), dict_params) # type: Type[DjangoObjectType]
registry.snippets[cls] = tp
registry.snippets_by_name[node] = tp
return tp
示例13: _add_django_model
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def _add_django_model(_cls: type, node: str, dict_params: dict) -> Type[DjangoObjectType]:
if node in registry.django: # pragma: no cover
return registry.django[node]
tp = type(node, (DjangoObjectType,), dict_params) # type: Type[DjangoObjectType]
registry.django[node] = tp
return tp
示例14: test_should_query_field
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def test_should_query_field():
r1 = Reporter(last_name="ABA")
r1.save()
r2 = Reporter(last_name="Griffin")
r2.save()
class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
class Query(graphene.ObjectType):
reporter = graphene.Field(ReporterType)
debug = graphene.Field(DjangoDebug, name="__debug")
def resolve_reporter(self, info, **args):
return Reporter.objects.first()
query = """
query ReporterQuery {
reporter {
lastName
}
__debug {
sql {
rawSql
}
}
}
"""
expected = {
"reporter": {"lastName": "ABA"},
"__debug": {
"sql": [{"rawSql": str(Reporter.objects.order_by("pk")[:1].query)}]
},
}
schema = graphene.Schema(query=Query)
result = schema.execute(
query, context_value=context(), middleware=[DjangoDebugMiddleware()]
)
assert not result.errors
assert result.data == expected
示例15: test_should_query_connection
# 需要導入模塊: import graphene_django [as 別名]
# 或者: from graphene_django import DjangoObjectType [as 別名]
def test_should_query_connection(graphene_settings, max_limit):
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = max_limit
r1 = Reporter(last_name="ABA")
r1.save()
r2 = Reporter(last_name="Griffin")
r2.save()
class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
class Query(graphene.ObjectType):
all_reporters = DjangoConnectionField(ReporterType)
debug = graphene.Field(DjangoDebug, name="__debug")
def resolve_all_reporters(self, info, **args):
return Reporter.objects.all()
query = """
query ReporterQuery {
allReporters(first:1) {
edges {
node {
lastName
}
}
}
__debug {
sql {
rawSql
}
}
}
"""
expected = {"allReporters": {"edges": [{"node": {"lastName": "ABA"}}]}}
schema = graphene.Schema(query=Query)
result = schema.execute(
query, context_value=context(), middleware=[DjangoDebugMiddleware()]
)
assert not result.errors
assert result.data["allReporters"] == expected["allReporters"]
assert len(result.data["__debug"]["sql"]) == 2
assert "COUNT" in result.data["__debug"]["sql"][0]["rawSql"]
query = str(Reporter.objects.all()[:1].query)
assert result.data["__debug"]["sql"][1]["rawSql"] == query