Skip to content

Commit 21ac89a

Browse files
committed
Fix function to return not a Promise.
1 parent 614a5ff commit 21ac89a

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/v2/providers/dataconnect.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import cors from "cors";
2424
import express from "express";
25-
import { json } from "body-parser";
2625
import { GraphQLResolveInfo } from "graphql";
2726
import { HttpsFunction, onRequest } from "./https";
2827
import { CloudEvent, CloudFunction } from "../core";
@@ -374,30 +373,32 @@ function onOperation<Variables, ResponseData, PathPatternOrOptions>(
374373
return func;
375374
}
376375

377-
/**
378-
* Handles HTTPS GraphQL requests.
379-
* @param {GraphqlServerOptions} opts - Options for configuring the GraphQL server.
380-
* @returns {HttpsFunction} A function you can export and deploy.
381-
*/
382-
export async function onGraphRequest(opts: GraphqlServerOptions): Promise<HttpsFunction> {
376+
async function initGraphqlServer(opts: GraphqlServerOptions): Promise<express.Express> {
383377
try {
384378
const { ApolloServer } = await import("@apollo/server");
385379
const { expressMiddleware } = await import("@as-integrations/express4");
380+
if (!opts.resolvers.query && !opts.resolvers.mutation) {
381+
throw new Error("At least one query or mutation resolver must be provided.");
382+
}
383+
const apolloResolvers: { [key: string]: any } = {};
384+
if (opts.resolvers.query) {
385+
apolloResolvers.Query = opts.resolvers.query;
386+
}
387+
if (opts.resolvers.mutation) {
388+
apolloResolvers.Mutation = opts.resolvers.mutation;
389+
}
386390
const serverPromise = (async () => {
387391
const app = express();
388392
const server = new ApolloServer({
389393
// TODO: Support loading schema from file path.
390394
typeDefs: opts.schema,
391-
resolvers: { Query: opts.resolvers.query, Mutation: opts.resolvers.mutation },
395+
resolvers: apolloResolvers,
392396
});
393397
await server.start();
394-
app.use(`/${opts.path ?? "graphql"}`, cors(), json(), expressMiddleware(server));
398+
app.use(`/${opts.path ?? "graphql"}`, cors(), express.json(), expressMiddleware(server));
395399
return app;
396400
})();
397-
return onRequest(async (req, res) => {
398-
const app = await serverPromise;
399-
app(req, res);
400-
});
401+
return serverPromise;
401402
} catch (e) {
402403
throw new Error(
403404
"'@apollo/server' and '@as-integrations/express4' are required to use 'onGraphRequest'. Please add these dependencies to your project to use this feature: " +
@@ -406,6 +407,21 @@ export async function onGraphRequest(opts: GraphqlServerOptions): Promise<HttpsF
406407
}
407408
}
408409

410+
let serverPromise: Promise<express.Express> | null = null;
411+
412+
/**
413+
* Handles HTTPS GraphQL requests.
414+
* @param {GraphqlServerOptions} opts - Options for configuring the GraphQL server.
415+
* @returns {HttpsFunction} A function you can export and deploy.
416+
*/
417+
export function onGraphRequest(opts: GraphqlServerOptions): HttpsFunction {
418+
return onRequest(async (req, res) => {
419+
serverPromise = serverPromise ?? initGraphqlServer(opts);
420+
const app = await serverPromise;
421+
app(req, res);
422+
});
423+
}
424+
409425
/** Options for configuring the GraphQL server. */
410426
export interface GraphqlServerOptions {
411427
/**

0 commit comments

Comments
 (0)