Graphene: Add Authorization header to Graphene Client

Created on 12 Jun 2018  路  9Comments  路  Source: graphql-python/graphene

Is there an easy way to add an authorization header to the graphene.test Client command? I'm doing unit tests, and I need to be able to send commands while logged in. If not, can you provide guidance on the best way to do this?

All 9 comments

@benwis when using the test client you can pass context_value to the execute call: http://docs.graphene-python.org/en/latest/testing/#execute-parameters

That allows you to pass authorization headers to the query execution.

@kawasakikou I'm not sure that what that error is. Can you provide a reproducible example?

@jkimbo can you express your opinion on this issue also #942.

@benwis when using the test client you can pass context_value to the execute call: http://docs.graphene-python.org/en/latest/testing/#execute-parameters

Hi, can you give me example how to pass headers into graphene client, i can not find any document about it. Thanks.

@thanhpd-teko Graphene does not have a concept of HTTP headers because it is not a web framework but a way of executing graphql queries. If you are using something like graphene-django then the info.context value in your resolvers will be the Django request object. So in your tests you can replicate that by passing a mock request object to the context param. You can create a mock request object using the Django request factory.

Hi there...new to this community. Are you saying that if using this library as a graphql client, it does not support passing http headers to the server? Sorry, I'm a bit confused. Thanks.

@jritsema this library is not a graphql client, it's a graphql server only. The above discussion was explaining how to pass headers in the Graphene TestClient which is a helper that can be used to test your graphene server not actually make graphql api calls.

If you need a graphql client in python you could try using: https://github.com/graphql-python/gql

Thanks for the explanation @jkimbo!

Hey, I was having problems with this issue too. I just want to post my solution so anyone having the same problem will find a way to solve it. I didn't know that I had to send a request inside the context. The documentation was not clear about this.

from flask import request

token = '......'
request.headers = {'Authorization': token}
client.execute(query, context_value=request)
Was this page helpful?
0 / 5 - 0 ratings