py-microservice/schema.py
2023-10-04 21:08:37 +02:00

82 lines
1.7 KiB
Python

"""
GraphQL schema definitions
"""
# stdlib imports
from random import randint
from datetime import datetime
import typing
import logging
# dependencies imports
import strawberry
# app imports
import consts
import auth
logger = logging.getLogger(consts.LOG_ROOT)
@strawberry.type
class Pizza:
"""
Pizza
"""
name: str
toppings: typing.List[str]
@strawberry.type
class OrderInfo:
"""
Information about a pizza order
"""
id: str
deliveryTime: str
bakerName: str
pizza: Pizza
@strawberry.type
class Mutation:
"""
Operation to order a pizza
"""
@strawberry.mutation
def order_pizza(self, name: str, toppings: typing.List[str], info: auth.Info) -> OrderInfo:
"""
Order a pizza with a name and list of toppings
"""
logger.debug("orderPizza name=%s toppings=%s", name, toppings)
token = info.context.token
has_permission = auth.validate_permissions(token, "orderPizza")
if not has_permission:
raise auth.SecurityException("Permission denied")
return OrderInfo(
id=str(randint(1, 100000)),
deliveryTime=str(datetime.now()),
bakerName="Domino's",
pizza=Pizza(name=name, toppings=toppings)
)
def empty():
"""
empty resolver for stub query (see below)
"""
@strawberry.type
class Query:
"""
`strawberry.Schema` requires at least one `Query`, so configure a stub
see https://strawberry.rocks/docs/general/mutations
"""
stub: str = strawberry.field(resolver=empty)
# query= is required: https://strawberry.rocks/docs/general/mutations
schema = strawberry.Schema(query=Query, mutation=Mutation)