Skip to content

Commit

Permalink
refactor: change to use json-stream-stringify to allow for large POJO…
Browse files Browse the repository at this point in the history
… stringification
  • Loading branch information
favna committed Dec 14, 2023
1 parent 3df710c commit f69e89e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 16 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Server<typeof IncomingMessage, typeof ServerResponse>> => {
const app = new Koa();
Expand All @@ -27,7 +28,21 @@ const gqlServer = async (): Promise<Server<typeof IncomingMessage, typeof Server
variables: defaultVariables,
embed: true
})
]
],
stringifyResult: async (value) => {

Check failure on line 32 in src/server.ts

View workflow job for this annotation

GitHub Actions / Compile source code

Type '(value: FormattedExecutionResult<ObjMap<unknown>, ObjMap<unknown>>) => Promise<string>' is not assignable to type '(value: FormattedExecutionResult<ObjMap<unknown>, ObjMap<unknown>>) => string'.
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();
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit f69e89e

Please sign in to comment.