From f69e89ede4ff3e5ad404d145c67e0776d542009a Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Fri, 15 Dec 2023 00:41:03 +0100 Subject: [PATCH] refactor: change to use json-stream-stringify to allow for large POJO stringification --- package.json | 1 + src/server.ts | 17 ++++++++++++++++- yarn.lock | 8 ++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ab6fd8aadb..ccd37f990c 100644 --- a/package.json +++ b/package.json @@ -93,6 +93,7 @@ "eslint-plugin-prettier": "^5.0.1", "graphql-parse-resolve-info": "^4.14.0", "graphql-tag": "^2.12.6", + "json-stream-stringify": "^3.1.1", "koa": "^2.14.2", "koa-bodyparser": "^4.4.1", "lint-staged": "^15.2.0", diff --git a/src/server.ts b/src/server.ts index f047ccf3b4..587cf5d36f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -9,6 +9,7 @@ import cors from '@koa/cors'; import Koa from 'koa'; import bodyParser from 'koa-bodyparser'; import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http'; +import { JsonStreamStringify } from 'json-stream-stringify'; const gqlServer = async (): Promise> => { const app = new Koa(); @@ -27,7 +28,21 @@ const gqlServer = async (): Promise { + const stringifyStream = new JsonStreamStringify(value); + let stringified = ''; + + try { + for await (const chunk of stringifyStream) { + stringified += chunk; + } + + return `${stringified}\n`; + } catch (error) { + return ''; + } + } }); await apolloServer.start(); diff --git a/yarn.lock b/yarn.lock index f5fce45f13..a9e67978db 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1524,6 +1524,7 @@ __metadata: graphql: "npm:^16.8.1" graphql-parse-resolve-info: "npm:^4.14.0" graphql-tag: "npm:^2.12.6" + json-stream-stringify: "npm:^3.1.1" koa: "npm:^2.14.2" koa-bodyparser: "npm:^4.4.1" lint-staged: "npm:^15.2.0" @@ -7072,6 +7073,13 @@ __metadata: languageName: node linkType: hard +"json-stream-stringify@npm:^3.1.1": + version: 3.1.1 + resolution: "json-stream-stringify@npm:3.1.1" + checksum: db6234d7afe5b933aee6b8d6328fb2816850ab6c1741ce3b335f29d866e9dad7fc1f0ca0d7961bd75145d3d9e6b725d73169de09982cb465537becf20ba3a36b + languageName: node + linkType: hard + "json-to-pretty-yaml@npm:^1.2.2": version: 1.2.2 resolution: "json-to-pretty-yaml@npm:1.2.2"