Skip to content

Commit 73f9101

Browse files
authored
Merge pull request #76 from Kauabunga/fix/avoidSubscriptionsInstallWhenDisabled
[fix issue #64] No longer installing subscription handlers when disabled
2 parents 9ad31d1 + 2aff6a1 commit 73f9101

2 files changed

Lines changed: 77 additions & 30 deletions

File tree

src/service.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,12 @@ module.exports = function(mixinOptions) {
610610
this.graphqlHandler = this.apolloServer.createHandler(
611611
mixinOptions.serverOptions
612612
);
613-
this.apolloServer.installSubscriptionHandlers(this.server);
613+
614+
if (mixinOptions.serverOptions.subscriptions !== false) {
615+
// Avoid installing the subscription handlers if they have been disabled
616+
this.apolloServer.installSubscriptionHandlers(this.server);
617+
}
618+
614619
this.graphqlSchema = schema;
615620

616621
this.buildLoaderOptionMap(services); // rebuild the options for DataLoaders

test/unit/service.spec.js

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,44 @@ describe("Test Service", () => {
13281328

13291329
GraphQL.printSchema.mockImplementation(() => "printed schema");
13301330

1331+
const services = [
1332+
{
1333+
name: "test-svc-1",
1334+
actions: [
1335+
{
1336+
name: "test-action-1",
1337+
graphql: {
1338+
dataLoaderOptions: { option1: "option-value-1" },
1339+
dataLoaderBatchParam: "batch-param-1",
1340+
},
1341+
},
1342+
{ name: "test-action-2" },
1343+
],
1344+
},
1345+
{
1346+
name: "test-svc-2",
1347+
version: 1,
1348+
actions: [
1349+
{
1350+
name: "test-action-3",
1351+
graphql: {
1352+
dataLoaderOptions: { option2: "option-value-2" },
1353+
dataLoaderBatchParam: "batch-param-2",
1354+
},
1355+
},
1356+
{ name: "test-action-4" },
1357+
],
1358+
},
1359+
];
1360+
1361+
beforeEach(() => {
1362+
createHandler.mockClear();
1363+
installSubscriptionHandlers.mockClear();
1364+
1365+
ApolloServer.mockClear();
1366+
GraphQL.printSchema.mockClear();
1367+
});
1368+
13311369
it("should create local variables", async () => {
13321370
const { broker, svc, stop } = await startService({
13331371
serverOptions: {
@@ -1338,35 +1376,6 @@ describe("Test Service", () => {
13381376

13391377
svc.server = "server";
13401378
broker.broadcast = jest.fn();
1341-
const services = [
1342-
{
1343-
name: "test-svc-1",
1344-
actions: [
1345-
{
1346-
name: "test-action-1",
1347-
graphql: {
1348-
dataLoaderOptions: { option1: "option-value-1" },
1349-
dataLoaderBatchParam: "batch-param-1",
1350-
},
1351-
},
1352-
{ name: "test-action-2" },
1353-
],
1354-
},
1355-
{
1356-
name: "test-svc-2",
1357-
version: 1,
1358-
actions: [
1359-
{
1360-
name: "test-action-3",
1361-
graphql: {
1362-
dataLoaderOptions: { option2: "option-value-2" },
1363-
dataLoaderBatchParam: "batch-param-2",
1364-
},
1365-
},
1366-
{ name: "test-action-4" },
1367-
],
1368-
},
1369-
];
13701379
broker.registry.getServiceList = jest.fn(() => services);
13711380
svc.generateGraphQLSchema = jest.fn(() => "graphql schema");
13721381

@@ -1482,5 +1491,38 @@ describe("Test Service", () => {
14821491

14831492
await stop();
14841493
});
1494+
1495+
it("Should avoid binding apollo subscription handlers if the server config has them disabled", async () => {
1496+
const { broker, svc, stop } = await startService({
1497+
serverOptions: {
1498+
path: "/my-graphql",
1499+
subscriptions: false,
1500+
},
1501+
});
1502+
1503+
svc.server = "server";
1504+
broker.broadcast = jest.fn();
1505+
1506+
broker.registry.getServiceList = jest.fn(() => services);
1507+
svc.generateGraphQLSchema = jest.fn(() => "graphql schema");
1508+
1509+
expect(svc.pubsub).toBeNull();
1510+
expect(svc.apolloServer).toBeNull();
1511+
expect(svc.graphqlHandler).toBeNull();
1512+
expect(svc.graphqlSchema).toBeNull();
1513+
expect(svc.shouldUpdateGraphqlSchema).toBe(true);
1514+
1515+
svc.prepareGraphQLSchema();
1516+
1517+
expect(installSubscriptionHandlers).not.toHaveBeenCalled();
1518+
1519+
expect(svc.generateGraphQLSchema).toBeCalledTimes(1);
1520+
expect(svc.generateGraphQLSchema).toBeCalledWith(services);
1521+
1522+
expect(svc.shouldUpdateGraphqlSchema).toBe(false);
1523+
expect(svc.graphqlSchema).toBe("graphql schema");
1524+
1525+
await stop();
1526+
});
14851527
});
14861528
});

0 commit comments

Comments
 (0)