|
21 | 21 | // SOFTWARE. |
22 | 22 |
|
23 | 23 | import { expect } from "chai"; |
| 24 | +import fs from "fs"; |
| 25 | +import * as sinon from "sinon"; |
24 | 26 | import * as dataconnect from "../../../src/v2/providers/dataconnect"; |
25 | 27 | import { CloudEvent } from "../../../src/v2"; |
26 | 28 | import { onInit } from "../../../src/v2/core"; |
@@ -527,4 +529,100 @@ describe("dataconnect", () => { |
527 | 529 | expect(hello).to.equal("world"); |
528 | 530 | }); |
529 | 531 | }); |
| 532 | + |
| 533 | + describe("initGraphqlServer", () => { |
| 534 | + let fsStub: sinon.SinonStub; |
| 535 | + |
| 536 | + beforeEach(() => { |
| 537 | + fsStub = sinon.stub(fs, "readFileSync"); |
| 538 | + }); |
| 539 | + |
| 540 | + afterEach(() => { |
| 541 | + sinon.restore(); |
| 542 | + }); |
| 543 | + |
| 544 | + it("both `schema` and `schemaFilePath` set", async () => { |
| 545 | + const opts = { |
| 546 | + schema: "type Query { hello: String }", |
| 547 | + schemaFilePath: "schema.gql", |
| 548 | + resolvers: { |
| 549 | + query: { |
| 550 | + hello: () => "Hello, world!", |
| 551 | + }, |
| 552 | + }, |
| 553 | + }; |
| 554 | + try { |
| 555 | + await dataconnect.initGraphqlServer(opts); |
| 556 | + } catch (err: any) { |
| 557 | + expect(err.message).to.equal( |
| 558 | + "Exactly one of 'schema' or 'schemaFilePath' must be provided." |
| 559 | + ); |
| 560 | + } |
| 561 | + }); |
| 562 | + it("neither `schema` nor `schemaFilePath` set", async () => { |
| 563 | + const opts = { |
| 564 | + resolvers: { |
| 565 | + query: { |
| 566 | + hello: () => "Hello, world!", |
| 567 | + }, |
| 568 | + }, |
| 569 | + }; |
| 570 | + try { |
| 571 | + await dataconnect.initGraphqlServer(opts); |
| 572 | + } catch (err: any) { |
| 573 | + expect(err.message).to.equal( |
| 574 | + "Exactly one of 'schema' or 'schemaFilePath' must be provided." |
| 575 | + ); |
| 576 | + } |
| 577 | + }); |
| 578 | + it("cannot read file in `schemaFilePath`", async () => { |
| 579 | + fsStub.throws(new Error("test file not found error")); |
| 580 | + const opts = { |
| 581 | + schemaFilePath: "schema.gql", |
| 582 | + resolvers: { |
| 583 | + query: { |
| 584 | + hello: () => "Hello, world!", |
| 585 | + }, |
| 586 | + }, |
| 587 | + }; |
| 588 | + try { |
| 589 | + await dataconnect.initGraphqlServer(opts); |
| 590 | + } catch (err: any) { |
| 591 | + expect(err.message).to.contain("test file not found error"); |
| 592 | + } |
| 593 | + }); |
| 594 | + it("no resolvers provided", async () => { |
| 595 | + const opts = { |
| 596 | + schema: "type Query { hello: String }", |
| 597 | + resolvers: {}, |
| 598 | + }; |
| 599 | + try { |
| 600 | + await dataconnect.initGraphqlServer(opts); |
| 601 | + } catch (err: any) { |
| 602 | + expect(err.message).to.equal("At least one query or mutation resolver must be provided."); |
| 603 | + } |
| 604 | + }); |
| 605 | + }); |
| 606 | + |
| 607 | + describe("onGraphRequest", () => { |
| 608 | + it("returns callable function", () => { |
| 609 | + const opts = { |
| 610 | + schema: "type Query { hello: String }", |
| 611 | + schemaFilePath: "schema.gql", |
| 612 | + resolvers: { |
| 613 | + query: { |
| 614 | + hello: () => "Hello, world!", |
| 615 | + }, |
| 616 | + }, |
| 617 | + }; |
| 618 | + const func = dataconnect.onGraphRequest(opts); |
| 619 | + expect(func.__trigger).to.deep.equal({ |
| 620 | + platform: "gcfv2", |
| 621 | + httpsTrigger: { |
| 622 | + allowInsecure: false, |
| 623 | + }, |
| 624 | + labels: {}, |
| 625 | + }); |
| 626 | + }); |
| 627 | + }); |
530 | 628 | }); |
0 commit comments