a few more lesons
This commit is contained in:
parent
867c5164d3
commit
493ea62ca5
65
main.py
65
main.py
@ -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):
|
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):
|
users = [
|
||||||
return "Hello " + name
|
{"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 = """
|
gql = """
|
||||||
{
|
query {
|
||||||
hello(name: "ghasdf")
|
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__":
|
if __name__ == "__main__":
|
||||||
res = schema.execute(gql)
|
res = schema.execute(gql)
|
||||||
print(res)
|
print(res)
|
||||||
|
res = schema.execute(gql2)
|
||||||
|
print(res)
|
||||||
|
Loading…
Reference in New Issue
Block a user