2016-09-18 02:29:00 +03:00
|
|
|
import datetime
|
2020-07-09 20:01:22 +03:00
|
|
|
import base64
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
from django.db import models
|
2020-04-20 15:23:20 +03:00
|
|
|
from django.db.models import Q
|
2016-10-16 03:40:12 +03:00
|
|
|
from django.utils.functional import SimpleLazyObject
|
2020-04-20 15:23:20 +03:00
|
|
|
from graphql_relay import to_global_id
|
2022-11-14 13:56:18 +03:00
|
|
|
from pytest import raises
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
import graphene
|
|
|
|
from graphene.relay import Node
|
|
|
|
|
2021-01-02 20:46:00 +03:00
|
|
|
from ..compat import IntegerRangeField, MissingType
|
2016-09-18 02:29:00 +03:00
|
|
|
from ..fields import DjangoConnectionField
|
2016-09-18 03:09:56 +03:00
|
|
|
from ..types import DjangoObjectType
|
2020-04-20 15:23:20 +03:00
|
|
|
from ..utils import DJANGO_FILTER_INSTALLED
|
2023-07-18 20:17:45 +03:00
|
|
|
from .models import (
|
|
|
|
Article,
|
|
|
|
CNNReporter,
|
|
|
|
Film,
|
|
|
|
FilmDetails,
|
|
|
|
Person,
|
|
|
|
Pet,
|
|
|
|
Reporter,
|
|
|
|
APNewsReporter,
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_query_only_fields():
|
|
|
|
with raises(Exception):
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
class ReporterType(DjangoObjectType):
|
2016-09-18 02:29:00 +03:00
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2019-07-09 16:03:11 +03:00
|
|
|
fields = ("articles",)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
schema = graphene.Schema(query=ReporterType)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2016-09-18 02:29:00 +03:00
|
|
|
query ReporterQuery {
|
|
|
|
articles
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2016-09-18 02:29:00 +03:00
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
|
|
|
|
|
2016-10-16 03:40:12 +03:00
|
|
|
def test_should_query_simplelazy_objects():
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2019-07-09 16:03:11 +03:00
|
|
|
fields = ("id",)
|
2016-10-16 03:40:12 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
reporter = graphene.Field(ReporterType)
|
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_reporter(self, info):
|
2016-10-16 03:40:12 +03:00
|
|
|
return SimpleLazyObject(lambda: Reporter(id=1))
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2016-10-16 03:40:12 +03:00
|
|
|
query {
|
|
|
|
reporter {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2016-10-16 03:40:12 +03:00
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
2018-07-20 02:51:33 +03:00
|
|
|
assert result.data == {"reporter": {"id": "1"}}
|
2016-10-16 03:40:12 +03:00
|
|
|
|
|
|
|
|
2020-05-09 19:35:09 +03:00
|
|
|
def test_should_query_wrapped_simplelazy_objects():
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
fields = ("id",)
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
reporter = graphene.Field(ReporterType)
|
|
|
|
|
|
|
|
def resolve_reporter(self, info):
|
|
|
|
return SimpleLazyObject(lambda: SimpleLazyObject(lambda: Reporter(id=1)))
|
|
|
|
|
2016-10-16 03:40:12 +03:00
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2016-10-16 03:40:12 +03:00
|
|
|
query {
|
|
|
|
reporter {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2016-10-16 03:40:12 +03:00
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
2018-07-20 02:51:33 +03:00
|
|
|
assert result.data == {"reporter": {"id": "1"}}
|
2016-10-16 03:40:12 +03:00
|
|
|
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
def test_should_query_well():
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
reporter = graphene.Field(ReporterType)
|
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_reporter(self, info):
|
2018-07-20 02:51:33 +03:00
|
|
|
return Reporter(first_name="ABA", last_name="X")
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2016-09-18 02:29:00 +03:00
|
|
|
query ReporterQuery {
|
|
|
|
reporter {
|
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
|
|
|
expected = {"reporter": {"firstName": "ABA", "lastName": "X", "email": ""}}
|
2016-09-18 02:29:00 +03:00
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
2021-01-02 20:46:00 +03:00
|
|
|
@pytest.mark.skipif(IntegerRangeField is MissingType, reason="RangeField should exist")
|
2016-09-18 02:29:00 +03:00
|
|
|
def test_should_query_postgres_fields():
|
2018-07-20 02:51:33 +03:00
|
|
|
from django.contrib.postgres.fields import (
|
|
|
|
IntegerRangeField,
|
|
|
|
ArrayField,
|
|
|
|
HStoreField,
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class Event(models.Model):
|
2018-07-20 02:51:33 +03:00
|
|
|
ages = IntegerRangeField(help_text="The age ranges")
|
2023-06-07 17:36:29 +03:00
|
|
|
data = models.JSONField(help_text="Data")
|
2016-09-18 02:29:00 +03:00
|
|
|
store = HStoreField()
|
|
|
|
tags = ArrayField(models.CharField(max_length=50))
|
|
|
|
|
|
|
|
class EventType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Event
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
event = graphene.Field(EventType)
|
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_event(self, info):
|
2016-09-18 02:29:00 +03:00
|
|
|
return Event(
|
|
|
|
ages=(0, 10),
|
2018-07-20 02:51:33 +03:00
|
|
|
data={"angry_babies": True},
|
|
|
|
store={"h": "store"},
|
|
|
|
tags=["child", "angry", "babies"],
|
2016-09-18 02:29:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2016-09-18 02:29:00 +03:00
|
|
|
query myQuery {
|
|
|
|
event {
|
|
|
|
ages
|
|
|
|
tags
|
|
|
|
data
|
|
|
|
store
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2016-09-18 02:29:00 +03:00
|
|
|
expected = {
|
2018-07-20 02:51:33 +03:00
|
|
|
"event": {
|
|
|
|
"ages": [0, 10],
|
|
|
|
"tags": ["child", "angry", "babies"],
|
|
|
|
"data": '{"angry_babies": true}',
|
|
|
|
"store": '{"h": "store"}',
|
|
|
|
}
|
2016-09-18 02:29:00 +03:00
|
|
|
}
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_node():
|
|
|
|
class ReporterNode(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
@classmethod
|
2017-07-28 19:43:27 +03:00
|
|
|
def get_node(cls, info, id):
|
2018-07-20 02:51:33 +03:00
|
|
|
return Reporter(id=2, first_name="Cookie Monster")
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_articles(self, info, **args):
|
2018-07-20 02:51:33 +03:00
|
|
|
return [Article(headline="Hi!")]
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class ArticleNode(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Article
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
@classmethod
|
2017-07-28 19:43:27 +03:00
|
|
|
def get_node(cls, info, id):
|
2018-07-20 02:51:33 +03:00
|
|
|
return Article(
|
|
|
|
id=1, headline="Article node", pub_date=datetime.date(2002, 3, 11)
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
node = Node.Field()
|
|
|
|
reporter = graphene.Field(ReporterNode)
|
|
|
|
article = graphene.Field(ArticleNode)
|
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_reporter(self, info):
|
2018-07-20 02:51:33 +03:00
|
|
|
return Reporter(id=1, first_name="ABA", last_name="X")
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2016-09-18 02:29:00 +03:00
|
|
|
query ReporterQuery {
|
|
|
|
reporter {
|
|
|
|
id,
|
|
|
|
firstName,
|
|
|
|
articles {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
headline
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastName,
|
|
|
|
email
|
|
|
|
}
|
|
|
|
myArticle: node(id:"QXJ0aWNsZU5vZGU6MQ==") {
|
|
|
|
id
|
|
|
|
... on ReporterNode {
|
|
|
|
firstName
|
|
|
|
}
|
|
|
|
... on ArticleNode {
|
|
|
|
headline
|
|
|
|
pubDate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2016-09-18 02:29:00 +03:00
|
|
|
expected = {
|
2018-07-20 02:51:33 +03:00
|
|
|
"reporter": {
|
|
|
|
"id": "UmVwb3J0ZXJOb2RlOjE=",
|
|
|
|
"firstName": "ABA",
|
|
|
|
"lastName": "X",
|
|
|
|
"email": "",
|
|
|
|
"articles": {"edges": [{"node": {"headline": "Hi!"}}]},
|
|
|
|
},
|
|
|
|
"myArticle": {
|
|
|
|
"id": "QXJ0aWNsZU5vZGU6MQ==",
|
|
|
|
"headline": "Article node",
|
|
|
|
"pubDate": "2002-03-11",
|
2016-09-18 02:29:00 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
2019-06-09 22:41:04 +03:00
|
|
|
def test_should_query_onetoone_fields():
|
2022-09-23 11:45:02 +03:00
|
|
|
film = Film.objects.create(id=1)
|
|
|
|
film_details = FilmDetails.objects.create(id=1, film=film)
|
2019-06-09 22:41:04 +03:00
|
|
|
|
|
|
|
class FilmNode(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Film
|
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2019-06-09 22:41:04 +03:00
|
|
|
|
|
|
|
class FilmDetailsNode(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = FilmDetails
|
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2019-06-09 22:41:04 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
film = graphene.Field(FilmNode)
|
|
|
|
film_details = graphene.Field(FilmDetailsNode)
|
|
|
|
|
|
|
|
def resolve_film(root, info):
|
|
|
|
return film
|
|
|
|
|
|
|
|
def resolve_film_details(root, info):
|
|
|
|
return film_details
|
|
|
|
|
|
|
|
query = """
|
|
|
|
query FilmQuery {
|
|
|
|
filmDetails {
|
|
|
|
id
|
|
|
|
film {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
film {
|
|
|
|
id
|
|
|
|
details {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
expected = {
|
|
|
|
"filmDetails": {
|
|
|
|
"id": "RmlsbURldGFpbHNOb2RlOjE=",
|
|
|
|
"film": {"id": "RmlsbU5vZGU6MQ=="},
|
|
|
|
},
|
|
|
|
"film": {
|
|
|
|
"id": "RmlsbU5vZGU6MQ==",
|
|
|
|
"details": {"id": "RmlsbURldGFpbHNOb2RlOjE="},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
def test_should_query_connectionfields():
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2019-07-09 16:03:11 +03:00
|
|
|
fields = ("articles",)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_all_reporters(self, info, **args):
|
2016-09-18 02:29:00 +03:00
|
|
|
return [Reporter(id=1)]
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2016-09-18 02:29:00 +03:00
|
|
|
query ReporterConnectionQuery {
|
|
|
|
allReporters {
|
|
|
|
pageInfo {
|
|
|
|
hasNextPage
|
|
|
|
}
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2016-09-18 02:29:00 +03:00
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == {
|
2018-07-20 02:51:33 +03:00
|
|
|
"allReporters": {
|
|
|
|
"pageInfo": {"hasNextPage": False},
|
|
|
|
"edges": [{"node": {"id": "UmVwb3J0ZXJUeXBlOjE="}}],
|
2016-09-18 02:29:00 +03:00
|
|
|
}
|
|
|
|
}
|
2016-12-30 12:34:59 +03:00
|
|
|
|
2016-12-30 12:57:50 +03:00
|
|
|
|
2017-06-05 22:56:09 +03:00
|
|
|
def test_should_keep_annotations():
|
2018-07-20 02:51:33 +03:00
|
|
|
from django.db.models import Count, Avg
|
2017-06-05 22:56:09 +03:00
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2019-07-09 16:03:11 +03:00
|
|
|
fields = ("articles",)
|
2017-06-05 22:56:09 +03:00
|
|
|
|
|
|
|
class ArticleType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Article
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2018-07-20 02:51:33 +03:00
|
|
|
filter_fields = ("lang",)
|
2017-06-05 22:56:09 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
all_articles = DjangoConnectionField(ArticleType)
|
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_all_reporters(self, info, **args):
|
2018-07-20 02:51:33 +03:00
|
|
|
return Reporter.objects.annotate(articles_c=Count("articles")).order_by(
|
|
|
|
"articles_c"
|
|
|
|
)
|
2017-06-05 22:56:09 +03:00
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_all_articles(self, info, **args):
|
2018-07-20 02:51:33 +03:00
|
|
|
return Article.objects.annotate(import_avg=Avg("importance")).order_by(
|
|
|
|
"import_avg"
|
|
|
|
)
|
2017-06-05 22:56:09 +03:00
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2017-06-05 22:56:09 +03:00
|
|
|
query ReporterConnectionQuery {
|
|
|
|
allReporters {
|
|
|
|
pageInfo {
|
|
|
|
hasNextPage
|
|
|
|
}
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allArticles {
|
|
|
|
pageInfo {
|
|
|
|
hasNextPage
|
|
|
|
}
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2017-06-05 22:56:09 +03:00
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
not DJANGO_FILTER_INSTALLED, reason="django-filter should be installed"
|
|
|
|
)
|
2016-12-30 12:34:59 +03:00
|
|
|
def test_should_query_node_filtering():
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2016-12-30 12:34:59 +03:00
|
|
|
|
|
|
|
class ArticleType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Article
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2018-07-20 02:51:33 +03:00
|
|
|
filter_fields = ("lang",)
|
2021-02-23 07:21:32 +03:00
|
|
|
convert_choices_to_enum = False
|
2016-12-30 12:34:59 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
r = Reporter.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
2016-12-30 12:34:59 +03:00
|
|
|
)
|
|
|
|
Article.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
headline="Article Node 1",
|
2016-12-30 12:34:59 +03:00
|
|
|
pub_date=datetime.date.today(),
|
2017-12-05 23:04:29 +03:00
|
|
|
pub_date_time=datetime.datetime.now(),
|
2016-12-30 12:34:59 +03:00
|
|
|
reporter=r,
|
|
|
|
editor=r,
|
2018-07-20 02:51:33 +03:00
|
|
|
lang="es",
|
2016-12-30 12:34:59 +03:00
|
|
|
)
|
|
|
|
Article.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
headline="Article Node 2",
|
2016-12-30 12:34:59 +03:00
|
|
|
pub_date=datetime.date.today(),
|
2017-12-05 23:04:29 +03:00
|
|
|
pub_date_time=datetime.datetime.now(),
|
2016-12-30 12:34:59 +03:00
|
|
|
reporter=r,
|
|
|
|
editor=r,
|
2018-07-20 02:51:33 +03:00
|
|
|
lang="en",
|
2016-12-30 12:34:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2016-12-30 12:34:59 +03:00
|
|
|
query NodeFilteringQuery {
|
|
|
|
allReporters {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
articles(lang: "es") {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2016-12-30 12:34:59 +03:00
|
|
|
|
2017-03-05 22:17:00 +03:00
|
|
|
expected = {
|
2018-07-20 02:51:33 +03:00
|
|
|
"allReporters": {
|
|
|
|
"edges": [
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": "UmVwb3J0ZXJUeXBlOjE=",
|
|
|
|
"articles": {
|
|
|
|
"edges": [{"node": {"id": "QXJ0aWNsZVR5cGU6MQ=="}}]
|
|
|
|
},
|
2017-03-05 22:17:00 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
]
|
2017-03-05 22:17:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
not DJANGO_FILTER_INSTALLED, reason="django-filter should be installed"
|
|
|
|
)
|
2017-10-11 16:43:50 +03:00
|
|
|
def test_should_query_node_filtering_with_distinct_queryset():
|
|
|
|
class FilmType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Film
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2018-07-20 02:51:33 +03:00
|
|
|
filter_fields = ("genre",)
|
2017-10-11 16:43:50 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
films = DjangoConnectionField(FilmType)
|
|
|
|
|
|
|
|
# def resolve_all_reporters_with_berlin_films(self, args, context, info):
|
|
|
|
# return Reporter.objects.filter(Q(films__film__location__contains="Berlin") | Q(a_choice=1))
|
|
|
|
|
2018-02-28 19:45:25 +03:00
|
|
|
def resolve_films(self, info, **args):
|
2018-07-20 02:51:33 +03:00
|
|
|
return Film.objects.filter(
|
|
|
|
Q(details__location__contains="Berlin") | Q(genre__in=["ot"])
|
|
|
|
).distinct()
|
2017-10-11 16:43:50 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
f = Film.objects.create()
|
|
|
|
fd = FilmDetails.objects.create(location="Berlin", film=f)
|
2017-10-11 16:43:50 +03:00
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2017-10-11 16:43:50 +03:00
|
|
|
query NodeFilteringQuery {
|
|
|
|
films {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
genre
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2017-10-11 16:43:50 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
expected = {"films": {"edges": [{"node": {"genre": "OT"}}]}}
|
2017-10-11 16:43:50 +03:00
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
not DJANGO_FILTER_INSTALLED, reason="django-filter should be installed"
|
|
|
|
)
|
2017-03-05 22:17:00 +03:00
|
|
|
def test_should_query_node_multiple_filtering():
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-03-05 22:17:00 +03:00
|
|
|
|
|
|
|
class ArticleType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Article
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2018-07-20 02:51:33 +03:00
|
|
|
filter_fields = ("lang", "headline")
|
2021-02-23 07:21:32 +03:00
|
|
|
convert_choices_to_enum = False
|
2017-03-05 22:17:00 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
r = Reporter.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
2017-03-05 22:17:00 +03:00
|
|
|
)
|
|
|
|
Article.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
headline="Article Node 1",
|
2017-03-05 22:17:00 +03:00
|
|
|
pub_date=datetime.date.today(),
|
2017-12-05 23:04:29 +03:00
|
|
|
pub_date_time=datetime.datetime.now(),
|
2017-03-05 22:17:00 +03:00
|
|
|
reporter=r,
|
|
|
|
editor=r,
|
2018-07-20 02:51:33 +03:00
|
|
|
lang="es",
|
2017-03-05 22:17:00 +03:00
|
|
|
)
|
|
|
|
Article.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
headline="Article Node 2",
|
2017-03-05 22:17:00 +03:00
|
|
|
pub_date=datetime.date.today(),
|
2017-12-05 23:04:29 +03:00
|
|
|
pub_date_time=datetime.datetime.now(),
|
2017-03-05 22:17:00 +03:00
|
|
|
reporter=r,
|
|
|
|
editor=r,
|
2018-07-20 02:51:33 +03:00
|
|
|
lang="es",
|
2017-03-05 22:17:00 +03:00
|
|
|
)
|
|
|
|
Article.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
headline="Article Node 3",
|
2017-03-05 22:17:00 +03:00
|
|
|
pub_date=datetime.date.today(),
|
2017-12-05 23:04:29 +03:00
|
|
|
pub_date_time=datetime.datetime.now(),
|
2017-03-05 22:17:00 +03:00
|
|
|
reporter=r,
|
|
|
|
editor=r,
|
2018-07-20 02:51:33 +03:00
|
|
|
lang="en",
|
2017-03-05 22:17:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2017-03-05 22:17:00 +03:00
|
|
|
query NodeFilteringQuery {
|
|
|
|
allReporters {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
2017-03-06 23:00:01 +03:00
|
|
|
articles(lang: "es", headline: "Article Node 1") {
|
2017-03-05 22:17:00 +03:00
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2017-03-05 22:17:00 +03:00
|
|
|
|
2016-12-30 12:34:59 +03:00
|
|
|
expected = {
|
2018-07-20 02:51:33 +03:00
|
|
|
"allReporters": {
|
|
|
|
"edges": [
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": "UmVwb3J0ZXJUeXBlOjE=",
|
|
|
|
"articles": {
|
|
|
|
"edges": [{"node": {"id": "QXJ0aWNsZVR5cGU6MQ=="}}]
|
|
|
|
},
|
2016-12-30 15:27:45 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
]
|
2016-12-30 15:27:45 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 20:13:09 +03:00
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
2017-04-15 12:09:05 +03:00
|
|
|
|
|
|
|
|
2020-04-20 15:23:20 +03:00
|
|
|
def test_should_enforce_first_or_last(graphene_settings):
|
2017-04-15 12:09:05 +03:00
|
|
|
graphene_settings.RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST = True
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-04-15 12:09:05 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
r = Reporter.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
2017-04-15 12:09:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2017-04-15 12:09:05 +03:00
|
|
|
query NodeFilteringQuery {
|
|
|
|
allReporters {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2017-04-15 12:09:05 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
expected = {"allReporters": None}
|
2017-04-15 12:09:05 +03:00
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert len(result.errors) == 1
|
2020-05-09 14:13:47 +03:00
|
|
|
assert str(result.errors[0]).startswith(
|
2018-07-20 02:51:33 +03:00
|
|
|
"You must provide a `first` or `last` value to properly "
|
2020-05-09 14:13:47 +03:00
|
|
|
"paginate the `allReporters` connection.\n"
|
2017-04-15 12:09:05 +03:00
|
|
|
)
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
2020-04-20 15:23:20 +03:00
|
|
|
def test_should_error_if_first_is_greater_than_max(graphene_settings):
|
2017-04-15 12:09:05 +03:00
|
|
|
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 100
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-04-15 12:09:05 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
2019-11-28 13:49:37 +03:00
|
|
|
assert Query.all_reporters.max_limit == 100
|
|
|
|
|
2017-04-15 12:09:05 +03:00
|
|
|
r = Reporter.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
2017-04-15 12:09:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2017-04-15 12:09:05 +03:00
|
|
|
query NodeFilteringQuery {
|
|
|
|
allReporters(first: 101) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2017-04-15 12:09:05 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
expected = {"allReporters": None}
|
2017-04-15 12:09:05 +03:00
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert len(result.errors) == 1
|
2020-05-09 14:13:47 +03:00
|
|
|
assert str(result.errors[0]).startswith(
|
2018-07-20 02:51:33 +03:00
|
|
|
"Requesting 101 records on the `allReporters` connection "
|
2020-05-09 14:13:47 +03:00
|
|
|
"exceeds the `first` limit of 100 records.\n"
|
2017-04-15 12:09:05 +03:00
|
|
|
)
|
|
|
|
assert result.data == expected
|
|
|
|
|
2017-05-20 02:12:28 +03:00
|
|
|
|
2020-04-20 15:23:20 +03:00
|
|
|
def test_should_error_if_last_is_greater_than_max(graphene_settings):
|
2017-12-12 20:24:11 +03:00
|
|
|
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 100
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-12-12 20:24:11 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
2019-11-28 13:49:37 +03:00
|
|
|
assert Query.all_reporters.max_limit == 100
|
|
|
|
|
2017-12-12 20:24:11 +03:00
|
|
|
r = Reporter.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
2017-12-12 20:24:11 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2017-12-12 20:24:11 +03:00
|
|
|
query NodeFilteringQuery {
|
|
|
|
allReporters(last: 101) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2017-12-12 20:24:11 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
expected = {"allReporters": None}
|
2017-12-12 20:24:11 +03:00
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert len(result.errors) == 1
|
2020-05-09 14:13:47 +03:00
|
|
|
assert str(result.errors[0]).startswith(
|
2018-07-20 02:51:33 +03:00
|
|
|
"Requesting 101 records on the `allReporters` connection "
|
2020-05-09 14:13:47 +03:00
|
|
|
"exceeds the `last` limit of 100 records.\n"
|
2017-12-12 20:24:11 +03:00
|
|
|
)
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
2017-05-20 02:12:28 +03:00
|
|
|
def test_should_query_promise_connectionfields():
|
|
|
|
from promise import Promise
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-05-20 02:12:28 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_all_reporters(self, info, **args):
|
2020-05-09 14:13:47 +03:00
|
|
|
return Promise.resolve([Reporter(id=1)]).get()
|
2018-03-28 13:28:58 +03:00
|
|
|
|
2017-05-20 02:12:28 +03:00
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2017-05-20 02:12:28 +03:00
|
|
|
query ReporterPromiseConnectionQuery {
|
|
|
|
allReporters(first: 1) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2017-05-20 02:12:28 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
expected = {"allReporters": {"edges": [{"node": {"id": "UmVwb3J0ZXJUeXBlOjE="}}]}}
|
2017-05-20 02:12:28 +03:00
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2017-12-12 20:49:02 +03:00
|
|
|
def test_should_query_connectionfields_with_last():
|
|
|
|
r = Reporter.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
2017-12-12 20:49:02 +03:00
|
|
|
)
|
2017-12-12 20:33:32 +03:00
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-12-12 20:33:32 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
def resolve_all_reporters(self, info, **args):
|
2017-12-12 20:49:02 +03:00
|
|
|
return Reporter.objects.all()
|
2018-03-28 13:28:58 +03:00
|
|
|
|
2017-12-12 20:33:32 +03:00
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2017-12-12 20:49:02 +03:00
|
|
|
query ReporterLastQuery {
|
2017-12-12 20:33:32 +03:00
|
|
|
allReporters(last: 1) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2017-12-12 20:33:32 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
expected = {"allReporters": {"edges": [{"node": {"id": "UmVwb3J0ZXJUeXBlOjE="}}]}}
|
2017-12-12 20:33:32 +03:00
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2017-12-12 20:52:32 +03:00
|
|
|
def test_should_query_connectionfields_with_manager():
|
|
|
|
r = Reporter.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
2017-12-12 20:52:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
r = Reporter.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
first_name="John", last_name="NotDoe", email="johndoe@example.com", a_choice=1
|
2017-12-12 20:52:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-12-12 20:52:32 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
2018-07-20 02:51:33 +03:00
|
|
|
all_reporters = DjangoConnectionField(ReporterType, on="doe_objects")
|
2017-12-12 20:52:32 +03:00
|
|
|
|
|
|
|
def resolve_all_reporters(self, info, **args):
|
|
|
|
return Reporter.objects.all()
|
2018-03-28 13:28:58 +03:00
|
|
|
|
2017-12-12 20:52:32 +03:00
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2017-12-12 20:52:32 +03:00
|
|
|
query ReporterLastQuery {
|
2019-11-28 13:49:37 +03:00
|
|
|
allReporters(first: 1) {
|
2017-12-12 20:52:32 +03:00
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2017-12-12 20:52:32 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
expected = {"allReporters": {"edges": [{"node": {"id": "UmVwb3J0ZXJUeXBlOjE="}}]}}
|
2017-12-12 20:52:32 +03:00
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
2017-05-20 02:12:28 +03:00
|
|
|
|
|
|
|
def test_should_query_dataloader_fields():
|
|
|
|
from promise import Promise
|
|
|
|
from promise.dataloader import DataLoader
|
|
|
|
|
|
|
|
def article_batch_load_fn(keys):
|
|
|
|
queryset = Article.objects.filter(reporter_id__in=keys)
|
2018-07-20 02:51:33 +03:00
|
|
|
return Promise.resolve(
|
|
|
|
[
|
|
|
|
[article for article in queryset if article.reporter_id == id]
|
|
|
|
for id in keys
|
|
|
|
]
|
|
|
|
)
|
2017-05-20 02:12:28 +03:00
|
|
|
|
|
|
|
article_loader = DataLoader(article_batch_load_fn)
|
|
|
|
|
|
|
|
class ArticleType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Article
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-05-20 02:12:28 +03:00
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2017-07-25 08:27:50 +03:00
|
|
|
use_connection = True
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-05-20 02:12:28 +03:00
|
|
|
|
|
|
|
articles = DjangoConnectionField(ArticleType)
|
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_articles(self, info, **args):
|
2020-05-09 14:13:47 +03:00
|
|
|
return article_loader.load(self.id).get()
|
2017-05-20 02:12:28 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
r = Reporter.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
2017-05-20 02:12:28 +03:00
|
|
|
)
|
2018-01-15 06:36:52 +03:00
|
|
|
|
2017-05-20 02:12:28 +03:00
|
|
|
Article.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
headline="Article Node 1",
|
2017-05-20 02:12:28 +03:00
|
|
|
pub_date=datetime.date.today(),
|
2017-12-05 23:04:29 +03:00
|
|
|
pub_date_time=datetime.datetime.now(),
|
2017-05-20 02:12:28 +03:00
|
|
|
reporter=r,
|
|
|
|
editor=r,
|
2018-07-20 02:51:33 +03:00
|
|
|
lang="es",
|
2017-05-20 02:12:28 +03:00
|
|
|
)
|
|
|
|
Article.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
headline="Article Node 2",
|
2017-05-20 02:12:28 +03:00
|
|
|
pub_date=datetime.date.today(),
|
2017-12-05 23:04:29 +03:00
|
|
|
pub_date_time=datetime.datetime.now(),
|
2017-05-20 02:12:28 +03:00
|
|
|
reporter=r,
|
|
|
|
editor=r,
|
2018-07-20 02:51:33 +03:00
|
|
|
lang="en",
|
2017-05-20 02:12:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2017-05-20 02:12:28 +03:00
|
|
|
query ReporterPromiseConnectionQuery {
|
|
|
|
allReporters(first: 1) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
articles(first: 2) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
headline
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2017-05-20 02:12:28 +03:00
|
|
|
|
|
|
|
expected = {
|
2018-07-20 02:51:33 +03:00
|
|
|
"allReporters": {
|
|
|
|
"edges": [
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": "UmVwb3J0ZXJUeXBlOjE=",
|
|
|
|
"articles": {
|
|
|
|
"edges": [
|
|
|
|
{"node": {"headline": "Article Node 1"}},
|
|
|
|
{"node": {"headline": "Article Node 2"}},
|
|
|
|
]
|
|
|
|
},
|
2017-05-20 02:12:28 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
]
|
2017-05-20 02:12:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
2017-04-21 20:25:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_handle_inherited_choices():
|
|
|
|
class BaseModel(models.Model):
|
2018-07-20 02:51:33 +03:00
|
|
|
choice_field = models.IntegerField(choices=((0, "zero"), (1, "one")))
|
2017-04-21 20:25:30 +03:00
|
|
|
|
|
|
|
class ChildModel(BaseModel):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
class BaseType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = BaseModel
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-04-21 20:25:30 +03:00
|
|
|
|
|
|
|
class ChildType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = ChildModel
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-04-21 20:25:30 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
base = graphene.Field(BaseType)
|
|
|
|
child = graphene.Field(ChildType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2017-04-21 20:25:30 +03:00
|
|
|
query {
|
|
|
|
child {
|
|
|
|
choiceField
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2017-04-21 20:25:30 +03:00
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
2018-01-15 06:36:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_proxy_model_support():
|
|
|
|
"""
|
2019-03-27 07:09:25 +03:00
|
|
|
This test asserts that we can query for all Reporters and proxied Reporters.
|
2018-01-15 06:36:52 +03:00
|
|
|
"""
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
class ReporterType(DjangoObjectType):
|
2018-01-15 06:36:52 +03:00
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2018-01-15 06:36:52 +03:00
|
|
|
use_connection = True
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2018-01-15 06:36:52 +03:00
|
|
|
|
2019-03-27 07:09:25 +03:00
|
|
|
class CNNReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = CNNReporter
|
|
|
|
interfaces = (Node,)
|
|
|
|
use_connection = True
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2019-03-27 07:09:25 +03:00
|
|
|
|
|
|
|
reporter = Reporter.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
2018-01-15 06:36:52 +03:00
|
|
|
)
|
|
|
|
|
2019-03-27 07:09:25 +03:00
|
|
|
cnn_reporter = CNNReporter.objects.create(
|
2018-07-20 02:51:33 +03:00
|
|
|
first_name="Some",
|
|
|
|
last_name="Guy",
|
|
|
|
email="someguy@cnn.com",
|
2018-01-15 06:36:52 +03:00
|
|
|
a_choice=1,
|
|
|
|
reporter_type=2, # set this guy to be CNN
|
|
|
|
)
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
2019-03-27 07:09:25 +03:00
|
|
|
cnn_reporters = DjangoConnectionField(CNNReporterType)
|
2018-01-15 06:36:52 +03:00
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2018-01-15 06:36:52 +03:00
|
|
|
query ProxyModelQuery {
|
|
|
|
allReporters {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-27 07:09:25 +03:00
|
|
|
cnnReporters {
|
2018-01-15 06:36:52 +03:00
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2018-01-15 06:36:52 +03:00
|
|
|
|
|
|
|
expected = {
|
2018-07-20 02:51:33 +03:00
|
|
|
"allReporters": {
|
|
|
|
"edges": [
|
2019-03-27 23:56:10 +03:00
|
|
|
{"node": {"id": to_global_id("ReporterType", reporter.id)}},
|
|
|
|
{"node": {"id": to_global_id("ReporterType", cnn_reporter.id)}},
|
2019-03-27 07:09:25 +03:00
|
|
|
]
|
|
|
|
},
|
|
|
|
"cnnReporters": {
|
|
|
|
"edges": [
|
2019-03-27 23:56:10 +03:00
|
|
|
{"node": {"id": to_global_id("CNNReporterType", cnn_reporter.id)}}
|
2018-01-15 06:36:52 +03:00
|
|
|
]
|
2019-06-11 06:54:30 +03:00
|
|
|
},
|
2018-01-15 06:36:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
result = schema.execute(query)
|
2019-03-27 07:09:25 +03:00
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
2019-06-11 06:54:30 +03:00
|
|
|
|
2019-03-31 14:01:17 +03:00
|
|
|
|
2023-07-18 20:17:45 +03:00
|
|
|
def test_model_inheritance_support_reverse_relationships():
|
|
|
|
"""
|
|
|
|
This test asserts that we can query reverse relationships for all Reporters and proxied Reporters and multi table Reporters.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class FilmType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Film
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (Node,)
|
|
|
|
use_connection = True
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
class CNNReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = CNNReporter
|
|
|
|
interfaces = (Node,)
|
|
|
|
use_connection = True
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
class APNewsReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = APNewsReporter
|
|
|
|
interfaces = (Node,)
|
|
|
|
use_connection = True
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
film = Film.objects.create(genre="do")
|
|
|
|
|
|
|
|
reporter = Reporter.objects.create(
|
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
|
|
|
)
|
|
|
|
|
|
|
|
cnn_reporter = CNNReporter.objects.create(
|
|
|
|
first_name="Some",
|
|
|
|
last_name="Guy",
|
|
|
|
email="someguy@cnn.com",
|
|
|
|
a_choice=1,
|
|
|
|
reporter_type=2, # set this guy to be CNN
|
|
|
|
)
|
|
|
|
|
|
|
|
ap_news_reporter = APNewsReporter.objects.create(
|
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
|
|
|
)
|
|
|
|
|
|
|
|
film.reporters.add(cnn_reporter, ap_news_reporter)
|
|
|
|
film.save()
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
cnn_reporters = DjangoConnectionField(CNNReporterType)
|
|
|
|
ap_news_reporters = DjangoConnectionField(APNewsReporterType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
query = """
|
|
|
|
query ProxyModelQuery {
|
|
|
|
allReporters {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
films {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cnnReporters {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
films {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
apNewsReporters {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
films {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
"allReporters": {
|
|
|
|
"edges": [
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": to_global_id("ReporterType", reporter.id),
|
|
|
|
"films": [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": to_global_id("ReporterType", cnn_reporter.id),
|
|
|
|
"films": [{"id": f"{film.id}"}],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": to_global_id("ReporterType", ap_news_reporter.id),
|
|
|
|
"films": [{"id": f"{film.id}"}],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"cnnReporters": {
|
|
|
|
"edges": [
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": to_global_id("CNNReporterType", cnn_reporter.id),
|
|
|
|
"films": [{"id": f"{film.id}"}],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"apNewsReporters": {
|
|
|
|
"edges": [
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": to_global_id("APNewsReporterType", ap_news_reporter.id),
|
|
|
|
"films": [{"id": f"{film.id}"}],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_model_inheritance_support_local_relationships():
|
|
|
|
"""
|
|
|
|
This test asserts that we can query local relationships for all Reporters and proxied Reporters and multi table Reporters.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class PersonType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Person
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (Node,)
|
|
|
|
use_connection = True
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
class CNNReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = CNNReporter
|
|
|
|
interfaces = (Node,)
|
|
|
|
use_connection = True
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
class APNewsReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = APNewsReporter
|
|
|
|
interfaces = (Node,)
|
|
|
|
use_connection = True
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
film = Film.objects.create(genre="do")
|
|
|
|
|
|
|
|
reporter = Reporter.objects.create(
|
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
|
|
|
)
|
|
|
|
|
|
|
|
reporter_fan = Person.objects.create(name="Reporter Fan")
|
|
|
|
|
|
|
|
reporter.fans.add(reporter_fan)
|
|
|
|
reporter.save()
|
|
|
|
|
|
|
|
cnn_reporter = CNNReporter.objects.create(
|
|
|
|
first_name="Some",
|
|
|
|
last_name="Guy",
|
|
|
|
email="someguy@cnn.com",
|
|
|
|
a_choice=1,
|
|
|
|
reporter_type=2, # set this guy to be CNN
|
|
|
|
)
|
|
|
|
cnn_fan = Person.objects.create(name="CNN Fan")
|
|
|
|
cnn_reporter.fans.add(cnn_fan)
|
|
|
|
cnn_reporter.save()
|
|
|
|
|
|
|
|
ap_news_reporter = APNewsReporter.objects.create(
|
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
|
|
|
)
|
|
|
|
ap_news_fan = Person.objects.create(name="AP News Fan")
|
|
|
|
ap_news_reporter.fans.add(ap_news_fan)
|
|
|
|
ap_news_reporter.save()
|
|
|
|
|
|
|
|
film.reporters.add(cnn_reporter, ap_news_reporter)
|
|
|
|
film.save()
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
cnn_reporters = DjangoConnectionField(CNNReporterType)
|
|
|
|
ap_news_reporters = DjangoConnectionField(APNewsReporterType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
query = """
|
|
|
|
query ProxyModelQuery {
|
|
|
|
allReporters {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
fans {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cnnReporters {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
fans {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
apNewsReporters {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
fans {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
"allReporters": {
|
|
|
|
"edges": [
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": to_global_id("ReporterType", reporter.id),
|
|
|
|
"fans": [{"name": f"{reporter_fan.name}"}],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": to_global_id("ReporterType", cnn_reporter.id),
|
|
|
|
"fans": [{"name": f"{cnn_fan.name}"}],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": to_global_id("ReporterType", ap_news_reporter.id),
|
|
|
|
"fans": [{"name": f"{ap_news_fan.name}"}],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"cnnReporters": {
|
|
|
|
"edges": [
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": to_global_id("CNNReporterType", cnn_reporter.id),
|
|
|
|
"fans": [{"name": f"{cnn_fan.name}"}],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"apNewsReporters": {
|
|
|
|
"edges": [
|
|
|
|
{
|
|
|
|
"node": {
|
|
|
|
"id": to_global_id("APNewsReporterType", ap_news_reporter.id),
|
|
|
|
"fans": [{"name": f"{ap_news_fan.name}"}],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
2019-03-31 14:01:17 +03:00
|
|
|
def test_should_resolve_get_queryset_connectionfields():
|
|
|
|
reporter_1 = Reporter.objects.create(
|
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
|
|
|
)
|
|
|
|
reporter_2 = CNNReporter.objects.create(
|
|
|
|
first_name="Some",
|
|
|
|
last_name="Guy",
|
|
|
|
email="someguy@cnn.com",
|
|
|
|
a_choice=1,
|
|
|
|
reporter_type=2, # set this guy to be CNN
|
|
|
|
)
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2019-03-31 14:01:17 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_queryset(cls, queryset, info):
|
|
|
|
return queryset.filter(reporter_type=2)
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
query = """
|
|
|
|
query ReporterPromiseConnectionQuery {
|
|
|
|
allReporters(first: 1) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
expected = {"allReporters": {"edges": [{"node": {"id": "UmVwb3J0ZXJUeXBlOjI="}}]}}
|
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
2019-07-02 21:37:50 +03:00
|
|
|
|
|
|
|
|
2020-07-09 20:01:22 +03:00
|
|
|
def test_connection_should_limit_after_to_list_length():
|
|
|
|
reporter_1 = Reporter.objects.create(
|
|
|
|
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
|
|
|
|
)
|
|
|
|
reporter_2 = Reporter.objects.create(
|
|
|
|
first_name="Some", last_name="Guy", email="someguy@cnn.com", a_choice=1
|
|
|
|
)
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (Node,)
|
2020-08-06 22:44:33 +03:00
|
|
|
fields = "__all__"
|
2020-07-09 20:01:22 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
query = """
|
|
|
|
query ReporterPromiseConnectionQuery ($after: String) {
|
|
|
|
allReporters(first: 1 after: $after) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
after = base64.b64encode(b"arrayconnection:10").decode()
|
|
|
|
result = schema.execute(query, variable_values=dict(after=after))
|
|
|
|
expected = {"allReporters": {"edges": []}}
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
2020-06-06 21:00:21 +03:00
|
|
|
REPORTERS = [
|
|
|
|
dict(
|
2022-10-19 17:10:30 +03:00
|
|
|
first_name=f"First {i}",
|
|
|
|
last_name=f"Last {i}",
|
|
|
|
email=f"johndoe+{i}@example.com",
|
2020-06-06 21:00:21 +03:00
|
|
|
a_choice=1,
|
|
|
|
)
|
|
|
|
for i in range(6)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_return_max_limit(graphene_settings):
|
|
|
|
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 4
|
|
|
|
reporters = [Reporter(**kwargs) for kwargs in REPORTERS]
|
|
|
|
Reporter.objects.bulk_create(reporters)
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (Node,)
|
2020-08-06 22:44:33 +03:00
|
|
|
fields = "__all__"
|
2020-06-06 21:00:21 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
query = """
|
|
|
|
query AllReporters {
|
|
|
|
allReporters {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert len(result.data["allReporters"]["edges"]) == 4
|
|
|
|
|
|
|
|
|
2020-06-25 15:00:24 +03:00
|
|
|
def test_should_have_next_page(graphene_settings):
|
2020-06-25 17:10:56 +03:00
|
|
|
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 4
|
2020-06-25 15:00:24 +03:00
|
|
|
reporters = [Reporter(**kwargs) for kwargs in REPORTERS]
|
|
|
|
Reporter.objects.bulk_create(reporters)
|
|
|
|
db_reporters = Reporter.objects.all()
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (Node,)
|
2020-08-06 22:44:33 +03:00
|
|
|
fields = "__all__"
|
2020-06-25 15:00:24 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
query = """
|
|
|
|
query AllReporters($first: Int, $after: String) {
|
|
|
|
allReporters(first: $first, after: $after) {
|
|
|
|
pageInfo {
|
|
|
|
hasNextPage
|
|
|
|
endCursor
|
|
|
|
}
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2020-06-25 17:10:56 +03:00
|
|
|
result = schema.execute(query, variable_values={})
|
2020-06-25 15:00:24 +03:00
|
|
|
assert not result.errors
|
|
|
|
assert len(result.data["allReporters"]["edges"]) == 4
|
|
|
|
assert result.data["allReporters"]["pageInfo"]["hasNextPage"]
|
|
|
|
|
|
|
|
last_result = result.data["allReporters"]["pageInfo"]["endCursor"]
|
|
|
|
result2 = schema.execute(query, variable_values=dict(first=4, after=last_result))
|
|
|
|
assert not result2.errors
|
|
|
|
assert len(result2.data["allReporters"]["edges"]) == 2
|
|
|
|
assert not result2.data["allReporters"]["pageInfo"]["hasNextPage"]
|
|
|
|
gql_reporters = (
|
|
|
|
result.data["allReporters"]["edges"] + result2.data["allReporters"]["edges"]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {to_global_id("ReporterType", reporter.id) for reporter in db_reporters} == {
|
|
|
|
gql_reporter["node"]["id"] for gql_reporter in gql_reporters
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-22 18:01:28 +03:00
|
|
|
@pytest.mark.parametrize("max_limit", [100, 4])
|
2020-12-31 02:37:57 +03:00
|
|
|
class TestBackwardPagination:
|
|
|
|
def setup_schema(self, graphene_settings, max_limit):
|
|
|
|
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = max_limit
|
|
|
|
reporters = [Reporter(**kwargs) for kwargs in REPORTERS]
|
|
|
|
Reporter.objects.bulk_create(reporters)
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (Node,)
|
2021-02-23 07:21:32 +03:00
|
|
|
fields = "__all__"
|
2020-12-31 02:37:57 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
return schema
|
|
|
|
|
2022-09-22 18:01:28 +03:00
|
|
|
def test_query_last(self, graphene_settings, max_limit):
|
|
|
|
schema = self.setup_schema(graphene_settings, max_limit=max_limit)
|
2020-12-31 02:37:57 +03:00
|
|
|
query_last = """
|
|
|
|
query {
|
|
|
|
allReporters(last: 3) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
firstName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
result = schema.execute(query_last)
|
|
|
|
assert not result.errors
|
|
|
|
assert len(result.data["allReporters"]["edges"]) == 3
|
|
|
|
assert [
|
|
|
|
e["node"]["firstName"] for e in result.data["allReporters"]["edges"]
|
|
|
|
] == ["First 3", "First 4", "First 5"]
|
|
|
|
|
2022-09-22 18:01:28 +03:00
|
|
|
def test_query_first_and_last(self, graphene_settings, max_limit):
|
|
|
|
schema = self.setup_schema(graphene_settings, max_limit=max_limit)
|
2020-12-31 02:37:57 +03:00
|
|
|
query_first_and_last = """
|
|
|
|
query {
|
|
|
|
allReporters(first: 4, last: 3) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
firstName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
result = schema.execute(query_first_and_last)
|
|
|
|
assert not result.errors
|
|
|
|
assert len(result.data["allReporters"]["edges"]) == 3
|
|
|
|
assert [
|
|
|
|
e["node"]["firstName"] for e in result.data["allReporters"]["edges"]
|
|
|
|
] == ["First 1", "First 2", "First 3"]
|
|
|
|
|
2022-09-22 18:01:28 +03:00
|
|
|
def test_query_first_last_and_after(self, graphene_settings, max_limit):
|
|
|
|
schema = self.setup_schema(graphene_settings, max_limit=max_limit)
|
2020-12-31 02:37:57 +03:00
|
|
|
query_first_last_and_after = """
|
|
|
|
query queryAfter($after: String) {
|
|
|
|
allReporters(first: 4, last: 3, after: $after) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
firstName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
after = base64.b64encode(b"arrayconnection:0").decode()
|
|
|
|
result = schema.execute(
|
2022-09-22 18:01:28 +03:00
|
|
|
query_first_last_and_after,
|
|
|
|
variable_values=dict(after=after),
|
2020-12-31 02:37:57 +03:00
|
|
|
)
|
|
|
|
assert not result.errors
|
|
|
|
assert len(result.data["allReporters"]["edges"]) == 3
|
|
|
|
assert [
|
|
|
|
e["node"]["firstName"] for e in result.data["allReporters"]["edges"]
|
|
|
|
] == ["First 2", "First 3", "First 4"]
|
|
|
|
|
2022-09-22 18:01:28 +03:00
|
|
|
def test_query_last_and_before(self, graphene_settings, max_limit):
|
|
|
|
schema = self.setup_schema(graphene_settings, max_limit=max_limit)
|
|
|
|
query_first_last_and_after = """
|
|
|
|
query queryAfter($before: String) {
|
|
|
|
allReporters(last: 1, before: $before) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
firstName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-31 02:37:57 +03:00
|
|
|
"""
|
|
|
|
|
2022-09-22 18:01:28 +03:00
|
|
|
result = schema.execute(
|
|
|
|
query_first_last_and_after,
|
|
|
|
)
|
|
|
|
assert not result.errors
|
|
|
|
assert len(result.data["allReporters"]["edges"]) == 1
|
|
|
|
assert result.data["allReporters"]["edges"][0]["node"]["firstName"] == "First 5"
|
|
|
|
|
|
|
|
before = base64.b64encode(b"arrayconnection:5").decode()
|
|
|
|
result = schema.execute(
|
|
|
|
query_first_last_and_after,
|
|
|
|
variable_values=dict(before=before),
|
|
|
|
)
|
|
|
|
assert not result.errors
|
|
|
|
assert len(result.data["allReporters"]["edges"]) == 1
|
|
|
|
assert result.data["allReporters"]["edges"][0]["node"]["firstName"] == "First 4"
|
2020-12-31 02:37:57 +03:00
|
|
|
|
|
|
|
|
2019-07-02 21:37:50 +03:00
|
|
|
def test_should_preserve_prefetch_related(django_assert_num_queries):
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (graphene.relay.Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2019-07-02 21:37:50 +03:00
|
|
|
|
|
|
|
class FilmType(DjangoObjectType):
|
|
|
|
reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Film
|
|
|
|
interfaces = (graphene.relay.Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2019-07-02 21:37:50 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
films = DjangoConnectionField(FilmType)
|
|
|
|
|
2020-05-09 14:13:47 +03:00
|
|
|
def resolve_films(root, info, **kwargs):
|
2019-07-02 21:37:50 +03:00
|
|
|
qs = Film.objects.prefetch_related("reporters")
|
|
|
|
return qs
|
|
|
|
|
|
|
|
r1 = Reporter.objects.create(first_name="Dave", last_name="Smith")
|
|
|
|
r2 = Reporter.objects.create(first_name="Jane", last_name="Doe")
|
|
|
|
|
|
|
|
f1 = Film.objects.create()
|
|
|
|
f1.reporters.set([r1, r2])
|
|
|
|
f2 = Film.objects.create()
|
|
|
|
f2.reporters.set([r2])
|
|
|
|
|
|
|
|
query = """
|
|
|
|
query {
|
|
|
|
films {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
reporters {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
firstName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
schema = graphene.Schema(query=Query)
|
2020-05-09 14:13:47 +03:00
|
|
|
|
2019-07-02 21:37:50 +03:00
|
|
|
with django_assert_num_queries(3) as captured:
|
|
|
|
result = schema.execute(query)
|
2020-05-09 14:13:47 +03:00
|
|
|
assert not result.errors
|
2019-11-28 13:49:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_preserve_annotations():
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (graphene.relay.Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2019-11-28 13:49:37 +03:00
|
|
|
|
|
|
|
class FilmType(DjangoObjectType):
|
|
|
|
reporters = DjangoConnectionField(ReporterType)
|
|
|
|
reporters_count = graphene.Int()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Film
|
|
|
|
interfaces = (graphene.relay.Node,)
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2019-11-28 13:49:37 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
films = DjangoConnectionField(FilmType)
|
|
|
|
|
2020-05-09 14:13:47 +03:00
|
|
|
def resolve_films(root, info, **kwargs):
|
2019-11-28 13:49:37 +03:00
|
|
|
qs = Film.objects.prefetch_related("reporters")
|
|
|
|
return qs.annotate(reporters_count=models.Count("reporters"))
|
|
|
|
|
|
|
|
r1 = Reporter.objects.create(first_name="Dave", last_name="Smith")
|
|
|
|
r2 = Reporter.objects.create(first_name="Jane", last_name="Doe")
|
|
|
|
|
|
|
|
f1 = Film.objects.create()
|
|
|
|
f1.reporters.set([r1, r2])
|
|
|
|
f2 = Film.objects.create()
|
|
|
|
f2.reporters.set([r2])
|
|
|
|
|
|
|
|
query = """
|
|
|
|
query {
|
|
|
|
films {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
reportersCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors, str(result)
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
"films": {
|
|
|
|
"edges": [{"node": {"reportersCount": 2}}, {"node": {"reportersCount": 1}}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert result.data == expected, str(result.data)
|
2020-05-09 14:13:47 +03:00
|
|
|
assert not result.errors
|
2020-12-31 02:37:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_connection_should_enable_offset_filtering():
|
|
|
|
Reporter.objects.create(first_name="John", last_name="Doe")
|
|
|
|
Reporter.objects.create(first_name="Some", last_name="Guy")
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (Node,)
|
2021-02-23 07:21:32 +03:00
|
|
|
fields = "__all__"
|
2020-12-31 02:37:57 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
query = """
|
|
|
|
query {
|
|
|
|
allReporters(first: 1, offset: 1) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
firstName
|
|
|
|
lastName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
expected = {
|
2022-09-22 12:26:21 +03:00
|
|
|
"allReporters": {
|
|
|
|
"edges": [
|
|
|
|
{"node": {"firstName": "Some", "lastName": "Guy"}},
|
|
|
|
]
|
|
|
|
}
|
2020-12-31 02:37:57 +03:00
|
|
|
}
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_connection_should_enable_offset_filtering_higher_than_max_limit(
|
|
|
|
graphene_settings,
|
|
|
|
):
|
|
|
|
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 2
|
|
|
|
Reporter.objects.create(first_name="John", last_name="Doe")
|
|
|
|
Reporter.objects.create(first_name="Some", last_name="Guy")
|
|
|
|
Reporter.objects.create(first_name="Jane", last_name="Roe")
|
|
|
|
Reporter.objects.create(first_name="Some", last_name="Lady")
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (Node,)
|
2021-02-23 07:21:32 +03:00
|
|
|
fields = "__all__"
|
2020-12-31 02:37:57 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
query = """
|
|
|
|
query {
|
|
|
|
allReporters(first: 1, offset: 3) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
firstName
|
|
|
|
lastName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
expected = {
|
|
|
|
"allReporters": {
|
2022-09-22 12:26:21 +03:00
|
|
|
"edges": [
|
|
|
|
{"node": {"firstName": "Some", "lastName": "Lady"}},
|
|
|
|
]
|
2020-12-31 02:37:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_connection_should_forbid_offset_filtering_with_before():
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (Node,)
|
2021-02-23 07:21:32 +03:00
|
|
|
fields = "__all__"
|
2020-12-31 02:37:57 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
query = """
|
|
|
|
query ReporterPromiseConnectionQuery ($before: String) {
|
|
|
|
allReporters(first: 1, before: $before, offset: 1) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
firstName
|
|
|
|
lastName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
before = base64.b64encode(b"arrayconnection:2").decode()
|
|
|
|
result = schema.execute(query, variable_values=dict(before=before))
|
|
|
|
expected_error = "You can't provide a `before` value at the same time as an `offset` value to properly paginate the `allReporters` connection."
|
|
|
|
assert len(result.errors) == 1
|
|
|
|
assert result.errors[0].message == expected_error
|
|
|
|
|
|
|
|
|
|
|
|
def test_connection_should_allow_offset_filtering_with_after():
|
|
|
|
Reporter.objects.create(first_name="John", last_name="Doe")
|
|
|
|
Reporter.objects.create(first_name="Some", last_name="Guy")
|
|
|
|
Reporter.objects.create(first_name="Jane", last_name="Roe")
|
|
|
|
Reporter.objects.create(first_name="Some", last_name="Lady")
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (Node,)
|
2021-02-23 07:21:32 +03:00
|
|
|
fields = "__all__"
|
2020-12-31 02:37:57 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
query = """
|
|
|
|
query ReporterPromiseConnectionQuery ($after: String) {
|
|
|
|
allReporters(first: 1, after: $after, offset: 1) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
firstName
|
|
|
|
lastName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
after = base64.b64encode(b"arrayconnection:0").decode()
|
|
|
|
result = schema.execute(query, variable_values=dict(after=after))
|
|
|
|
assert not result.errors
|
|
|
|
expected = {
|
2022-09-22 12:26:21 +03:00
|
|
|
"allReporters": {
|
|
|
|
"edges": [
|
|
|
|
{"node": {"firstName": "Jane", "lastName": "Roe"}},
|
|
|
|
]
|
|
|
|
}
|
2020-12-31 02:37:57 +03:00
|
|
|
}
|
|
|
|
assert result.data == expected
|
2022-09-06 15:00:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_connection_should_succeed_if_last_higher_than_number_of_objects():
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
interfaces = (Node,)
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
query = """
|
|
|
|
query ReporterPromiseConnectionQuery ($last: Int) {
|
|
|
|
allReporters(last: $last) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
firstName
|
|
|
|
lastName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
result = schema.execute(query, variable_values=dict(last=2))
|
|
|
|
assert not result.errors
|
|
|
|
expected = {"allReporters": {"edges": []}}
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
Reporter.objects.create(first_name="John", last_name="Doe")
|
|
|
|
Reporter.objects.create(first_name="Some", last_name="Guy")
|
|
|
|
Reporter.objects.create(first_name="Jane", last_name="Roe")
|
|
|
|
Reporter.objects.create(first_name="Some", last_name="Lady")
|
|
|
|
|
|
|
|
result = schema.execute(query, variable_values=dict(last=2))
|
|
|
|
assert not result.errors
|
|
|
|
expected = {
|
|
|
|
"allReporters": {
|
|
|
|
"edges": [
|
|
|
|
{"node": {"firstName": "Jane", "lastName": "Roe"}},
|
|
|
|
{"node": {"firstName": "Some", "lastName": "Lady"}},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
result = schema.execute(query, variable_values=dict(last=4))
|
|
|
|
assert not result.errors
|
|
|
|
expected = {
|
|
|
|
"allReporters": {
|
|
|
|
"edges": [
|
|
|
|
{"node": {"firstName": "John", "lastName": "Doe"}},
|
|
|
|
{"node": {"firstName": "Some", "lastName": "Guy"}},
|
|
|
|
{"node": {"firstName": "Jane", "lastName": "Roe"}},
|
|
|
|
{"node": {"firstName": "Some", "lastName": "Lady"}},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
result = schema.execute(query, variable_values=dict(last=20))
|
|
|
|
assert not result.errors
|
|
|
|
expected = {
|
|
|
|
"allReporters": {
|
|
|
|
"edges": [
|
|
|
|
{"node": {"firstName": "John", "lastName": "Doe"}},
|
|
|
|
{"node": {"firstName": "Some", "lastName": "Guy"}},
|
|
|
|
{"node": {"firstName": "Jane", "lastName": "Roe"}},
|
|
|
|
{"node": {"firstName": "Some", "lastName": "Lady"}},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert result.data == expected
|
2022-09-23 11:45:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_query_nullable_foreign_key():
|
|
|
|
class PetType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Pet
|
|
|
|
|
|
|
|
class PersonType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Person
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
pet = graphene.Field(PetType, name=graphene.String(required=True))
|
|
|
|
person = graphene.Field(PersonType, name=graphene.String(required=True))
|
|
|
|
|
|
|
|
def resolve_pet(self, info, name):
|
|
|
|
return Pet.objects.filter(name=name).first()
|
|
|
|
|
|
|
|
def resolve_person(self, info, name):
|
|
|
|
return Person.objects.filter(name=name).first()
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
|
|
|
|
person = Person.objects.create(name="Jane")
|
|
|
|
pets = [
|
|
|
|
Pet.objects.create(name="Stray dog", age=1),
|
|
|
|
Pet.objects.create(name="Jane's dog", owner=person, age=1),
|
|
|
|
]
|
|
|
|
|
|
|
|
query_pet = """
|
|
|
|
query getPet($name: String!) {
|
|
|
|
pet(name: $name) {
|
|
|
|
owner {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
result = schema.execute(query_pet, variables={"name": "Stray dog"})
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data["pet"] == {
|
|
|
|
"owner": None,
|
|
|
|
}
|
|
|
|
|
|
|
|
result = schema.execute(query_pet, variables={"name": "Jane's dog"})
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data["pet"] == {
|
|
|
|
"owner": {"name": "Jane"},
|
|
|
|
}
|
|
|
|
|
|
|
|
query_owner = """
|
|
|
|
query getOwner($name: String!) {
|
|
|
|
person(name: $name) {
|
|
|
|
pets {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
result = schema.execute(query_owner, variables={"name": "Jane"})
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data["person"] == {
|
|
|
|
"pets": [{"name": "Jane's dog"}],
|
|
|
|
}
|