Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

[incremental-delivery]: Remove the content-length requirement #728

Merged
merged 7 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 2 additions & 32 deletions src/__tests__/http-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,16 +1074,12 @@ function runTests(server: Server) {
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 26',
'',
'{"data":{},"hasNext":true}',
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 78',
'',
'{"data":{"test":"Hello World"},"path":[],"label":"deferLabel","hasNext":false}',
'',
'-----',
'',
].join('\r\n'),
Expand Down Expand Up @@ -1204,13 +1200,10 @@ function runTests(server: Server) {
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 35',
'',
['{', ' "data": {},', ' "hasNext": true', '}'].join('\n'),
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 79',
'',
[
'{',
Expand All @@ -1221,7 +1214,6 @@ function runTests(server: Server) {
' "hasNext": false',
'}',
].join('\n'),
'',
'-----',
'',
].join('\r\n'),
Expand Down Expand Up @@ -1413,16 +1405,12 @@ function runTests(server: Server) {
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 94',
'',
'{"errors":[{"message":"Custom error format: Throws!"}],"data":{"thrower":null},"hasNext":true}',
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 57',
'',
'{"data":{"test":"Hello World"},"path":[],"hasNext":false}',
'',
'-----',
'',
].join('\r\n'),
Expand Down Expand Up @@ -1464,16 +1452,12 @@ function runTests(server: Server) {
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 46',
'',
'{"data":{"test":"Hello World"},"hasNext":true}',
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 105',
'',
'{"data":{"thrower":null},"path":[],"errors":[{"message":"Custom error format: Throws!"}],"hasNext":false}',
'',
'-----',
'',
].join('\r\n'),
Expand Down Expand Up @@ -2426,16 +2410,12 @@ function runTests(server: Server) {
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 48',
'',
'{"data":{"test2":"Modification"},"hasNext":true}',
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 64',
'',
'{"errors":[{"message":"I did something wrong"}],"hasNext":false}',
'',
'-----',
'',
].join('\r\n'),
Expand Down Expand Up @@ -2500,10 +2480,9 @@ function runTests(server: Server) {
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 47',
'',
'{"data":{"test":"Hello, World"},"hasNext":true}',
'',
'---\r\n',
].join('\r\n'),
);
expect(fakeReturn.callCount).to.equal(1);
Expand Down Expand Up @@ -2565,10 +2544,9 @@ function runTests(server: Server) {
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 47',
'',
'{"data":{"test":"Hello, World"},"hasNext":true}',
'',
'---\r\n',
].join('\r\n'),
);
});
Expand Down Expand Up @@ -2721,16 +2699,12 @@ function runTests(server: Server) {
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 124',
'',
'{"data":{"hello":"Hello Rob"},"hasNext":true,"extensions":{"preservedResult":{"data":{"hello":"Hello Rob"},"hasNext":true}}}',
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 148',
'',
'{"data":{"test":"Hello World"},"path":[],"hasNext":false,"extensions":{"preservedResult":{"data":{"test":"Hello World"},"path":[],"hasNext":false}}}',
'',
'-----',
'',
].join('\r\n'),
Expand Down Expand Up @@ -2801,16 +2775,12 @@ function runTests(server: Server) {
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 45',
'',
'{"data":{"hello":"Hello Rob"},"hasNext":true}',
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: 57',
'',
'{"data":{"test":"Hello World"},"path":[],"hasNext":false}',
'',
'-----',
'',
].join('\r\n'),
Expand Down
20 changes: 9 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ export function graphqlHTTP(options: Options): Middleware {

if (isAsyncIterable(executeResult)) {
response.setHeader('Content-Type', 'multipart/mixed; boundary="-"');
response.write('\r\n---\r\n');
sendPartialResponse(pretty, response, formattedResult);
try {
for await (let payload of executeResult) {
Expand Down Expand Up @@ -489,7 +490,6 @@ export function graphqlHTTP(options: Options): Middleware {
hasNext: false,
});
}
response.write('\r\n-----\r\n');
response.end();
finishedIterable = true;
return;
Expand Down Expand Up @@ -625,16 +625,14 @@ function sendPartialResponse(
): void {
const json = JSON.stringify(result, null, pretty ? 2 : 0);
const chunk = Buffer.from(json, 'utf8');
const data = [
'',
'---',
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' + String(chunk.length),
'',
chunk,
'',
].join('\r\n');
response.write(data);
const data = ['Content-Type: application/json; charset=utf-8', '', chunk];
// @ts-expect-error
if (result.hasNext === true) {
data.push('---\r\n');
} else {
data.push('-----\r\n');
}
response.write(data.join('\r\n'));
// flush response if compression middleware is used
if (
typeof response.flush === 'function' &&
Expand Down