1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.graphqlLambda = void 0;
|
4 | const apollo_server_core_1 = require("apollo-server-core");
|
5 | const apollo_server_env_1 = require("apollo-server-env");
|
6 | function graphqlLambda(options) {
|
7 | if (!options) {
|
8 | throw new Error('Apollo Server requires options.');
|
9 | }
|
10 | if (arguments.length > 1) {
|
11 | throw new Error(`Apollo Server expects exactly one argument, got ${arguments.length}`);
|
12 | }
|
13 | const graphqlHandler = (event, context, callback) => {
|
14 | context.callbackWaitsForEmptyEventLoop = false;
|
15 | let { body, headers, isBase64Encoded } = event;
|
16 | let query;
|
17 | const contentType = (headers['content-type'] || headers['Content-Type'] || '').toLowerCase();
|
18 | const isMultipart = contentType.startsWith('multipart/form-data');
|
19 | if (body && isBase64Encoded && !isMultipart) {
|
20 | body = Buffer.from(body, 'base64').toString();
|
21 | }
|
22 | if (event.httpMethod === 'POST' && !body) {
|
23 | return callback(null, {
|
24 | body: 'POST body missing.',
|
25 | statusCode: 500,
|
26 | });
|
27 | }
|
28 | if (body && event.httpMethod === 'POST' && isMultipart) {
|
29 | query = body;
|
30 | }
|
31 | else if (body && event.httpMethod === 'POST') {
|
32 | query = JSON.parse(body);
|
33 | }
|
34 | else {
|
35 | query = event.queryStringParameters || {};
|
36 | }
|
37 | apollo_server_core_1.runHttpQuery([event, context], {
|
38 | method: event.httpMethod,
|
39 | options: options,
|
40 | query,
|
41 | request: {
|
42 | url: event.path,
|
43 | method: event.httpMethod,
|
44 | headers: new apollo_server_env_1.Headers(event.headers),
|
45 | },
|
46 | }).then(({ graphqlResponse, responseInit }) => {
|
47 | callback(null, {
|
48 | body: graphqlResponse,
|
49 | statusCode: 200,
|
50 | headers: responseInit.headers,
|
51 | });
|
52 | }, (error) => {
|
53 | if ('HttpQueryError' !== error.name)
|
54 | return callback(error);
|
55 | callback(null, {
|
56 | body: error.message,
|
57 | statusCode: error.statusCode,
|
58 | headers: error.headers,
|
59 | });
|
60 | });
|
61 | };
|
62 | return graphqlHandler;
|
63 | }
|
64 | exports.graphqlLambda = graphqlLambda;
|
65 |
|
\ | No newline at end of file |