2222
2323import cors from "cors" ;
2424import express from "express" ;
25- import { json } from "body-parser" ;
2625import { GraphQLResolveInfo } from "graphql" ;
2726import { HttpsFunction , onRequest } from "./https" ;
2827import { 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. */
410426export interface GraphqlServerOptions {
411427 /**
0 commit comments