forked from thinkb4/a-walk-in-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolvers.py
83 lines (54 loc) · 2.26 KB
/
resolvers.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
from typing import List, TypeVar
from ariadne import QueryType, ObjectType
from random import choice
from graphql import GraphQLResolveInfo
from models import Skill, Person
from data import session
from datetime import datetime
query = QueryType()
# Type definition
skill = ObjectType("Skill")
person = ObjectType("Person")
ID = TypeVar("ID", int, str)
# Top level resolvers
@query.field("randomSkill")
def resolve_random_skill(_, info: GraphQLResolveInfo) -> Skill | None:
records = [skill.id for skill in session.query(Skill.id)]
return session.query(Skill).get(choice(records))
@query.field("randomPerson")
def resolve_random_person(_, info: GraphQLResolveInfo) -> Skill | None:
records = [person.id for person in session.query(Person.id)]
return session.query(Person).get(choice(records))
@query.field("persons")
def resolve_persons(_, info: GraphQLResolveInfo, id: ID | None = None):
return session.query(Person).filter_by(id=id) if id else session.query(Person).all()
@query.field("person")
def resolve_person(_, info: GraphQLResolveInfo, id: ID | None = None):
return session.query(Person).get(id)
@query.field("skills")
def resolve_skills(_, info: GraphQLResolveInfo, id: ID | None = None):
return session.query(Skill).filter_by(id=id) if id else session.query(Skill).all()
@query.field("skill")
def resolve_skill(_, info: GraphQLResolveInfo, id: ID | None = None):
return session.query(Skill).get(id)
# Field level resolvers
@skill.field("now")
def resolve_now(_, info: GraphQLResolveInfo) -> datetime:
return datetime.now()
@skill.field("parent")
def resolve_parent(obj, info: GraphQLResolveInfo) -> Skill:
return obj.parent_skill
@person.field("fullName")
def resolve_full_name(obj, info: GraphQLResolveInfo) -> str:
return f"{obj.name} {obj.surname}"
@person.field("friends")
def resolve_friends(obj, info: GraphQLResolveInfo, id: ID | None = None) -> Person:
return obj.friends.filter_by(id=id) if id else obj.friends
@person.field("skills")
def resolve_person_skills(
obj, info: GraphQLResolveInfo, id: ID | None = None
) -> List[Skill]:
return obj.skills.filter_by(id=id) if id else obj.skills
@person.field("favSkill")
def resolve_fav_skill(obj, info: GraphQLResolveInfo) -> Skill:
return obj.person_favSkill