Skip to content

Commit f184cdd

Browse files
committed
Move some errors out of try-catch block.
1 parent c2297d8 commit f184cdd

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/v2/providers/dataconnect.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -375,28 +375,25 @@ function onOperation<Variables, ResponseData, PathPatternOrOptions>(
375375
}
376376

377377
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.");
378+
if ((!opts.schema && !opts.schemaFilePath) || (opts.schema && opts.schemaFilePath)) {
379+
throw new Error("Exactly one of 'schema' or 'schemaFilePath' must be provided.");
383380
}
384381
if (opts.schemaFilePath) {
385382
opts.schema = fs.readFileSync(opts.schemaFilePath, "utf-8");
386383
}
387384
if (!opts.resolvers.query && !opts.resolvers.mutation) {
388385
throw new Error("At least one query or mutation resolver must be provided.");
389386
}
387+
const apolloResolvers: { [key: string]: any } = {};
388+
if (opts.resolvers.query) {
389+
apolloResolvers.Query = opts.resolvers.query;
390+
}
391+
if (opts.resolvers.mutation) {
392+
apolloResolvers.Mutation = opts.resolvers.mutation;
393+
}
390394
try {
391395
const { ApolloServer } = await import("@apollo/server");
392396
const { expressMiddleware } = await import("@as-integrations/express4");
393-
const apolloResolvers: { [key: string]: any } = {};
394-
if (opts.resolvers.query) {
395-
apolloResolvers.Query = opts.resolvers.query;
396-
}
397-
if (opts.resolvers.mutation) {
398-
apolloResolvers.Mutation = opts.resolvers.mutation;
399-
}
400397
const serverPromise = (async () => {
401398
const app = express();
402399
const server = new ApolloServer({
@@ -408,11 +405,12 @@ async function initGraphqlServer(opts: GraphqlServerOptions): Promise<express.Ex
408405
return app;
409406
})();
410407
return serverPromise;
411-
} catch (e) {
412-
throw new Error(
413-
"'@apollo/server' and '@as-integrations/express4' are required to use 'onGraphRequest'. Please add these dependencies to your project to use this feature: " +
414-
e
415-
);
408+
} catch (e: unknown) {
409+
if (e instanceof Error) {
410+
throw new Error("Error initializing GraphQL server: " + e.message);
411+
} else {
412+
throw e;
413+
}
416414
}
417415
}
418416

0 commit comments

Comments
 (0)