a few more lesons

This commit is contained in:
Tyrel Souza 2023-12-18 16:42:29 -05:00
parent 867c5164d3
commit 493ea62ca5
2 changed files with 58 additions and 28628 deletions

65
main.py
View File

@ -1,18 +1,67 @@
from graphene import Schema, ObjectType, String
from graphene import Schema, ObjectType, String, Int, Field, List, Mutation
class UserType(ObjectType):
id = Int()
name = String()
age = Int()
class CreateUser(Mutation):
class Arguments:
name = String()
age = Int()
user = Field(UserType)
def mutate(self, info, name, age):
user = {"id": len(Query.users) + 1, "name": name, "age": age}
Query.users.append(user)
return CreateUser(user=user)
class Query(ObjectType):
hello = String(name=String(default_value="world"))
user = Field(UserType, user_id=Int())
users_by_min_age = List(UserType, min_age=Int())
def resolve_hello(self, info, name):
return "Hello " + name
users = [
{"id": 1, "name": "Tyrel Souza", "age": 35},
{"id": 2, "name": "Lauren Feldman", "age": 38},
{"id": 3, "name": "Astrid Feldman", "age": 0},
]
@staticmethod
def resolve_user(root, info, user_id):
matched_users = [user for user in Query.users if user["id"] == user_id]
return matched_users[0] if matched_users else None
@staticmethod
def resolve_users_by_min_age(root, info, min_age):
return [user for user in Query.users if user["age"] >= min_age]
schema = Schema(query=Query)
class Mut(ObjectType):
create_user = CreateUser.Field()
schema = Schema(query=Query, mutation=Mut)
gql = """
{
hello(name: "ghasdf")
query {
user(userId: 4) {
id
name
age
}
}
"""
gql2 = """
mutation{
createUser(name: "Jill", age: 61) {
user {
id
name
age
}
}
}
"""
@ -20,3 +69,5 @@ gql = """
if __name__ == "__main__":
res = schema.execute(gql)
print(res)
res = schema.execute(gql2)
print(res)

28621
tags

File diff suppressed because one or more lines are too long