graphene/examples/context_example.py

36 lines
698 B
Python
Raw Normal View History

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)
def resolve_me(root, info):
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)
query = """
2016-11-12 06:40:23 +03:00
query something{
me {
id
name
}
}
"""
2016-11-12 06:40:23 +03:00
def test_query():
result = schema.execute(query, context={"user": User(id="1", name="Syrus")})
2016-11-12 06:40:23 +03:00
assert not result.errors
assert result.data == {"me": {"id": "1", "name": "Syrus"}}
2016-11-12 06:40:23 +03:00
if __name__ == "__main__":
result = schema.execute(query, context={"user": User(id="X", name="Console")})
print(result.data["me"])