pythonoauthgraphqlgraphql-python

Python GraphQL gql client authentication


I´m having a hard time using GraphQL with Python, because the suggested library (gql) is completely undocumented.

I found out that to provide the API URL, I need to pass an RequestsHTTPTransport object to Client like this:

client = Client(transport=RequestsHTTPTransport(
        url='https://some.api.com/v3/graphql'))

but how to provide credentials like the Bearer Key?

I noticed that RequestsHTTPTransport also accepts an auth param, which is described as:

:param auth: Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth

However, I still can not find out how to create this tuple or callable to work with a Bearer Key. :(


Solution

  • You can add it in the headers.

    reqHeaders = {
        'x-api-key' : API_KEY,
        'Authorization': 'Bearer ' + TOKEN_KEY // This is the key
    }
    
    _transport = RequestsHTTPTransport(
        url=API_ENDPOINT,
        headers = reqHeaders,
        use_json=True,
    )
    
    client = Client(
        transport = _transport,
        fetch_schema_from_transport=True,
    )