mirror of
https://github.com/rjNemo/federation
synced 2026-06-06 02:06:39 +00:00
fix: typing
This commit is contained in:
parent
7879312837
commit
5b695e485e
9 changed files with 24 additions and 17 deletions
|
|
@ -17,4 +17,5 @@ def resolve_top_products(*_, first: int):
|
|||
weight=randint(0, first),
|
||||
name=choice(ascii_letters),
|
||||
)
|
||||
] * first
|
||||
for _ in range(first)
|
||||
]
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from user.models.user import User
|
|||
|
||||
@dataclass
|
||||
class Review:
|
||||
id: str
|
||||
id: int
|
||||
body: str
|
||||
author: User
|
||||
product: Product
|
||||
|
|
|
|||
|
|
@ -19,4 +19,5 @@ def resolve_reviews(*_, first: int):
|
|||
author=User(),
|
||||
product=Product(upc="id", name="product", price=10, weight=12),
|
||||
)
|
||||
] * first
|
||||
for _ in range(first)
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
from typing import Optional
|
||||
from uuid import uuid4
|
||||
|
||||
from pydantic import BaseModel, validator
|
||||
from pydantic import BaseModel, Field, validator
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
id: str = str(uuid4())
|
||||
id: str = Field(default_factory=uuid4)
|
||||
name: str = "John"
|
||||
|
||||
@property
|
||||
|
|
@ -18,20 +19,23 @@ FORBIDDEN_NAMES = ["joe dalton"]
|
|||
class UserInput(BaseModel):
|
||||
name: str
|
||||
|
||||
class Config:
|
||||
anystr_strip_whitespace = True
|
||||
|
||||
@validator("name")
|
||||
def name_must_contain_space(cls, name: str) -> str:
|
||||
if " " not in name.strip():
|
||||
if " " not in name:
|
||||
raise ValueError("must contain a space")
|
||||
return name.title()
|
||||
|
||||
@validator("name")
|
||||
def name_must_not_be(cls, name: str):
|
||||
if name.strip() in FORBIDDEN_NAMES:
|
||||
def name_must_not_be(cls, name: str) -> str:
|
||||
if name in FORBIDDEN_NAMES:
|
||||
raise ValueError("This name is forbidden")
|
||||
return name
|
||||
|
||||
|
||||
class UserResponse(BaseModel):
|
||||
success: bool = True
|
||||
errorMessage: str = None
|
||||
user: User = None
|
||||
errorMessage: Optional[str] = None
|
||||
user: Optional[User] = None
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
type Mutation {
|
||||
"Create a user"
|
||||
createUser(input: UserInput!): UserResponse
|
||||
createUser(userData: UserInput!): UserResponse
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
from ariadne import MutationType
|
||||
from ariadne import MutationType, convert_kwargs_to_snake_case
|
||||
|
||||
from user.models.user import User, UserResponse, UserInput
|
||||
|
||||
|
|
@ -15,9 +15,10 @@ def create_user(user_input: UserInput) -> User:
|
|||
|
||||
|
||||
@mutation.field("createUser")
|
||||
def resolve_create_user(*_, input: dict) -> UserResponse:
|
||||
@convert_kwargs_to_snake_case
|
||||
def resolve_create_user(*_, user_data: dict) -> UserResponse:
|
||||
try:
|
||||
user_input = UserInput(name=input.get("name"))
|
||||
user_input = UserInput(name=user_data["name"])
|
||||
user = create_user(user_input)
|
||||
return UserResponse(user=user)
|
||||
except ValueError as error:
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
type Query {
|
||||
"Returns a user by its name"
|
||||
user(name: String): User
|
||||
user(name: String!): User
|
||||
}
|
||||
Loading…
Reference in a new issue