Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment AWS Lambda Streaming #3119

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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: 23 additions & 11 deletions examples/aws-lambda/lambda/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { APIGatewayEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
/* eslint-disable @typescript-eslint/no-explicit-any */
import { pipeline, Writable } from 'node:stream';
import { promisify } from 'node:util';
import { APIGatewayEvent, Context } from 'aws-lambda';
import { createSchema, createYoga } from 'graphql-yoga';

declare const awslambda: any;

const pipeline$ = promisify(pipeline);

const yoga = createYoga<{
event: APIGatewayEvent;
lambdaContext: Context;
responseStream: Writable;
}>({
graphqlEndpoint: '/graphql',
landingPage: false,
Expand All @@ -21,10 +29,11 @@ const yoga = createYoga<{
}),
});

export async function handler(
export const handler = awslambda.streamifyResponse(async function handler(
event: APIGatewayEvent,
responseStream: Writable,
lambdaContext: Context,
): Promise<APIGatewayProxyResult> {
) {
const response = await yoga.fetch(
event.path +
'?' +
Expand All @@ -39,15 +48,18 @@ export async function handler(
{
event,
lambdaContext,
responseStream,
},
);

const responseHeaders = Object.fromEntries(response.headers.entries());

return {
responseStream = awslambda.HttpResponseStream.from(responseStream, {
statusCode: response.status,
headers: responseHeaders,
body: await response.text(),
isBase64Encoded: false,
};
}
headers: Object.fromEntries(response.headers.entries()),
});

if (response.body) {
await pipeline$(response.body, responseStream);
} else {
responseStream.end();
}
});
2 changes: 1 addition & 1 deletion examples/aws-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"devDependencies": {
"@aws-cdk/assert": "2.68.0",
"@types/aws-lambda": "8.10.116",
"@types/aws-lambda": "8.10.129",
"@types/node": "18.16.16",
"aws-cdk": "2.83.0",
"aws-cdk-lib": "2.83.0",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading