diff --git a/package.json b/package.json index 666fc0f..69ec35e 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "graphiql-subscriptions-fetcher", - "version": "0.0.2", + "version": "0.0.4", "main": "dist/index.js", "repository": "https://github.com/apollographql/GraphiQL-Subscriptions-Fetcher.git", "author": "Urigo ", "license": "MIT", "dependencies": { - "graphql": "^0.9.1", - "subscriptions-transport-ws": "0.5.4" + "graphql": "0.13.2", + "subscriptions-transport-ws": "0.9.14" }, "scripts": { "compile": "tsc", @@ -20,11 +20,11 @@ "prepublish": "yarn compile && yarn browser-compile" }, "devDependencies": { - "@types/graphql": "^0.8.6", + "@types/graphql": "0.13.2", "@types/jest": "^19.2.2", "jest": "^19.0.2", "tslint": "^4.5.1", - "typescript": "^2.2.1", + "typescript": "^2.9.1", "webpack": "^2.3.2" }, "jest": { diff --git a/src/fetcher.ts b/src/fetcher.ts index 1b22cde..52defcc 100644 --- a/src/fetcher.ts +++ b/src/fetcher.ts @@ -17,32 +17,35 @@ const hasSubscriptionOperation = (graphQlParams: any) => { }; export const graphQLFetcher = (subscriptionsClient: SubscriptionClient, fallbackFetcher: Function) => { - let activeSubscriptionId: number | null = null; + let activeSubscription = false; return (graphQLParams: any) => { - if (subscriptionsClient && activeSubscriptionId !== null) { - subscriptionsClient.unsubscribe(activeSubscriptionId); + if (!subscriptionsClient) { + return fallbackFetcher(graphQLParams); + } + + if (activeSubscription) { + subscriptionsClient.unsubscribeAll(); + } + + const subscriptionRequest = () => { + activeSubscription = true; + + return subscriptionsClient.request({ + query: graphQLParams.query, + variables: graphQLParams.variables, + operationName: graphQLParams.operationName, + }); + }; + + if (hasSubscriptionOperation(graphQLParams)) { + return subscriptionRequest(); } - if (subscriptionsClient && hasSubscriptionOperation(graphQLParams)) { - return { - subscribe: (observer: { error: Function, next: Function }) => { - observer.next('Your subscription data will appear here after server publication!'); - - activeSubscriptionId = subscriptionsClient.subscribe({ - query: graphQLParams.query, - variables: graphQLParams.variables, - }, function (error, result) { - if (error) { - observer.error(error); - } else { - observer.next(result); - } - }); - }, - }; - } else { + if (fallbackFetcher) { return fallbackFetcher(graphQLParams); } + + return subscriptionRequest(); }; }; diff --git a/tests/fetcher.spec.ts b/tests/fetcher.spec.ts index fba80a6..efe85be 100644 --- a/tests/fetcher.spec.ts +++ b/tests/fetcher.spec.ts @@ -7,45 +7,45 @@ describe('GraphiQL Fetcher', () => { beforeEach(() => { subscriptionsClient = { - subscribe: jest.fn(), - unsubscribe: jest.fn(), + request: jest.fn(), + unsubscribeAll: jest.fn(), }; fallbackFetcher = jest.fn(); fetcher = graphQLFetcher(subscriptionsClient, fallbackFetcher); }); it('should use subscriptions fetcher when using with named operation', () => { - const ret = fetcher({ + fetcher({ query: 'subscription commentAdded { field }', operationName: 'commentAdded', }); - expect(ret.subscribe).not.toBe(undefined); + expect(subscriptionsClient.request.mock.calls.length).toBe(1); }); it('should use subscriptions fetcher when using with named operation with fragments', () => { - const ret = fetcher({ + fetcher({ query: 'fragment f on MyType { field } subscription commentAdded { ...f }', operationName: 'commentAdded', }); - expect(ret.subscribe).not.toBe(undefined); + expect(subscriptionsClient.request.mock.calls.length).toBe(1); }); it('should use subscriptions fetcher when using with operation with fragments', () => { - const ret = fetcher({ + fetcher({ query: 'fragment f on MyType { field } subscription { ...f }', }); - expect(ret.subscribe).not.toBe(undefined); + expect(subscriptionsClient.request.mock.calls.length).toBe(1); }); it('should use subscriptions fetcher when using with operation', () => { - const ret = fetcher({ + fetcher({ query: 'subscription { ...f }', }); - expect(ret.subscribe).not.toBe(undefined); + expect(subscriptionsClient.request.mock.calls.length).toBe(1); }); it('should use fallback fetcher when using query', () => { @@ -63,4 +63,34 @@ describe('GraphiQL Fetcher', () => { expect(fallbackFetcher.mock.calls.length).toBe(1); }); + + describe('with no fallbackFetcher', () => { + beforeEach(() => { + fetcher = graphQLFetcher(subscriptionsClient); + }); + + it('should use subscriptions fetcher when using with operation', () => { + fetcher({ + query: 'subscription { ...f }', + }); + + expect(subscriptionsClient.request.mock.calls.length).toBe(1); + }); + + it('should use subscriptions fetcher when using query', () => { + fetcher({ + query: 'query { field }', + }); + + expect(subscriptionsClient.request.mock.calls.length).toBe(1); + }); + + it('should use subscriptions fetcher when using mutations', () => { + fetcher({ + query: 'mutation { field }', + }); + + expect(subscriptionsClient.request.mock.calls.length).toBe(1); + }); + }); }); diff --git a/yarn.lock b/yarn.lock index c192e78..1430ed8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,24 +2,14 @@ # yarn lockfile v1 -"@types/graphql@^0.8.6": - version "0.8.6" - resolved "https://registry.yarnpkg.com/@types/graphql/-/graphql-0.8.6.tgz#b34fb880493ba835b0c067024ee70130d6f9bb68" +"@types/graphql@0.13.2": + version "0.13.2" + resolved "https://registry.yarnpkg.com/@types/graphql/-/graphql-0.13.2.tgz#cb7d01f7c543541514eb3e192576cc04d305c363" "@types/jest@^19.2.2": version "19.2.2" resolved "https://registry.yarnpkg.com/@types/jest/-/jest-19.2.2.tgz#71f428be2fa6eb9f15bb0abc3cade67905f94839" -"@types/node@*": - version "7.0.11" - resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.11.tgz#55680189f2335f080f0aeb57871f0b9823646d89" - -"@types/ws@0.0.38": - version "0.0.38" - resolved "https://registry.yarnpkg.com/@types/ws/-/ws-0.0.38.tgz#42106fff4b422ca956734e29f0d73a6d893194d3" - dependencies: - "@types/node" "*" - abab@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/abab/-/abab-1.0.3.tgz#b81de5f7274ec4e756d797cd834f303642724e5d" @@ -173,6 +163,10 @@ async-each@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d" +async-limiter@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8" + async@^1.4.0, async@^1.4.2: version "1.5.2" resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" @@ -855,10 +849,6 @@ error-ex@^1.2.0: dependencies: is-arrayish "^0.2.1" -es6-promise@^3.2.1: - version "3.3.1" - resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-3.3.1.tgz#a08cdde84ccdbf34d027a1451bc91d4bcd28a613" - escape-string-regexp@^1.0.2: version "1.0.5" resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" @@ -890,9 +880,9 @@ esutils@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b" -eventemitter3@^2.0.2: - version "2.0.2" - resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-2.0.2.tgz#20ce4891909ce9f35b088c94fab40e2c96f473ac" +eventemitter3@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.0.tgz#090b4d6cdbd645ed10bf750d4b5407942d7ba163" events@^1.0.0: version "1.1.1" @@ -1138,21 +1128,11 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6: version "4.1.11" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658" -graphql-subscriptions@^0.3.0: - version "0.3.1" - resolved "https://registry.yarnpkg.com/graphql-subscriptions/-/graphql-subscriptions-0.3.1.tgz#0cedc2d507420cf26cf414080b079f05402f0303" +graphql@0.13.2: + version "0.13.2" + resolved "http://registry.npmjs.org/graphql/-/graphql-0.13.2.tgz#4c740ae3c222823e7004096f832e7b93b2108270" dependencies: - es6-promise "^3.2.1" - -graphql-tag@^1.2.4: - version "1.3.2" - resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-1.3.2.tgz#7abb3a8fd9f3415d07163314ed237061c785b759" - -graphql@^0.9.1: - version "0.9.1" - resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.9.1.tgz#f4d154cbec054d4a5d3b1be95f23435c09aa86c8" - dependencies: - iterall "1.0.3" + iterall "^1.2.1" growly@^1.3.0: version "1.3.0" @@ -1488,9 +1468,9 @@ istanbul-reports@^1.0.0: dependencies: handlebars "^4.0.3" -iterall@1.0.3: - version "1.0.3" - resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.0.3.tgz#e0b31958f835013c323ff0b10943829ac69aa4b7" +iterall@^1.2.1: + version "1.2.2" + resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.2.2.tgz#92d70deb8028e0c39ff3164fdbf4d8b088130cd7" jest-changed-files@^19.0.2: version "19.0.2" @@ -1845,14 +1825,6 @@ locate-path@^2.0.0: p-locate "^2.0.0" path-exists "^3.0.0" -lodash.isobject@^3.0.2: - version "3.0.2" - resolved "https://registry.yarnpkg.com/lodash.isobject/-/lodash.isobject-3.0.2.tgz#3c8fb8d5b5bf4bf90ae06e14f2a530a4ed935e1d" - -lodash.isstring@^4.0.1: - version "4.0.1" - resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451" - lodash@^4.14.0, lodash@^4.2.0: version "4.17.4" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae" @@ -2107,10 +2079,6 @@ optionator@^0.8.1: type-check "~0.3.2" wordwrap "~1.0.0" -options@>=0.0.5: - version "0.0.6" - resolved "https://registry.yarnpkg.com/options/-/options-0.0.6.tgz#ec22d312806bb53e731773e7cdaefcf1c643128f" - os-browserify@^0.2.0: version "0.2.1" resolved "https://registry.yarnpkg.com/os-browserify/-/os-browserify-0.2.1.tgz#63fc4ccee5d2d7763d26bbf8601078e6c2e0044f" @@ -2667,18 +2635,15 @@ strip-json-comments@~2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" -subscriptions-transport-ws@0.5.4: - version "0.5.4" - resolved "https://registry.yarnpkg.com/subscriptions-transport-ws/-/subscriptions-transport-ws-0.5.4.tgz#1091e55e73bff0d691a86570ac2b97ff6fb35cfc" +subscriptions-transport-ws@0.9.14: + version "0.9.14" + resolved "https://registry.yarnpkg.com/subscriptions-transport-ws/-/subscriptions-transport-ws-0.9.14.tgz#a39e08edba89ee4cfd95f30484c55d865f5d8451" dependencies: - "@types/ws" "0.0.38" backo2 "^1.0.2" - eventemitter3 "^2.0.2" - graphql-subscriptions "^0.3.0" - graphql-tag "^1.2.4" - lodash.isobject "^3.0.2" - lodash.isstring "^4.0.1" - ws "^1.1.0" + eventemitter3 "^3.1.0" + iterall "^1.2.1" + symbol-observable "^1.0.4" + ws "^5.2.0" supports-color@^2.0.0: version "2.0.0" @@ -2690,6 +2655,10 @@ supports-color@^3.1.0, supports-color@^3.1.2: dependencies: has-flag "^1.0.0" +symbol-observable@^1.0.4: + version "1.2.0" + resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804" + symbol-tree@^3.2.1: version "3.2.2" resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.2.tgz#ae27db38f660a7ae2e1c3b7d1bc290819b8519e6" @@ -2813,9 +2782,9 @@ type-check@~0.3.2: dependencies: prelude-ls "~1.1.2" -typescript@^2.2.1: - version "2.2.1" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.2.1.tgz#4862b662b988a4c8ff691cc7969622d24db76ae9" +typescript@^2.9.1: + version "2.9.2" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.2.tgz#1cbf61d05d6b96269244eb6a3bce4bd914e0f00c" uglify-js@^2.6, uglify-js@^2.8.5: version "2.8.16" @@ -2834,10 +2803,6 @@ uid-number@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81" -ultron@1.0.x: - version "1.0.2" - resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.0.2.tgz#ace116ab557cd197386a4e88f4685378c8b2e4fa" - unique-string@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-1.0.0.tgz#9e1057cca851abb93398f8b33ae187b99caec11a" @@ -3038,12 +3003,11 @@ write-file-atomic@^1.1.2: imurmurhash "^0.1.4" slide "^1.1.5" -ws@^1.1.0: - version "1.1.4" - resolved "https://registry.yarnpkg.com/ws/-/ws-1.1.4.tgz#57f40d036832e5f5055662a397c4de76ed66bf61" +ws@^5.2.0: + version "5.2.2" + resolved "https://registry.yarnpkg.com/ws/-/ws-5.2.2.tgz#dffef14866b8e8dc9133582514d1befaf96e980f" dependencies: - options ">=0.0.5" - ultron "1.0.x" + async-limiter "~1.0.0" xdg-basedir@^3.0.0: version "3.0.0"