-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathschema_graphene.py
65 lines (51 loc) · 1.81 KB
/
schema_graphene.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
from django.contrib.auth import get_user_model
from graphene_django import DjangoObjectType
from blog import models
import graphene
class UserType(DjangoObjectType):
class Meta:
model = get_user_model()
class AuthorType(DjangoObjectType):
class Meta:
model = models.Profile
class PostType(DjangoObjectType):
class Meta:
model = models.Post
class TagType(DjangoObjectType):
class Meta:
model = models.Tag
class Query(graphene.ObjectType):
all_posts = graphene.List(PostType)
author_by_username = graphene.Field(AuthorType, username=graphene.String())
post_by_slug = graphene.Field(PostType, slug=graphene.String())
posts_by_author = graphene.List(PostType, username=graphene.String())
posts_by_tag = graphene.List(PostType, tag=graphene.String())
def resolve_all_posts(root, info):
return (
models.Post.objects.prefetch_related("tags")
.select_related("author")
.all()
)
def resolve_author_by_username(root, info, username):
return models.Profile.objects.select_related("user").get(
user__username=username
)
def resolve_post_by_slug(root, info, slug):
return (
models.Post.objects.prefetch_related("tags")
.select_related("author")
.get(slug=slug)
)
def resolve_posts_by_author(root, info, username):
return (
models.Post.objects.prefetch_related("tags")
.select_related("author")
.filter(author__user__username=username)
)
def resolve_posts_by_tag(root, info, tag):
return (
models.Post.objects.prefetch_related("tags")
.select_related("author")
.filter(tags__name__iexact=tag)
)
schema = graphene.Schema(query=Query)