2020// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121// SOFTWARE.
2222
23- import cors from "cors" ;
2423import express from "express" ;
2524import fs from "fs" ;
26- import { GraphQLResolveInfo } from "graphql" ;
25+ import type { GraphQLResolveInfo } from "graphql" ;
2726import { HttpsFunction , HttpsOptions } from "./https" ;
2827import { CloudEvent , CloudFunction } from "../core" ;
2928import { ParamsOf , VarName } from "../../common/params" ;
@@ -42,7 +41,6 @@ import { Expression } from "../../params";
4241import * as options from "../options" ;
4342import { ResetValue } from "../../common/options" ;
4443import { withErrorHandler , Request } from "../../common/providers/https" ;
45- import { isDebugFeatureEnabled } from "../../common/debug" ;
4644import { convertIfPresent , convertInvoker } from "../../common/encoding" ;
4745
4846/** @internal */
@@ -389,7 +387,7 @@ export async function initGraphqlServer(opts: GraphqlServerOptions): Promise<exp
389387 if ( ! opts . resolvers . query && ! opts . resolvers . mutation ) {
390388 throw new Error ( "At least one query or mutation resolver must be provided." ) ;
391389 }
392- const apolloResolvers : { [ key : string ] : any } = { } ;
390+ const apolloResolvers : Record < string , any > = { } ;
393391 if ( opts . resolvers . query ) {
394392 apolloResolvers . Query = opts . resolvers . query ;
395393 }
@@ -406,7 +404,7 @@ export async function initGraphqlServer(opts: GraphqlServerOptions): Promise<exp
406404 resolvers : apolloResolvers ,
407405 } ) ;
408406 await server . start ( ) ;
409- app . use ( `/${ opts . path ?? "graphql" } ` , cors ( ) , express . json ( ) , expressMiddleware ( server ) ) ;
407+ app . use ( `/${ opts . path ?? "graphql" } ` , express . json ( ) , expressMiddleware ( server ) ) ;
410408 return app ;
411409 } ) ( ) ;
412410 return serverPromise ;
@@ -427,41 +425,16 @@ let serverPromise: Promise<express.Express> | null = null;
427425 * @returns {HttpsFunction } A function you can export and deploy.
428426 */
429427export function onGraphRequest ( opts : GraphqlServerOptions ) : HttpsFunction {
430- let handler : ( req : Request , res : express . Response ) => void | Promise < void > = withErrorHandler (
431- async ( req : Request , res : express . Response ) => {
432- serverPromise = serverPromise ?? initGraphqlServer ( opts ) ;
433- const app = await serverPromise ;
434- app ( req , res ) ;
435- }
428+ const handler : ( req : Request , res : express . Response ) => void | Promise < void > = wrapTraceContext (
429+ withInit (
430+ withErrorHandler ( async ( req : Request , res : express . Response ) => {
431+ serverPromise = serverPromise ?? initGraphqlServer ( opts ) ;
432+ const app = await serverPromise ;
433+ app ( req , res ) ;
434+ } )
435+ )
436436 ) ;
437437
438- if ( isDebugFeatureEnabled ( "enableCors" ) || "cors" in opts ) {
439- let origin = opts . cors instanceof Expression ? opts . cors . value ( ) : opts . cors ;
440- if ( isDebugFeatureEnabled ( "enableCors" ) ) {
441- // Respect `cors: false` to turn off cors even if debug feature is enabled.
442- origin = opts . cors === false ? false : true ;
443- }
444- // Arrays cause the access-control-allow-origin header to be dynamic based
445- // on the origin header of the request. If there is only one element in the
446- // array, this is unnecessary.
447- if ( Array . isArray ( origin ) && origin . length === 1 ) {
448- origin = origin [ 0 ] ;
449- }
450- const middleware = cors ( { origin } ) ;
451-
452- const userProvidedHandler = handler ;
453- handler = ( req : Request , res : express . Response ) : void | Promise < void > => {
454- return new Promise ( ( resolve ) => {
455- res . on ( "finish" , resolve ) ;
456- middleware ( req , res , ( ) => {
457- resolve ( userProvidedHandler ( req , res ) ) ;
458- } ) ;
459- } ) ;
460- } ;
461- }
462-
463- handler = wrapTraceContext ( withInit ( handler ) ) ;
464-
465438 const globalOpts = options . getGlobalOptions ( ) ;
466439 const baseOpts = options . optionsToEndpoint ( globalOpts ) ;
467440 // global options calls region a scalar and https allows it to be an array,
@@ -476,16 +449,16 @@ export function onGraphRequest(opts: GraphqlServerOptions): HttpsFunction {
476449 ...baseOpts ?. labels ,
477450 ...specificOpts ?. labels ,
478451 } ,
479- dataConnectHttpsTrigger : { } ,
452+ dataConnectGraphqlTrigger : { } ,
480453 } ;
481454 convertIfPresent (
482- endpoint . dataConnectHttpsTrigger ,
455+ endpoint . dataConnectGraphqlTrigger ,
483456 globalOpts ,
484457 "invoker" ,
485458 "invoker" ,
486459 convertInvoker
487460 ) ;
488- convertIfPresent ( endpoint . dataConnectHttpsTrigger , opts , "invoker" , "invoker" , convertInvoker ) ;
461+ convertIfPresent ( endpoint . dataConnectGraphqlTrigger , opts , "invoker" , "invoker" , convertInvoker ) ;
489462 ( handler as HttpsFunction ) . __endpoint = endpoint ;
490463
491464 return handler as HttpsFunction ;
0 commit comments