Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 21 changed files with 85 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/apollo-datasource-rest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-datasource-rest",
"version": "0.300.0-alpha.4",
"version": "0.300.0-lambda.0",
"author": "Apollo <[email protected]>",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-azure-functions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-azure-functions",
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Production-ready Node.js GraphQL server for Azure Functions",
"keywords": [
"GraphQL",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-cloud-functions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-cloud-functions",
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Production-ready Node.js GraphQL server for Google Cloud Functions",
"keywords": [
"GraphQL",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-cloudflare/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-cloudflare",
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Production-ready Node.js GraphQL server for Cloudflare workers",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-core",
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Core engine for Apollo GraphQL server",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-express/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-express",
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Production-ready Node.js GraphQL server for Express",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-fastify/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-fastify",
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Production-ready Node.js GraphQL server for Fastify",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-hapi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-hapi",
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Production-ready Node.js GraphQL server for Hapi",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-integration-testsuite/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "apollo-server-integration-testsuite",
"private": true,
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Apollo Server Integrations testsuite",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-koa/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-koa",
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Production-ready Node.js GraphQL server for Koa",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-server-lambda/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-lambda",
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Production-ready Node.js GraphQL server for AWS Lambda",
"keywords": [
"GraphQL",
Expand All @@ -26,8 +26,8 @@
"node": ">=12.0"
},
"dependencies": {
"@vendia/serverless-express": "^4.3.7",
"@types/aws-lambda": "^8.10.76",
"@vendia/serverless-express": "^4.3.7",
"apollo-server-core": "file:../apollo-server-core",
"apollo-server-express": "file:../apollo-server-express",
"express": "^4.17.1"
Expand Down
20 changes: 19 additions & 1 deletion packages/apollo-server-lambda/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import {
ApolloServer as ApolloServerExpress,
GetMiddlewareOptions,
} from 'apollo-server-express';
import type { GraphQLOptions } from 'apollo-server-core';
import express from 'express';
import serverlessExpress from '@vendia/serverless-express';
import serverlessExpress, {
getCurrentInvoke,
} from '@vendia/serverless-express';

export interface CreateHandlerOptions {
expressAppFromMiddleware?: (
Expand Down Expand Up @@ -43,4 +46,19 @@ export class ApolloServer extends ApolloServerExpress {
return (await realHandler(...args)) as TResult;
};
}

// This method is called by apollo-server-express with the request and
// response. It fetches the Lambda context as well (from a global variable,
// which is safe because the Lambda runtime doesn't invoke multiple operations
// concurrently).
async createGraphQLServerOptions(
req: express.Request,
res: express.Response,
): Promise<GraphQLOptions> {
const { event, context } = getCurrentInvoke();
return super.graphQLServerOptions({
express: { req, res },
lambda: { event, context },
});
}
}
48 changes: 44 additions & 4 deletions packages/apollo-server-lambda/src/__tests__/ApolloServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,55 @@ describe('apollo-server-lambda', () => {

const createLambda = (
expressGetMiddlewareOptions: Partial<GetMiddlewareOptions> = {},
config: Config = { typeDefs, resolvers },
) => {
const server = new ApolloServer({
typeDefs,
resolvers,
});
const server = new ApolloServer(config);
const handler = server.createHandler({ expressGetMiddlewareOptions });
return createMockServer(handler);
};

describe('context', () => {
it('receives both Express and Lambda context', async () => {
const app = createLambda(
{},
{
typeDefs: 'type Query { context: String }',
resolvers: {
Query: {
context: (_parent, _args, context) => JSON.stringify(context),
},
},
// Add something interesting from each context argument part to the
// context.
context({ express, lambda }) {
const { req, res } = express;
const { event, context } = lambda;
return {
reqHttpVersion: req.httpVersion,
resHasApp: !!res.app,
eventVersion: event.version,
contextFunctionName: context.functionName,
};
},
},
);
await request(app)
.post('/graphql')
.send({ query: '{context}' })
.expect(200)
.expect((res) => {
expect(typeof res.body.data.context).toBe("string");
const context = JSON.parse(res.body.data.context);
expect(context).toEqual({
reqHttpVersion: '1.1',
resHasApp: true,
eventVersion: '2.0',
contextFunctionName: 'someFunc',
});
});
});
});

describe('healthchecks', () => {
it('creates a healthcheck endpoint', async () => {
const app = createLambda();
Expand Down
6 changes: 3 additions & 3 deletions packages/apollo-server-lambda/src/__tests__/mockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export function createMockServer(
// reject should cause a test to fail
req.on('end', async () => {
const event = eventFromRequest(req, body);
const result = await handler(
const result = (await handler(
event,
{} as LambdaContext, // we don't bother with all the fields
{ functionName: 'someFunc' } as LambdaContext, // we don't bother with all the fields
() => {
throw Error("we don't use callback");
},
) as APIGatewayProxyStructuredResultV2;
)) as APIGatewayProxyStructuredResultV2;
res.statusCode = result.statusCode!;
Object.entries(result.headers ?? {}).forEach(([key, value]) => {
res.setHeader(key, value.toString());
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-micro/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-micro",
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Production-ready Node.js GraphQL server for Micro",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-plugin-base/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-plugin-base",
"version": "0.300.0-alpha.4",
"version": "0.300.0-lambda.0",
"description": "Apollo Server plugin base classes",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-plugin-operation-registry",
"version": "0.300.0-alpha.4",
"version": "0.300.0-lambda.0",
"description": "Apollo Server operation registry",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-plugin-response-cache/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-plugin-response-cache",
"version": "0.300.0-alpha.4",
"version": "0.300.0-lambda.0",
"description": "Apollo Server full query response cache",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-testing/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-testing",
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Test utils for apollo-server",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-types",
"version": "0.300.0-alpha.4",
"version": "0.300.0-lambda.0",
"description": "Apollo Server shared types",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server",
"version": "3.0.0-alpha.4",
"version": "3.0.0-lambda.0",
"description": "Production ready GraphQL Server",
"author": "Apollo <[email protected]>",
"main": "dist/index.js",
Expand Down

0 comments on commit 7d4854a

Please sign in to comment.