2222
2323import cors from "cors" ;
2424import express from "express" ;
25+ import fs from "fs" ;
2526import { GraphQLResolveInfo } from "graphql" ;
2627import { HttpsFunction , onRequest } from "./https" ;
2728import { CloudEvent , CloudFunction } from "../core" ;
@@ -374,12 +375,21 @@ function onOperation<Variables, ResponseData, PathPatternOrOptions>(
374375}
375376
376377async function initGraphqlServer ( opts : GraphqlServerOptions ) : Promise < express . Express > {
378+ if ( ! opts . schema && ! opts . schemaFilePath ) {
379+ throw new Error ( "Either 'schema' or 'schemaFilePath' must be provided." ) ;
380+ }
381+ if ( opts . schema && opts . schemaFilePath ) {
382+ throw new Error ( "Only one of 'schema' or 'schemaFilePath' can be provided." ) ;
383+ }
384+ if ( opts . schemaFilePath ) {
385+ opts . schema = fs . readFileSync ( opts . schemaFilePath , "utf-8" ) ;
386+ }
387+ if ( ! opts . resolvers . query && ! opts . resolvers . mutation ) {
388+ throw new Error ( "At least one query or mutation resolver must be provided." ) ;
389+ }
377390 try {
378391 const { ApolloServer } = await import ( "@apollo/server" ) ;
379392 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- }
383393 const apolloResolvers : { [ key : string ] : any } = { } ;
384394 if ( opts . resolvers . query ) {
385395 apolloResolvers . Query = opts . resolvers . query ;
@@ -390,7 +400,6 @@ async function initGraphqlServer(opts: GraphqlServerOptions): Promise<express.Ex
390400 const serverPromise = ( async ( ) => {
391401 const app = express ( ) ;
392402 const server = new ApolloServer ( {
393- // TODO: Support loading schema from file path.
394403 typeDefs : opts . schema ,
395404 resolvers : apolloResolvers ,
396405 } ) ;
@@ -441,6 +450,7 @@ export interface GraphqlServerOptions {
441450 path ?: string ;
442451 /** A map of functions that populate data for individual GraphQL schema fields. */
443452 resolvers : GraphqlResolvers ;
453+ // TODO: Add a field for a context function.
444454}
445455
446456/** Per-request context state shared by all resolvers in a particular query. */
0 commit comments