-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathschema.py
93 lines (79 loc) · 2.3 KB
/
schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import strawberry
import strawberry_django
from strawberry_django import auto, auth
from . import models
from typing import List, Optional
from datetime import date
from django.contrib.auth import get_user_model
@strawberry.django.type(get_user_model())
class UserType:
id: auto
username: auto
first_name: auto
last_name: auto
is_superuser: auto
is_staff: auto
email: auto
is_active: auto
profile: auto
@strawberry.django.type(models.Profile)
class AuthorType:
id: auto
user: 'UserType'
website: auto
bio: auto
post_set: List['PostType']
@strawberry.django.type(models.Tag)
class TagType:
id: auto
name: auto
@strawberry.django.type(models.Post)
class PostType:
id: auto
title: auto
subtitle: auto
slug: auto
body: auto
meta_description: auto
date_created: auto
date_modified: auto
publish_date: auto
published: auto
author: 'AuthorType'
tags: List['TagType']
def get_all_posts(root, info):
return(
models.Post.objects.prefetch_related('tags')
.select_related('author')
.all()
)
def author_by_username(root, info, username: str):
return models.Profile.objects.select_related('user').get(
user__username=username
)
def post_by_slug(root, info, slug: str):
return (
models.Post.objects.prefetch_related('tags')
.select_related('author')
.get(slug=slug)
)
def posts_by_author(root, info, username: str):
return (
models.Post.objects.prefetch_related('tags')
.select_related("author")
.filter(author__user__username=username)
)
def posts_by_tag(root, info, tag: str):
return (
models.Post.objects.prefetch_related('tags')
.select_related('author')
.filter(tags__name__iexact=tag)
)
@strawberry.type
class Query:
all_posts: List[PostType] = strawberry.django.field(resolver=get_all_posts)
author_by_username: Optional[AuthorType] = strawberry.django.field(resolver=author_by_username)
post_by_slug: 'PostType' = strawberry.django.field(resolver=post_by_slug)
posts_by_author: List[PostType] = strawberry.django.field(resolver=posts_by_author)
posts_by_tag: List[PostType] = strawberry.django.field(resolver=posts_by_tag)
schema = strawberry.Schema(query=Query)