Skip to content

Commit c2297d8

Browse files
committed
Fix some of the error catching.
1 parent f315016 commit c2297d8

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/v2/providers/dataconnect.ts

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

2323
import cors from "cors";
2424
import express from "express";
25+
import fs from "fs";
2526
import { GraphQLResolveInfo } from "graphql";
2627
import { HttpsFunction, onRequest } from "./https";
2728
import { CloudEvent, CloudFunction } from "../core";
@@ -374,12 +375,21 @@ function onOperation<Variables, ResponseData, PathPatternOrOptions>(
374375
}
375376

376377
async 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

Comments
 (0)