Skip to content

Commit b7d8150

Browse files
authored
feat(transport/requests): RequestsHTTPTransport execute func add extra_args param (#232)
1 parent 20ae2e2 commit b7d8150

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

gql/transport/requests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def execute( # type: ignore
103103
variable_values: Optional[Dict[str, Any]] = None,
104104
operation_name: Optional[str] = None,
105105
timeout: Optional[int] = None,
106+
extra_args: Dict[str, Any] = None,
106107
) -> ExecutionResult:
107108
"""Execute GraphQL query.
108109
@@ -114,6 +115,7 @@ def execute( # type: ignore
114115
:param operation_name: Name of the operation that shall be executed.
115116
Only required in multi-operation documents (Default: None).
116117
:param timeout: Specifies a default timeout for requests (Default: None).
118+
:param extra_args: additional arguments to send to the requests post method
117119
:return: The result of execution.
118120
`data` is the result of executing the query, `errors` is null
119121
if no errors occurred, and is a non-empty array if an error occurred.
@@ -146,6 +148,10 @@ def execute( # type: ignore
146148
# Pass kwargs to requests post method
147149
post_args.update(self.kwargs)
148150

151+
# Pass post_args to requests post method
152+
if extra_args:
153+
post_args.update(extra_args)
154+
149155
# Using the created session to perform requests
150156
response = self.session.request(
151157
self.method, self.url, **post_args # type: ignore

tests/fixtures/vcr_cassettes/queries.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,48 @@ interactions:
338338
status:
339339
code: 200
340340
message: OK
341+
- request:
342+
body: '{"query": "query Planet($id: ID!) {\n planet(id: $id) {\n id\n name\n }\n}\n"}'
343+
headers:
344+
Accept:
345+
- '*/*'
346+
Accept-Encoding:
347+
- gzip, deflate
348+
Connection:
349+
- keep-alive
350+
Content-Length:
351+
- '86'
352+
Content-Type:
353+
- application/json
354+
Cookie:
355+
- csrftoken=kAyQyUjNOGXZfkKUtWtvUROaFfDe2GBiV7yIRsqs3r2j9aYchRDXTNo3lHp72h5k;
356+
csrftoken=kAyQyUjNOGXZfkKUtWtvUROaFfDe2GBiV7yIRsqs3r2j9aYchRDXTNo3lHp72h5k
357+
User-Agent:
358+
- python-requests/2.26.0
359+
authorization:
360+
- xxx-123
361+
method: POST
362+
uri: http://127.0.0.1:8000/graphql
363+
response:
364+
body:
365+
string: '{"data":{"planet":{"id":"UGxhbmV0OjEx","name":"Geonosis"}}}'
366+
headers:
367+
Content-Length:
368+
- '59'
369+
Content-Type:
370+
- application/json
371+
Date:
372+
- Fri, 06 Nov 2020 11:30:21 GMT
373+
Server:
374+
- WSGIServer/0.1 Python/2.7.18
375+
Set-Cookie:
376+
- csrftoken=kAyQyUjNOGXZfkKUtWtvUROaFfDe2GBiV7yIRsqs3r2j9aYchRDXTNo3lHp72h5k;
377+
expires=Fri, 05-Nov-2021 11:30:21 GMT; Max-Age=31449600; Path=/
378+
Vary:
379+
- Cookie
380+
X-Frame-Options:
381+
- SAMEORIGIN
382+
status:
383+
code: 200
384+
message: OK
341385
version: 1

tests/test_transport.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,22 @@ def test_named_query(client):
123123
with use_cassette("queries"):
124124
result = client.execute(query, operation_name="Planet2")
125125
assert result == expected
126+
127+
128+
def test_header_query(client):
129+
query = gql(
130+
"""
131+
query Planet($id: ID!) {
132+
planet(id: $id) {
133+
id
134+
name
135+
}
136+
}
137+
"""
138+
)
139+
expected = {"planet": {"id": "UGxhbmV0OjEx", "name": "Geonosis"}}
140+
with use_cassette("queries"):
141+
result = client.execute(
142+
query, extra_args={"headers": {"authorization": "xxx-123"}}
143+
)
144+
assert result == expected

0 commit comments

Comments
 (0)