mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-25 11:03:58 +03:00
Added context example
This commit is contained in:
parent
6c7cd4e4fa
commit
b1bffc4f8d
39
examples/context_example.py
Normal file
39
examples/context_example.py
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
import graphene
|
||||||
|
|
||||||
|
|
||||||
|
class User(graphene.ObjectType):
|
||||||
|
id = graphene.ID()
|
||||||
|
name = graphene.String()
|
||||||
|
|
||||||
|
|
||||||
|
class Query(graphene.ObjectType):
|
||||||
|
me = graphene.Field(User)
|
||||||
|
|
||||||
|
def resolve_me(self, args, context, info):
|
||||||
|
return context['user']
|
||||||
|
|
||||||
|
schema = graphene.Schema(query=Query)
|
||||||
|
query = '''
|
||||||
|
query something{
|
||||||
|
me {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def test_query():
|
||||||
|
result = schema.execute(query, context_value={'user': User(id='1', name='Syrus')})
|
||||||
|
assert not result.errors
|
||||||
|
assert result.data == {
|
||||||
|
'me': {
|
||||||
|
'id': '1',
|
||||||
|
'name': 'Syrus',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
result = schema.execute(query, context_value={'user': User(id='X', name='Console')})
|
||||||
|
print(result.data['me'])
|
Loading…
Reference in New Issue
Block a user