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),
|
weight=randint(0, first),
|
||||||
name=choice(ascii_letters),
|
name=choice(ascii_letters),
|
||||||
)
|
)
|
||||||
] * first
|
for _ in range(first)
|
||||||
|
]
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ from user.models.user import User
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Review:
|
class Review:
|
||||||
id: str
|
id: int
|
||||||
body: str
|
body: str
|
||||||
author: User
|
author: User
|
||||||
product: Product
|
product: Product
|
||||||
|
|
|
||||||
|
|
@ -19,4 +19,5 @@ def resolve_reviews(*_, first: int):
|
||||||
author=User(),
|
author=User(),
|
||||||
product=Product(upc="id", name="product", price=10, weight=12),
|
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 uuid import uuid4
|
||||||
|
|
||||||
from pydantic import BaseModel, validator
|
from pydantic import BaseModel, Field, validator
|
||||||
|
|
||||||
|
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
id: str = str(uuid4())
|
id: str = Field(default_factory=uuid4)
|
||||||
name: str = "John"
|
name: str = "John"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -18,20 +19,23 @@ FORBIDDEN_NAMES = ["joe dalton"]
|
||||||
class UserInput(BaseModel):
|
class UserInput(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
anystr_strip_whitespace = True
|
||||||
|
|
||||||
@validator("name")
|
@validator("name")
|
||||||
def name_must_contain_space(cls, name: str) -> str:
|
def name_must_contain_space(cls, name: str) -> str:
|
||||||
if " " not in name.strip():
|
if " " not in name:
|
||||||
raise ValueError("must contain a space")
|
raise ValueError("must contain a space")
|
||||||
return name.title()
|
return name.title()
|
||||||
|
|
||||||
@validator("name")
|
@validator("name")
|
||||||
def name_must_not_be(cls, name: str):
|
def name_must_not_be(cls, name: str) -> str:
|
||||||
if name.strip() in FORBIDDEN_NAMES:
|
if name in FORBIDDEN_NAMES:
|
||||||
raise ValueError("This name is forbidden")
|
raise ValueError("This name is forbidden")
|
||||||
return name
|
return name
|
||||||
|
|
||||||
|
|
||||||
class UserResponse(BaseModel):
|
class UserResponse(BaseModel):
|
||||||
success: bool = True
|
success: bool = True
|
||||||
errorMessage: str = None
|
errorMessage: Optional[str] = None
|
||||||
user: User = None
|
user: Optional[User] = None
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
type Mutation {
|
type Mutation {
|
||||||
"Create a user"
|
"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
|
from user.models.user import User, UserResponse, UserInput
|
||||||
|
|
||||||
|
|
@ -15,9 +15,10 @@ def create_user(user_input: UserInput) -> User:
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("createUser")
|
@mutation.field("createUser")
|
||||||
def resolve_create_user(*_, input: dict) -> UserResponse:
|
@convert_kwargs_to_snake_case
|
||||||
|
def resolve_create_user(*_, user_data: dict) -> UserResponse:
|
||||||
try:
|
try:
|
||||||
user_input = UserInput(name=input.get("name"))
|
user_input = UserInput(name=user_data["name"])
|
||||||
user = create_user(user_input)
|
user = create_user(user_input)
|
||||||
return UserResponse(user=user)
|
return UserResponse(user=user)
|
||||||
except ValueError as error:
|
except ValueError as error:
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
type Query {
|
type Query {
|
||||||
"Returns a user by its name"
|
"Returns a user by its name"
|
||||||
user(name: String): User
|
user(name: String!): User
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue