Skip to content

Commit 182bfb0

Browse files
committed
Address review comments.
1 parent b2c6b1c commit 182bfb0

File tree

5 files changed

+20
-46
lines changed

5 files changed

+20
-46
lines changed

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@
505505
"@types/express": "^4.17.21",
506506
"cors": "^2.8.5",
507507
"express": "^4.21.0",
508-
"graphql": "^16.12.0",
509508
"protobufjs": "^7.2.2"
510509
},
511510
"devDependencies": {
@@ -537,6 +536,7 @@
537536
"eslint-plugin-prettier": "^4.2.1",
538537
"firebase-admin": "^13.0.0",
539538
"genkit": "^1.0.0-rc.4",
539+
"graphql": "^16.12.0",
540540
"jsdom": "^16.2.1",
541541
"jsonwebtoken": "^9.0.0",
542542
"jwk-to-pem": "^2.0.5",

spec/v2/providers/dataconnect.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ describe("dataconnect", () => {
618618
const func = dataconnect.onGraphRequest(opts);
619619
expect(func.__endpoint).to.deep.equal({
620620
...expectedEndpointBase,
621-
dataConnectHttpsTrigger: {},
621+
dataConnectGraphqlTrigger: {},
622622
});
623623
});
624624
it("returns callable function with request opts with Data Connect trigger", () => {
@@ -636,7 +636,7 @@ describe("dataconnect", () => {
636636
const func = dataconnect.onGraphRequest(opts);
637637
expect(func.__endpoint).to.deep.equal({
638638
...expectedEndpointBase,
639-
dataConnectHttpsTrigger: {
639+
dataConnectGraphqlTrigger: {
640640
invoker: ["[email protected]"],
641641
},
642642
region: ["us-east4"],

src/runtime/manifest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export interface ManifestEndpoint {
7171
genkitAction?: string;
7272
};
7373

74-
dataConnectHttpsTrigger?: {
74+
dataConnectGraphqlTrigger?: {
7575
invoker?: string[];
7676
};
7777

src/v2/providers/dataconnect.ts

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
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";
2423
import express from "express";
2524
import fs from "fs";
26-
import { GraphQLResolveInfo } from "graphql";
25+
import type { GraphQLResolveInfo } from "graphql";
2726
import { HttpsFunction, HttpsOptions } from "./https";
2827
import { CloudEvent, CloudFunction } from "../core";
2928
import { ParamsOf, VarName } from "../../common/params";
@@ -42,7 +41,6 @@ import { Expression } from "../../params";
4241
import * as options from "../options";
4342
import { ResetValue } from "../../common/options";
4443
import { withErrorHandler, Request } from "../../common/providers/https";
45-
import { isDebugFeatureEnabled } from "../../common/debug";
4644
import { 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
*/
429427
export 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

Comments
 (0)