Skip to content

Commit

Permalink
Merge pull request #76 from Kauabunga/fix/avoidSubscriptionsInstallWh…
Browse files Browse the repository at this point in the history
…enDisabled

[fix issue #64] No longer installing subscription handlers when disabled
  • Loading branch information
icebob authored May 29, 2020
2 parents 9ad31d1 + 2aff6a1 commit 73f9101
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 30 deletions.
7 changes: 6 additions & 1 deletion src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,12 @@ module.exports = function(mixinOptions) {
this.graphqlHandler = this.apolloServer.createHandler(
mixinOptions.serverOptions
);
this.apolloServer.installSubscriptionHandlers(this.server);

if (mixinOptions.serverOptions.subscriptions !== false) {
// Avoid installing the subscription handlers if they have been disabled
this.apolloServer.installSubscriptionHandlers(this.server);
}

this.graphqlSchema = schema;

this.buildLoaderOptionMap(services); // rebuild the options for DataLoaders
Expand Down
100 changes: 71 additions & 29 deletions test/unit/service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,44 @@ describe("Test Service", () => {

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

const services = [
{
name: "test-svc-1",
actions: [
{
name: "test-action-1",
graphql: {
dataLoaderOptions: { option1: "option-value-1" },
dataLoaderBatchParam: "batch-param-1",
},
},
{ name: "test-action-2" },
],
},
{
name: "test-svc-2",
version: 1,
actions: [
{
name: "test-action-3",
graphql: {
dataLoaderOptions: { option2: "option-value-2" },
dataLoaderBatchParam: "batch-param-2",
},
},
{ name: "test-action-4" },
],
},
];

beforeEach(() => {
createHandler.mockClear();
installSubscriptionHandlers.mockClear();

ApolloServer.mockClear();
GraphQL.printSchema.mockClear();
});

it("should create local variables", async () => {
const { broker, svc, stop } = await startService({
serverOptions: {
Expand All @@ -1338,35 +1376,6 @@ describe("Test Service", () => {

svc.server = "server";
broker.broadcast = jest.fn();
const services = [
{
name: "test-svc-1",
actions: [
{
name: "test-action-1",
graphql: {
dataLoaderOptions: { option1: "option-value-1" },
dataLoaderBatchParam: "batch-param-1",
},
},
{ name: "test-action-2" },
],
},
{
name: "test-svc-2",
version: 1,
actions: [
{
name: "test-action-3",
graphql: {
dataLoaderOptions: { option2: "option-value-2" },
dataLoaderBatchParam: "batch-param-2",
},
},
{ name: "test-action-4" },
],
},
];
broker.registry.getServiceList = jest.fn(() => services);
svc.generateGraphQLSchema = jest.fn(() => "graphql schema");

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

await stop();
});

it("Should avoid binding apollo subscription handlers if the server config has them disabled", async () => {
const { broker, svc, stop } = await startService({
serverOptions: {
path: "/my-graphql",
subscriptions: false,
},
});

svc.server = "server";
broker.broadcast = jest.fn();

broker.registry.getServiceList = jest.fn(() => services);
svc.generateGraphQLSchema = jest.fn(() => "graphql schema");

expect(svc.pubsub).toBeNull();
expect(svc.apolloServer).toBeNull();
expect(svc.graphqlHandler).toBeNull();
expect(svc.graphqlSchema).toBeNull();
expect(svc.shouldUpdateGraphqlSchema).toBe(true);

svc.prepareGraphQLSchema();

expect(installSubscriptionHandlers).not.toHaveBeenCalled();

expect(svc.generateGraphQLSchema).toBeCalledTimes(1);
expect(svc.generateGraphQLSchema).toBeCalledWith(services);

expect(svc.shouldUpdateGraphqlSchema).toBe(false);
expect(svc.graphqlSchema).toBe("graphql schema");

await stop();
});
});
});

0 comments on commit 73f9101

Please sign in to comment.