@fanoutio/apollo-server-lambda-grip
Version:
Apollo Server that runs on AWS Lambda API Gateway and GRIP to pump subscriptions
83 lines • 4.19 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApolloServer = void 0;
const apollo_server_lambda_1 = require("apollo-server-lambda");
const graphql_1 = require("graphql");
const subscriptions_transport_ws_over_http_1 = require("@fanoutio/subscriptions-transport-ws-over-http");
const aws_lambda_adapters_1 = require("./aws-lambda-adapters");
const persistence_1 = require("./persistence");
// This apollo-server extends apollo-server-lambda and adds supports for subscriptions using
// subscriptions-transport-ws-over-http
class ApolloServer extends apollo_server_lambda_1.ApolloServer {
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 (persistence_1.isDynamoDbOptions(persistenceOptions)) {
persistence = new persistence_1.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 = subscriptions_transport_ws_over_http_1.SubscriptionServer.create({
schema,
execute: graphql_1.execute,
subscribe: graphql_1.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 } = aws_lambda_adapters_1.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 aws_lambda_adapters_1.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);
}
});
});
});
}
}
exports.ApolloServer = ApolloServer;
//# sourceMappingURL=apollo-server-lambda-grip.js.map