|
5 | 5 |
|
6 | 6 | from graphene import List, NonNull, ObjectType, Schema, String
|
7 | 7 |
|
8 |
| -from ..fields import DjangoListField |
| 8 | +from ..fields import DjangoListField, DjangoInstanceField |
9 | 9 | from ..types import DjangoObjectType
|
10 | 10 | from .models import Article as ArticleModel
|
11 | 11 | from .models import Reporter as ReporterModel
|
@@ -302,6 +302,149 @@ def resolve_reporters(_, info):
|
302 | 302 | assert not result.errors
|
303 | 303 | assert result.data == {"reporters": [{"firstName": "Tara"}]}
|
304 | 304 |
|
| 305 | + def test_get_queryset_filter_instance(self): |
| 306 | + """Resolving prefilter list to get instance""" |
| 307 | + |
| 308 | + class Reporter(DjangoObjectType): |
| 309 | + class Meta: |
| 310 | + model = ReporterModel |
| 311 | + fields = ("first_name", "articles") |
| 312 | + |
| 313 | + @classmethod |
| 314 | + def get_queryset(cls, queryset, info): |
| 315 | + # Only get reporters with at least 1 article |
| 316 | + return queryset.annotate(article_count=Count("articles")).filter( |
| 317 | + article_count__gt=0 |
| 318 | + ) |
| 319 | + |
| 320 | + class Query(ObjectType): |
| 321 | + reporter = DjangoInstanceField( |
| 322 | + Reporter, |
| 323 | + unique_fields=("first_name",), |
| 324 | + first_name=String(required=True), |
| 325 | + ) |
| 326 | + |
| 327 | + schema = Schema(query=Query) |
| 328 | + |
| 329 | + query = """ |
| 330 | + query { |
| 331 | + reporter(firstName: "Tara") { |
| 332 | + firstName |
| 333 | + } |
| 334 | + } |
| 335 | + """ |
| 336 | + |
| 337 | + r1 = ReporterModel.objects.create(first_name="Tara", last_name="West") |
| 338 | + ReporterModel.objects.create(first_name="Debra", last_name="Payne") |
| 339 | + |
| 340 | + ArticleModel.objects.create( |
| 341 | + headline="Amazing news", |
| 342 | + reporter=r1, |
| 343 | + pub_date=datetime.date.today(), |
| 344 | + pub_date_time=datetime.datetime.now(), |
| 345 | + editor=r1, |
| 346 | + ) |
| 347 | + |
| 348 | + result = schema.execute(query) |
| 349 | + |
| 350 | + assert not result.errors |
| 351 | + assert result.data == {"reporter": {"firstName": "Tara"}} |
| 352 | + |
| 353 | + def test_get_queryset_filter_instance_null(self): |
| 354 | + """Resolving prefilter list with no results""" |
| 355 | + |
| 356 | + class Reporter(DjangoObjectType): |
| 357 | + class Meta: |
| 358 | + model = ReporterModel |
| 359 | + fields = ("first_name", "articles") |
| 360 | + |
| 361 | + @classmethod |
| 362 | + def get_queryset(cls, queryset, info): |
| 363 | + # Only get reporters with at least 1 article |
| 364 | + return queryset.annotate(article_count=Count("articles")).filter( |
| 365 | + article_count__gt=0 |
| 366 | + ) |
| 367 | + |
| 368 | + class Query(ObjectType): |
| 369 | + reporter = DjangoInstanceField( |
| 370 | + Reporter, |
| 371 | + unique_fields=("first_name",), |
| 372 | + first_name=String(required=True), |
| 373 | + ) |
| 374 | + |
| 375 | + schema = Schema(query=Query) |
| 376 | + |
| 377 | + query = """ |
| 378 | + query { |
| 379 | + reporter(firstName: "Debra") { |
| 380 | + firstName |
| 381 | + } |
| 382 | + } |
| 383 | + """ |
| 384 | + |
| 385 | + r1 = ReporterModel.objects.create(first_name="Tara", last_name="West") |
| 386 | + ReporterModel.objects.create(first_name="Debra", last_name="Payne") |
| 387 | + |
| 388 | + ArticleModel.objects.create( |
| 389 | + headline="Amazing news", |
| 390 | + reporter=r1, |
| 391 | + pub_date=datetime.date.today(), |
| 392 | + pub_date_time=datetime.datetime.now(), |
| 393 | + editor=r1, |
| 394 | + ) |
| 395 | + |
| 396 | + result = schema.execute(query) |
| 397 | + |
| 398 | + assert not result.errors |
| 399 | + assert result.data == {"reporter": None} |
| 400 | + |
| 401 | + def test_get_queryset_filter_instance_plain(self): |
| 402 | + """Resolving a plain object should work (and not call get_queryset)""" |
| 403 | + |
| 404 | + class Reporter(DjangoObjectType): |
| 405 | + class Meta: |
| 406 | + model = ReporterModel |
| 407 | + fields = ("first_name", "articles") |
| 408 | + |
| 409 | + @classmethod |
| 410 | + def get_queryset(cls, queryset, info): |
| 411 | + # Only get reporters with at least 1 article |
| 412 | + return queryset.annotate(article_count=Count("articles")).filter( |
| 413 | + article_count__gt=0 |
| 414 | + ) |
| 415 | + |
| 416 | + class Query(ObjectType): |
| 417 | + reporter = DjangoInstanceField(Reporter, first_name=String(required=True)) |
| 418 | + |
| 419 | + def resolve_reporter(_, info, first_name): |
| 420 | + return ReporterModel.objects.get(first_name=first_name) |
| 421 | + |
| 422 | + schema = Schema(query=Query) |
| 423 | + |
| 424 | + query = """ |
| 425 | + query { |
| 426 | + reporter(firstName: "Debra") { |
| 427 | + firstName |
| 428 | + } |
| 429 | + } |
| 430 | + """ |
| 431 | + |
| 432 | + r1 = ReporterModel.objects.create(first_name="Tara", last_name="West") |
| 433 | + ReporterModel.objects.create(first_name="Debra", last_name="Payne") |
| 434 | + |
| 435 | + ArticleModel.objects.create( |
| 436 | + headline="Amazing news", |
| 437 | + reporter=r1, |
| 438 | + pub_date=datetime.date.today(), |
| 439 | + pub_date_time=datetime.datetime.now(), |
| 440 | + editor=r1, |
| 441 | + ) |
| 442 | + |
| 443 | + result = schema.execute(query) |
| 444 | + |
| 445 | + assert not result.errors |
| 446 | + assert result.data == {"reporter": {"firstName": "Debra"}} |
| 447 | + |
305 | 448 | def test_resolve_list(self):
|
306 | 449 | """Resolving a plain list should work (and not call get_queryset)"""
|
307 | 450 |
|
|
0 commit comments