diff --git a/packages/apollo-datasource-rest/package.json b/packages/apollo-datasource-rest/package.json index 82987035690..45e5b5126d5 100644 --- a/packages/apollo-datasource-rest/package.json +++ b/packages/apollo-datasource-rest/package.json @@ -1,6 +1,6 @@ { "name": "apollo-datasource-rest", - "version": "0.300.0-alpha.4", + "version": "0.300.0-lambda.0", "author": "Apollo ", "license": "MIT", "repository": { diff --git a/packages/apollo-server-azure-functions/package.json b/packages/apollo-server-azure-functions/package.json index 5e7e14721f0..88041b0eaf8 100644 --- a/packages/apollo-server-azure-functions/package.json +++ b/packages/apollo-server-azure-functions/package.json @@ -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", diff --git a/packages/apollo-server-cloud-functions/package.json b/packages/apollo-server-cloud-functions/package.json index 066b9073bd4..54c710fdb7b 100644 --- a/packages/apollo-server-cloud-functions/package.json +++ b/packages/apollo-server-cloud-functions/package.json @@ -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", diff --git a/packages/apollo-server-cloudflare/package.json b/packages/apollo-server-cloudflare/package.json index e78758fe526..ba8678f6d8c 100644 --- a/packages/apollo-server-cloudflare/package.json +++ b/packages/apollo-server-cloudflare/package.json @@ -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", diff --git a/packages/apollo-server-core/package.json b/packages/apollo-server-core/package.json index 0e7801cac7f..048a3964c67 100644 --- a/packages/apollo-server-core/package.json +++ b/packages/apollo-server-core/package.json @@ -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", diff --git a/packages/apollo-server-express/package.json b/packages/apollo-server-express/package.json index 93e88319dec..77065ef2e69 100644 --- a/packages/apollo-server-express/package.json +++ b/packages/apollo-server-express/package.json @@ -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", diff --git a/packages/apollo-server-fastify/package.json b/packages/apollo-server-fastify/package.json index 9a6651b3077..908dc29cfb0 100644 --- a/packages/apollo-server-fastify/package.json +++ b/packages/apollo-server-fastify/package.json @@ -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", diff --git a/packages/apollo-server-hapi/package.json b/packages/apollo-server-hapi/package.json index e4e4a4e10e2..cae5213d31b 100644 --- a/packages/apollo-server-hapi/package.json +++ b/packages/apollo-server-hapi/package.json @@ -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", diff --git a/packages/apollo-server-integration-testsuite/package.json b/packages/apollo-server-integration-testsuite/package.json index bb669cb983a..e7c19b5b1e5 100644 --- a/packages/apollo-server-integration-testsuite/package.json +++ b/packages/apollo-server-integration-testsuite/package.json @@ -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", diff --git a/packages/apollo-server-koa/package.json b/packages/apollo-server-koa/package.json index 78aad101efb..03dcd6bfa20 100644 --- a/packages/apollo-server-koa/package.json +++ b/packages/apollo-server-koa/package.json @@ -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", diff --git a/packages/apollo-server-lambda/package.json b/packages/apollo-server-lambda/package.json index 152d6a25f65..694a712401d 100644 --- a/packages/apollo-server-lambda/package.json +++ b/packages/apollo-server-lambda/package.json @@ -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", @@ -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" diff --git a/packages/apollo-server-lambda/src/ApolloServer.ts b/packages/apollo-server-lambda/src/ApolloServer.ts index 9036642bcf1..caa757726f3 100644 --- a/packages/apollo-server-lambda/src/ApolloServer.ts +++ b/packages/apollo-server-lambda/src/ApolloServer.ts @@ -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?: ( @@ -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 { + const { event, context } = getCurrentInvoke(); + return super.graphQLServerOptions({ + express: { req, res }, + lambda: { event, context }, + }); + } } diff --git a/packages/apollo-server-lambda/src/__tests__/ApolloServer.test.ts b/packages/apollo-server-lambda/src/__tests__/ApolloServer.test.ts index 52b132008b9..24c384cd048 100644 --- a/packages/apollo-server-lambda/src/__tests__/ApolloServer.test.ts +++ b/packages/apollo-server-lambda/src/__tests__/ApolloServer.test.ts @@ -50,15 +50,55 @@ describe('apollo-server-lambda', () => { const createLambda = ( expressGetMiddlewareOptions: Partial = {}, + 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(); diff --git a/packages/apollo-server-lambda/src/__tests__/mockServer.ts b/packages/apollo-server-lambda/src/__tests__/mockServer.ts index cb565eb0228..b94266e7f37 100644 --- a/packages/apollo-server-lambda/src/__tests__/mockServer.ts +++ b/packages/apollo-server-lambda/src/__tests__/mockServer.ts @@ -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()); diff --git a/packages/apollo-server-micro/package.json b/packages/apollo-server-micro/package.json index d835b889acc..3550dad0ab8 100644 --- a/packages/apollo-server-micro/package.json +++ b/packages/apollo-server-micro/package.json @@ -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", diff --git a/packages/apollo-server-plugin-base/package.json b/packages/apollo-server-plugin-base/package.json index f31962188d8..7401ce90ea1 100644 --- a/packages/apollo-server-plugin-base/package.json +++ b/packages/apollo-server-plugin-base/package.json @@ -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", diff --git a/packages/apollo-server-plugin-operation-registry/package.json b/packages/apollo-server-plugin-operation-registry/package.json index 8b40ec7cc81..33b5c38ee1d 100644 --- a/packages/apollo-server-plugin-operation-registry/package.json +++ b/packages/apollo-server-plugin-operation-registry/package.json @@ -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", diff --git a/packages/apollo-server-plugin-response-cache/package.json b/packages/apollo-server-plugin-response-cache/package.json index 15cd7c92da4..351a5215379 100644 --- a/packages/apollo-server-plugin-response-cache/package.json +++ b/packages/apollo-server-plugin-response-cache/package.json @@ -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", diff --git a/packages/apollo-server-testing/package.json b/packages/apollo-server-testing/package.json index f5300e36f29..a30943704d3 100644 --- a/packages/apollo-server-testing/package.json +++ b/packages/apollo-server-testing/package.json @@ -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", diff --git a/packages/apollo-server-types/package.json b/packages/apollo-server-types/package.json index 61497cad299..76d9a034254 100644 --- a/packages/apollo-server-types/package.json +++ b/packages/apollo-server-types/package.json @@ -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", diff --git a/packages/apollo-server/package.json b/packages/apollo-server/package.json index 439c89f0035..6590736fcf1 100644 --- a/packages/apollo-server/package.json +++ b/packages/apollo-server/package.json @@ -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 ", "main": "dist/index.js",