UNPKG

@fanoutio/apollo-server-lambda-grip

Version:

Apollo Server that runs on AWS Lambda API Gateway and GRIP to pump subscriptions

79 lines 3.94 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { ApolloServer as ApolloServerLambda } from 'apollo-server-lambda'; import { execute, subscribe } from "graphql"; import { SubscriptionServer } from "@fanoutio/subscriptions-transport-ws-over-http"; import { apiGatewayResponseFromHttp, httpFromApiGatewayEvent } from "./aws-lambda-adapters"; import { DynamoDbPersistence, isDynamoDbOptions } from "./persistence"; // This apollo-server extends apollo-server-lambda and adds supports for subscriptions using // subscriptions-transport-ws-over-http export class ApolloServer extends ApolloServerLambda { supportsSubscriptions() { return true; } createHandler(createHandlerOptions) { const { cors, onHealthCheck, grip, gripPrefix, persistence: persistenceOptions, } = createHandlerOptions; // Make sure we have a schema const schema = this.schema; if (this.schema === undefined) { throw new Error('Schema undefined during creation of subscription server.'); } // We need the playground to also hit the subscription path. if (this.playgroundOptions != null) { this.playgroundOptions.subscriptionEndpoint = this.subscriptionsPath; } // Set up persistence let persistence; if (isDynamoDbOptions(persistenceOptions)) { persistence = new DynamoDbPersistence(persistenceOptions); } else { persistence = persistenceOptions; } // Create an instance of subscriptions-transport-ws-over-http's SubscriptionServer // with the passed in GRIP config and prefix const subscriptionServer = SubscriptionServer.create({ schema, execute, subscribe, grip, gripPrefix, persistence, }); // This is the original handler from apollo-server-lambda that we are wrapping, to // handle requests that are not using ws-over-http const originalHandler = super.createHandler({ cors, onHealthCheck }); // This is our replacement handler. return (event, context) => __awaiter(this, void 0, void 0, function* () { yield subscriptionServer.ready(); // We create simulated "req" and "res" objects from this event const { req, res } = httpFromApiGatewayEvent(event); // Check if this is something the subscription transport will handle. const subscriptionHandled = yield subscriptionServer.requestHandler(req, res); if (subscriptionHandled) { // Convert the simulated response object back to an API Gateway response. return apiGatewayResponseFromHttp(res); } // If the subscription transport did not handle it, then we just fall back to the // original handler. return new Promise((resolve, reject) => { originalHandler(event, context, (e, result) => { if (e != null) { reject(e); } else { resolve(result); } }); }); }); } } //# sourceMappingURL=apollo-server-lambda-grip.js.map