forked from OCA/rest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.py
101 lines (76 loc) · 2.77 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
94
95
96
97
98
99
100
101
# Copyright 2018 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
# disable undefined variable error, which erroneously triggers
# on forward declarations of classes in lambdas
# pylint: disable=E0602
import graphene
from odoo import _
from odoo.exceptions import UserError
from odoo.addons.graphql_base import OdooObjectType
class Country(OdooObjectType):
code = graphene.String(required=True)
name = graphene.String(required=True)
class Partner(OdooObjectType):
name = graphene.String(required=True)
street = graphene.String()
street2 = graphene.String()
city = graphene.String()
zip = graphene.String()
country = graphene.Field(Country)
email = graphene.String()
phone = graphene.String()
is_company = graphene.Boolean(required=True)
contacts = graphene.List(graphene.NonNull(lambda: Partner), required=True)
@staticmethod
def resolve_country(root, info):
return root.country_id or None
@staticmethod
def resolve_contacts(root, info):
return root.child_ids
class Query(graphene.ObjectType):
all_partners = graphene.List(
graphene.NonNull(Partner),
required=True,
companies_only=graphene.Boolean(),
limit=graphene.Int(),
offset=graphene.Int(),
)
reverse = graphene.String(
required=True,
description="Reverse a string",
word=graphene.String(required=True),
)
error_example = graphene.String()
@staticmethod
def resolve_all_partners(root, info, companies_only=False, limit=None, offset=None):
domain = []
if companies_only:
domain.append(("is_company", "=", True))
return info.context["env"]["res.partner"].search(
domain, limit=limit, offset=offset
)
@staticmethod
def resolve_reverse(root, info, word):
return word[::-1]
@staticmethod
def resolve_error_example(root, info):
raise UserError(_("UserError example"))
class CreatePartner(graphene.Mutation):
class Arguments:
name = graphene.String(required=True)
email = graphene.String(required=True)
is_company = graphene.Boolean()
raise_after_create = graphene.Boolean()
Output = Partner
@staticmethod
def mutate(self, info, name, email, is_company=False, raise_after_create=False):
env = info.context["env"]
partner = env["res.partner"].create(
{"name": name, "email": email, "is_company": is_company}
)
if raise_after_create:
raise UserError(_("as requested"))
return partner
class Mutation(graphene.ObjectType):
create_partner = CreatePartner.Field(description="Documentation of CreatePartner")
schema = graphene.Schema(query=Query, mutation=Mutation)