forked from dvndrsn/graphql-python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthor.py
More file actions
69 lines (49 loc) · 2 KB
/
author.py
File metadata and controls
69 lines (49 loc) · 2 KB
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
from typing import Any, Iterable, Optional, List
import graphene
from promise import Promise
from story import models
class AuthorDisplayNameEnum(graphene.Enum):
FIRST_LAST = models.Author.DISPLAY_FIRST_LAST
LAST_FIRST = models.Author.DISPLAY_LAST_FIRST
class AuthorType(graphene.ObjectType):
class Meta:
interfaces = (graphene.Node, )
first_name = graphene.String()
last_name = graphene.String()
twitter_account = graphene.String()
full_name = graphene.String(
args={
'display': graphene.Argument(
AuthorDisplayNameEnum,
required=True,
default_value=AuthorDisplayNameEnum.FIRST_LAST,
description='Display format to use for Full Name of Author - default FIRST_LAST.'
)
}
)
stories = graphene.ConnectionField('api.query.story.StoryConnection')
@staticmethod
def resolve_full_name(root: models.Author, info: graphene.ResolveInfo, display: str) -> str:
return root.full_name(display)
@staticmethod
def resolve_stories(root: models.Author, info: graphene.ResolveInfo, **kwargs
) -> Promise[List[models.Story]]:
return info.context.loaders.stories_from_author.load(root.id)
@classmethod
def is_type_of(cls, root: Any, info: graphene.ResolveInfo) -> bool:
return isinstance(root, models.Author)
@classmethod
def get_node(cls, info: graphene.ResolveInfo, decoded_id: str
) -> Promise[Optional[models.Author]]:
key = int(decoded_id)
return info.context.loaders.author.load(key)
class AuthorConnection(graphene.Connection):
class Meta:
node = AuthorType
class Query(graphene.ObjectType):
node = graphene.Node.Field()
authors = graphene.ConnectionField(AuthorConnection)
@staticmethod
def resolve_authors(root: None, info: graphene.ResolveInfo, **kwargs
) -> Iterable[models.Author]:
return models.Author.objects.all()