From 8bd10e31a136ec40905c974d7f6f953d0149e6b6 Mon Sep 17 00:00:00 2001 From: Binbin Ye Date: Thu, 4 Jun 2020 09:35:59 +0900 Subject: [PATCH 1/4] Fix server type check --- packages/apollo-server-core/src/ApolloServer.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/apollo-server-core/src/ApolloServer.ts b/packages/apollo-server-core/src/ApolloServer.ts index bdd93df6908..1b89ce4d400 100644 --- a/packages/apollo-server-core/src/ApolloServer.ts +++ b/packages/apollo-server-core/src/ApolloServer.ts @@ -678,12 +678,12 @@ export class ApolloServerBase { keepAlive, validationRules: this.requestOptions.validationRules }, - server instanceof WebSocket.Server - ? server - : { - server, - path, - }, + server instanceof HttpServer + ? { + server, + path, + } + : server ); } From 5b80ba1e6cc9536dac65f1f6a3e4de74cf6e9b45 Mon Sep 17 00:00:00 2001 From: Binbin Date: Tue, 9 Jun 2020 09:23:29 +0900 Subject: [PATCH 2/4] Add more types to installSubscriptionHandlers --- packages/apollo-server-core/src/ApolloServer.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/apollo-server-core/src/ApolloServer.ts b/packages/apollo-server-core/src/ApolloServer.ts index 1b89ce4d400..c903c9722ee 100644 --- a/packages/apollo-server-core/src/ApolloServer.ts +++ b/packages/apollo-server-core/src/ApolloServer.ts @@ -3,7 +3,11 @@ import { addMockFunctionsToSchema, GraphQLParseOptions, } from 'graphql-tools'; +import { Server as NetServer } from 'net' +import { Server as TlsServer } from 'tls' import { Server as HttpServer } from 'http'; +import { Http2Server, Http2SecureServer } from 'http2'; +import { Server as HttpsServer } from 'https'; import loglevel from 'loglevel'; import { execute, @@ -602,7 +606,7 @@ export class ApolloServerBase { } } - public installSubscriptionHandlers(server: HttpServer | WebSocket.Server) { + public installSubscriptionHandlers(server: HttpServer | HttpsServer | Http2Server | Http2SecureServer | WebSocket.Server) { if (!this.subscriptionServerOptions) { if (this.config.gateway) { throw Error( @@ -678,7 +682,7 @@ export class ApolloServerBase { keepAlive, validationRules: this.requestOptions.validationRules }, - server instanceof HttpServer + server instanceof NetServer || server instanceof TlsServer ? { server, path, From e104f0376e35f5b882e8d0a090a0f32e836f4b0e Mon Sep 17 00:00:00 2001 From: Binbin Date: Fri, 10 Jul 2020 14:51:28 +0900 Subject: [PATCH 3/4] Add tset for passing websocket server to subscription handlers --- .../src/ApolloServer.ts | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/packages/apollo-server-integration-testsuite/src/ApolloServer.ts b/packages/apollo-server-integration-testsuite/src/ApolloServer.ts index bfcb8cfa4ff..bcc0caf3996 100644 --- a/packages/apollo-server-integration-testsuite/src/ApolloServer.ts +++ b/packages/apollo-server-integration-testsuite/src/ApolloServer.ts @@ -2041,6 +2041,87 @@ export function testApolloServer( }) .catch(done.fail); }); + + it('takes websocket server subscriptions configuration', done => { + const onConnect = jest.fn(connectionParams => ({ + ...connectionParams, + })); + const typeDefs = gql` + type Query { + hi: String + } + + type Subscription { + num: Int + } + `; + + const query = ` + subscription { + num + } + `; + + const resolvers = { + Query: { + hi: () => 'here to placate graphql-js', + }, + Subscription: { + num: { + subscribe: () => { + createEvent(1); + createEvent(2); + createEvent(3); + return pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC); + }, + }, + }, + }; + + const path = '/sub'; + createApolloServer({ + typeDefs, + resolvers, + subscriptions: { onConnect, path }, + }) + .then(({ port, server }) => { + const subPort = (typeof port === "number" ? port : parseInt(port)) + 1 + const websocketServer = new WebSocket.Server({port: subPort}) + server.installSubscriptionHandlers(websocketServer); + expect(onConnect).not.toBeCalled(); + + expect(server.subscriptionsPath).toEqual(path); + const client = new SubscriptionClient( + `ws://localhost:${subPort}${server.subscriptionsPath}`, + {}, + WebSocket, + ); + + const observable = client.request({ query }); + + let i = 1; + subscription = observable.subscribe({ + next: ({ data }) => { + try { + expect(onConnect).toHaveBeenCalledTimes(1); + expect(data.num).toEqual(i); + if (i === 3) { + done(); + } + i++; + } catch (e) { + done.fail(e); + } + }, + error: done.fail, + complete: () => { + done.fail(new Error('should not complete')); + }, + }); + }) + .catch(done.fail); + }); + it('allows introspection when introspection is enabled on ApolloServer', done => { const typeDefs = gql` type Query { From dc3970415bca55aae7f016a04d9b5fe16bec0eb4 Mon Sep 17 00:00:00 2001 From: Jesse Rosenberger Date: Thu, 13 Aug 2020 12:32:50 +0000 Subject: [PATCH 4/4] Add CHANGELOG.md for #4200. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a284b35e79..8503b9ffd26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ The version headers in this history reflect the versions of Apollo Server itself > The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. With few exceptions, the format of the entry should follow convention (i.e., prefix with package name, use markdown `backtick formatting` for package names and code, suffix with a link to the change-set à la `[PR #YYY](https://link/pull/YYY)`, etc.). When a release is being prepared, a new header will be (manually) created below and the appropriate changes within that release will be moved into the new section. -- _Nothing yet! Stay tuned!_ +- subscriptions: Fix bug which prevented `installSubscriptionHandlers` from accepting a `websocket.Server` (as intended in [PR #1966](https://github.com/apollographql/apollo-server/pull/1966)) and also added support for other `http.Server` variations (e.g., Tls). [Issue #4198](https://github.com/apollographql/apollo-server/issues/4198) [PR #4200](https://github.com/apollographql/apollo-server/pull/4200) ## v2.16.1