Skip to content

Commit 673cf2a

Browse files
committed
Make all http spec tests pass 🎉
1 parent 6710585 commit 673cf2a

File tree

14 files changed

+85
-29
lines changed

14 files changed

+85
-29
lines changed

src/graphql_server/aiohttp/views.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,17 @@ async def get_context(
220220
return {"request": request, "response": response} # type: ignore
221221

222222
def create_response(
223-
self, response_data: GraphQLHTTPResponse, sub_response: web.Response
223+
self,
224+
response_data: GraphQLHTTPResponse,
225+
sub_response: web.Response,
226+
is_strict: bool,
224227
) -> web.Response:
225228
status_code = getattr(sub_response, "status_code", None)
226229
return web.Response(
227230
text=self.encode_json(response_data),
228-
content_type="application/json",
231+
content_type="application/graphql-response+json"
232+
if is_strict
233+
else "application/json",
229234
headers=sub_response.headers,
230235
status=status_code or sub_response.status,
231236
)

src/graphql_server/asgi/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,18 @@ async def render_graphql_ide(
211211
return HTMLResponse(request_data.to_template_string(self.graphql_ide_html))
212212

213213
def create_response(
214-
self, response_data: GraphQLHTTPResponse, sub_response: Response
214+
self,
215+
response_data: GraphQLHTTPResponse,
216+
sub_response: Response,
217+
is_strict: bool,
215218
) -> Response:
216219
response = Response(
217220
self.encode_json(response_data),
218221
status_code=sub_response.status_code or status.HTTP_200_OK,
219222
headers=sub_response.headers,
220-
media_type="application/json",
223+
media_type="application/graphql-response+json"
224+
if is_strict
225+
else "application/json",
221226
)
222227

223228
if sub_response.background:

src/graphql_server/chalice/views.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,18 @@ def get_context(self, request: Request, response: TemporalResponse) -> Context:
118118
return {"request": request, "response": response} # type: ignore
119119

120120
def create_response(
121-
self, response_data: GraphQLHTTPResponse, sub_response: TemporalResponse
121+
self,
122+
response_data: GraphQLHTTPResponse,
123+
sub_response: TemporalResponse,
124+
is_strict: bool,
122125
) -> Response:
123126
return Response(
124127
body=self.encode_json(response_data),
125128
status_code=sub_response.status_code,
126129
headers={
127-
"Content-Type": "application/json",
130+
"Content-Type": "application/graphql-response+json"
131+
if is_strict
132+
else "application/json",
128133
**sub_response.headers,
129134
},
130135
)

src/graphql_server/channels/handlers/http_handler.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,17 @@ def __init__(
191191
super().__init__(**kwargs)
192192

193193
def create_response(
194-
self, response_data: GraphQLHTTPResponse, sub_response: TemporalResponse
194+
self,
195+
response_data: GraphQLHTTPResponse,
196+
sub_response: TemporalResponse,
197+
is_strict: bool,
195198
) -> ChannelsResponse:
196199
return ChannelsResponse(
197200
content=json.dumps(response_data).encode(),
198201
status=sub_response.status_code,
202+
content_type="application/graphql-response+json"
203+
if is_strict
204+
else "application/json",
199205
headers={k.encode(): v.encode() for k, v in sub_response.headers.items()},
200206
)
201207

src/graphql_server/channels/handlers/ws_handler.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ async def get_sub_response(self, request: GraphQLWSConsumer) -> GraphQLWSConsume
172172
raise NotImplementedError
173173

174174
def create_response(
175-
self, response_data: GraphQLHTTPResponse, sub_response: GraphQLWSConsumer
175+
self,
176+
response_data: GraphQLHTTPResponse,
177+
sub_response: GraphQLWSConsumer,
178+
is_strict: bool,
176179
) -> GraphQLWSConsumer:
177180
raise NotImplementedError
178181

src/graphql_server/django/views.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,14 @@ def __init__(
167167
super().__init__(**kwargs)
168168

169169
def create_response(
170-
self, response_data: GraphQLHTTPResponse, sub_response: HttpResponse
170+
self,
171+
response_data: GraphQLHTTPResponse,
172+
sub_response: HttpResponse,
173+
is_strict: bool,
171174
) -> HttpResponseBase:
172175
data = self.encode_json(response_data)
173176
response = HttpResponse(
174177
data,
175-
content_type="application/json",
176178
status=sub_response.status_code,
177179
)
178180

@@ -182,6 +184,10 @@ def create_response(
182184
if sub_response.status_code:
183185
response.status_code = sub_response.status_code
184186

187+
response.headers["content-type"] = (
188+
"application/graphql-response+json" if is_strict else "application/json"
189+
)
190+
185191
for name, value in sub_response.cookies.items():
186192
response.cookies[name] = value
187193

src/graphql_server/fastapi/router.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,16 @@ async def get_sub_response(self, request: Request) -> Response:
288288
return self.temporal_response
289289

290290
def create_response(
291-
self, response_data: GraphQLHTTPResponse, sub_response: Response
291+
self,
292+
response_data: GraphQLHTTPResponse,
293+
sub_response: Response,
294+
is_strict: bool,
292295
) -> Response:
293296
response = Response(
294297
self.encode_json(response_data),
295-
media_type="application/json",
298+
media_type="application/graphql-response+json"
299+
if is_strict
300+
else "application/json",
296301
status_code=sub_response.status_code or status.HTTP_200_OK,
297302
)
298303

src/graphql_server/flask/views.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,15 @@ def __init__(
9696
self.graphql_ide = graphql_ide
9797

9898
def create_response(
99-
self, response_data: GraphQLHTTPResponse, sub_response: Response
99+
self,
100+
response_data: GraphQLHTTPResponse,
101+
sub_response: Response,
102+
is_strict: bool,
100103
) -> Response:
101104
sub_response.set_data(self.encode_json(response_data)) # type: ignore
102-
105+
sub_response.headers["content-type"] = (
106+
"application/graphql-response+json" if is_strict else "application/json"
107+
)
103108
return sub_response
104109

105110

src/graphql_server/http/async_base_view.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ async def get_root_value(
164164

165165
@abc.abstractmethod
166166
def create_response(
167-
self, response_data: GraphQLHTTPResponse, sub_response: SubResponse
167+
self,
168+
response_data: GraphQLHTTPResponse,
169+
sub_response: SubResponse,
170+
is_strict: bool,
168171
) -> Response: ...
169172

170173
@abc.abstractmethod
@@ -383,7 +386,6 @@ async def run(
383386
except GraphQLValidationError as e:
384387
if is_strict:
385388
sub_response.status_code = 400 # type: ignore
386-
# sub_response.headers["content-type"] = "application/graphql-response+json"
387389
result = ExecutionResult(data=None, errors=e.errors)
388390
except HTTPException:
389391
raise
@@ -415,7 +417,7 @@ async def run(
415417
self._handle_errors(result.errors, response_data)
416418

417419
return self.create_response(
418-
response_data=response_data, sub_response=sub_response
420+
response_data=response_data, sub_response=sub_response, is_strict=is_strict
419421
)
420422

421423
def encode_multipart_data(self, data: Any, separator: str) -> str:

src/graphql_server/http/sync_base_view.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ def get_root_value(self, request: Request) -> Optional[RootValue]: ...
9090

9191
@abc.abstractmethod
9292
def create_response(
93-
self, response_data: GraphQLHTTPResponse, sub_response: SubResponse
93+
self,
94+
response_data: GraphQLHTTPResponse,
95+
sub_response: SubResponse,
96+
is_strict: bool,
9497
) -> Response: ...
9598

9699
@abc.abstractmethod
@@ -261,7 +264,7 @@ def run(
261264
self._handle_errors(result.errors, response_data)
262265

263266
return self.create_response(
264-
response_data=response_data, sub_response=sub_response
267+
response_data=response_data, sub_response=sub_response, is_strict=is_strict
265268
)
266269

267270
def process_result(

0 commit comments

Comments
 (0)