Skip to content

Commit c72bd71

Browse files
magicmarkclaude
andcommitted
Add transport integration tests for request extensions
Verify that extensions set on GraphQLRequest are sent in the HTTP request body for aiohttp, httpx, and requests transports. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 00fc3f5 commit c72bd71

3 files changed

Lines changed: 131 additions & 3 deletions

File tree

tests/test_aiohttp.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88

9-
from gql import Client, FileVar, gql
9+
from gql import Client, FileVar, GraphQLRequest, gql
1010
from gql.cli import get_parser, main
1111
from gql.transport.exceptions import (
1212
TransportAlreadyConnected,
@@ -87,6 +87,46 @@ async def handler(request):
8787
assert transport.response_headers["dummy"] == "test1234"
8888

8989

90+
@pytest.mark.asyncio
91+
async def test_aiohttp_query_with_extensions(aiohttp_server):
92+
from aiohttp import web
93+
94+
from gql.transport.aiohttp import AIOHTTPTransport
95+
96+
async def handler(request):
97+
body = await request.json()
98+
assert "extensions" in body
99+
assert body["extensions"] == {
100+
"persistedQuery": {"version": 1, "sha256Hash": "abc123"}
101+
}
102+
return web.Response(
103+
text=query1_server_answer,
104+
content_type="application/json",
105+
)
106+
107+
app = web.Application()
108+
app.router.add_route("POST", "/", handler)
109+
server = await aiohttp_server(app)
110+
111+
url = server.make_url("/")
112+
113+
transport = AIOHTTPTransport(url=url, timeout=10)
114+
115+
async with Client(transport=transport) as session:
116+
117+
request = GraphQLRequest(
118+
query1_str,
119+
extensions={
120+
"persistedQuery": {"version": 1, "sha256Hash": "abc123"}
121+
},
122+
)
123+
124+
result = await session.execute(request)
125+
126+
continents = result["continents"]
127+
assert continents[0]["code"] == "AF"
128+
129+
90130
@pytest.mark.asyncio
91131
async def test_aiohttp_ignore_backend_content_type(aiohttp_server):
92132
from aiohttp import web

tests/test_httpx.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from gql import Client, FileVar, gql
6+
from gql import Client, FileVar, GraphQLRequest, gql
77
from gql.transport.exceptions import (
88
TransportAlreadyConnected,
99
TransportClosed,
@@ -84,6 +84,50 @@ def test_code():
8484
await run_sync_test(server, test_code)
8585

8686

87+
@pytest.mark.aiohttp
88+
@pytest.mark.asyncio
89+
async def test_httpx_query_with_extensions(aiohttp_server, run_sync_test):
90+
from aiohttp import web
91+
92+
from gql.transport.httpx import HTTPXTransport
93+
94+
async def handler(request):
95+
body = await request.json()
96+
assert "extensions" in body
97+
assert body["extensions"] == {
98+
"persistedQuery": {"version": 1, "sha256Hash": "abc123"}
99+
}
100+
return web.Response(
101+
text=query1_server_answer,
102+
content_type="application/json",
103+
)
104+
105+
app = web.Application()
106+
app.router.add_route("POST", "/", handler)
107+
server = await aiohttp_server(app)
108+
109+
url = str(server.make_url("/"))
110+
111+
def test_code():
112+
transport = HTTPXTransport(url=url)
113+
114+
with Client(transport=transport) as session:
115+
116+
request = GraphQLRequest(
117+
query1_str,
118+
extensions={
119+
"persistedQuery": {"version": 1, "sha256Hash": "abc123"}
120+
},
121+
)
122+
123+
result = session.execute(request)
124+
125+
continents = result["continents"]
126+
assert continents[0]["code"] == "AF"
127+
128+
await run_sync_test(server, test_code)
129+
130+
87131
@pytest.mark.aiohttp
88132
@pytest.mark.asyncio
89133
@pytest.mark.parametrize("verify_https", ["disabled", "cert_provided"])

tests/test_requests.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from gql import Client, FileVar, gql
7+
from gql import Client, FileVar, GraphQLRequest, gql
88
from gql.transport.exceptions import (
99
TransportAlreadyConnected,
1010
TransportClosed,
@@ -85,6 +85,50 @@ def test_code():
8585
await run_sync_test(server, test_code)
8686

8787

88+
@pytest.mark.aiohttp
89+
@pytest.mark.asyncio
90+
async def test_requests_query_with_extensions(aiohttp_server, run_sync_test):
91+
from aiohttp import web
92+
93+
from gql.transport.requests import RequestsHTTPTransport
94+
95+
async def handler(request):
96+
body = await request.json()
97+
assert "extensions" in body
98+
assert body["extensions"] == {
99+
"persistedQuery": {"version": 1, "sha256Hash": "abc123"}
100+
}
101+
return web.Response(
102+
text=query1_server_answer,
103+
content_type="application/json",
104+
)
105+
106+
app = web.Application()
107+
app.router.add_route("POST", "/", handler)
108+
server = await aiohttp_server(app)
109+
110+
url = server.make_url("/")
111+
112+
def test_code():
113+
transport = RequestsHTTPTransport(url=url)
114+
115+
with Client(transport=transport) as session:
116+
117+
request = GraphQLRequest(
118+
query1_str,
119+
extensions={
120+
"persistedQuery": {"version": 1, "sha256Hash": "abc123"}
121+
},
122+
)
123+
124+
result = session.execute(request)
125+
126+
continents = result["continents"]
127+
assert continents[0]["code"] == "AF"
128+
129+
await run_sync_test(server, test_code)
130+
131+
88132
@pytest.mark.aiohttp
89133
@pytest.mark.asyncio
90134
@pytest.mark.parametrize("verify_https", ["disabled", "cert_provided"])

0 commit comments

Comments
 (0)