diff --git a/src/__tests__/http-test.ts b/src/__tests__/http-test.ts index 1fec0933..7af96620 100644 --- a/src/__tests__/http-test.ts +++ b/src/__tests__/http-test.ts @@ -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'), @@ -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', '', [ '{', @@ -1221,7 +1214,6 @@ function runTests(server: Server) { ' "hasNext": false', '}', ].join('\n'), - '', '-----', '', ].join('\r\n'), @@ -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'), @@ -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'), @@ -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'), @@ -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); @@ -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'), ); }); @@ -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'), @@ -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'), diff --git a/src/index.ts b/src/index.ts index 05161631..9cc5c51f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) { @@ -489,7 +490,6 @@ export function graphqlHTTP(options: Options): Middleware { hasNext: false, }); } - response.write('\r\n-----\r\n'); response.end(); finishedIterable = true; return; @@ -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' &&