-
Notifications
You must be signed in to change notification settings - Fork 61
/
apis.py
87 lines (66 loc) · 2.19 KB
/
apis.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
import logging
import uuid
from typing import Dict # noqa
import graphene
from graphene import ObjectType, relay
from .services import BookService
logger = logging.getLogger(__name__)
# Internal API
class BookAPI:
@staticmethod
def get(*, book_id: uuid.UUID) -> Dict:
logger.info('method "get" called')
return BookService.get_book(id=book_id)
# graphQL Entity
class BookEntity(ObjectType):
class Meta:
interfaces = (relay.Node,)
name = graphene.String()
author_name = graphene.String()
# graphQL mutation
class CreateBook(relay.ClientIDMutation):
class Input:
book_name = graphene.String(required=True)
author_id = graphene.ID(required=True)
book = graphene.Field(BookEntity)
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
new_book = BookService.create_book(
name=input.get('book_name'),
author_id=input.get('author_id')
)
return BookEntity(
name=new_book['name'],
author_name=new_book['author_name'],
)
# graphQL mutation
class UpdateBookAndAuthorName(relay.ClientIDMutation):
class Input:
book_name = graphene.String(required=True)
author_name = graphene.String(required=True)
author_id = graphene.ID(required=True)
book_id = graphene.ID(required=True)
book = graphene.Field(BookEntity)
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
book = BookService.update_book_name_and_author_name(
name=input.get('book_name'),
id=input.get('book_id'),
author_id=input.get('author_id'),
author_name=input.get('author_name'),
)
return BookEntity(
name=book['name'],
author_name=book['author_name'],
)
class Mutation:
create_book = CreateBook.Field()
class Query:
book = graphene.Field(BookEntity)
def resolve_book(self, info):
logger.info(f"graphQL query for book with id {info.get('id')}")
book = BookService.get_book(id=info.get('id'))
return BookEntity(
name=book['name'],
author_name=book['author_name'],
)