2016-11-12 06:40:23 +03:00
|
|
|
import graphene
|
|
|
|
|
|
|
|
|
|
|
|
class User(graphene.ObjectType):
|
|
|
|
id = graphene.ID()
|
|
|
|
name = graphene.String()
|
|
|
|
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
me = graphene.Field(User)
|
|
|
|
|
2020-03-15 21:52:56 +03:00
|
|
|
def resolve_me(root, info):
|
2018-07-06 22:09:23 +03:00
|
|
|
return info.context["user"]
|
2016-11-12 06:40:23 +03:00
|
|
|
|
2017-07-13 07:45:06 +03:00
|
|
|
|
2016-11-12 06:40:23 +03:00
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-06 22:09:23 +03:00
|
|
|
query = """
|
2016-11-12 06:40:23 +03:00
|
|
|
query something{
|
|
|
|
me {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
2018-07-06 22:09:23 +03:00
|
|
|
"""
|
2016-11-12 06:40:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_query():
|
2018-08-29 12:35:44 +03:00
|
|
|
result = schema.execute(query, context={"user": User(id="1", name="Syrus")})
|
2016-11-12 06:40:23 +03:00
|
|
|
assert not result.errors
|
2018-07-06 22:09:23 +03:00
|
|
|
assert result.data == {"me": {"id": "1", "name": "Syrus"}}
|
2016-11-12 06:40:23 +03:00
|
|
|
|
|
|
|
|
2018-07-06 22:09:23 +03:00
|
|
|
if __name__ == "__main__":
|
2018-08-29 12:35:44 +03:00
|
|
|
result = schema.execute(query, context={"user": User(id="X", name="Console")})
|
2018-07-06 22:09:23 +03:00
|
|
|
print(result.data["me"])
|