diff --git a/package.json b/package.json index ed8be54f..a493221c 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ }, "dependencies": { "apollo-link": "1.0.7", + "apollo-link-error": "1.0.3", "apollo-link-http": "1.3.2", "apollo-link-ws": "^1.0.4", "cross-fetch": "^1.1.1", @@ -35,7 +36,7 @@ "graphql-import": "0.1.9", "graphql-tools": "2.16.0", "jsonwebtoken": "^8.1.0", - "subscriptions-transport-ws": "^0.9.4" + "subscriptions-transport-ws": "0.9.4" }, "devDependencies": { "@types/graphql": "0.11.7", diff --git a/src/link.ts b/src/link.ts index 59ef28ff..0024fc39 100644 --- a/src/link.ts +++ b/src/link.ts @@ -3,7 +3,7 @@ import * as fetch from 'cross-fetch' import { print, OperationDefinitionNode } from 'graphql' import { ApolloLink, Operation, split } from 'apollo-link' import { WebSocketLink } from 'apollo-link-ws' -import { SubscriptionClient } from 'subscriptions-transport-ws' +import { onError } from 'apollo-link-error' import * as ws from 'ws' export function makeGraphcoolLink({ @@ -23,31 +23,23 @@ export function makeGraphcoolLink({ // also works for https/wss const wsEndpoint = endpoint.replace(/^http/, 'ws') - const subscriptionClient = new SubscriptionClient( - wsEndpoint, - { reconnect: true }, - ws, - ) - const wsLink = new WebSocketLink(subscriptionClient) + const wsLink = new WebSocketLink({ + uri: wsEndpoint, + options: { reconnect: true}, + webSocketImpl: ws + }) const backendLink = split(op => isSubscription(op), wsLink, httpLink) - /* TODO: Fix this: this is causing duplicate requests - const reportErrors = new ApolloLink((operation, forward) => { - const observer = forward!(operation) - observer.subscribe({ - error: err => { - console.log(err) - - // unfortunately throwing errors in links doesn't work yet - // current workaround: console log - - // throw err - }, - }) - return observer - }) - */ + const reportErrors = onError(({ graphQLErrors, networkError }) => { + if (graphQLErrors) + graphQLErrors.map(({ message, locations, path }) => + console.log( + `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}` + ) + ); + if (networkError) console.log(`[Network error]: ${networkError}`); + }); if (debug) { const debugLink = new ApolloLink((operation, forward) => { @@ -65,9 +57,9 @@ export function makeGraphcoolLink({ }) }) - return ApolloLink.from([debugLink/*, reportErrors*/, backendLink]) + return ApolloLink.from([debugLink, reportErrors, backendLink]) } else { - return ApolloLink.from([/*reportErrors, */backendLink]) + return ApolloLink.from([reportErrors, backendLink]) } } diff --git a/yarn.lock b/yarn.lock index d6ea417d..be40c8be 100644 --- a/yarn.lock +++ b/yarn.lock @@ -42,6 +42,12 @@ ansi-styles@^3.1.0: dependencies: color-convert "^1.9.0" +apollo-link-error@1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/apollo-link-error/-/apollo-link-error-1.0.3.tgz#2c679d2e6a2df09a9ae3f70d23c64af922a801a2" + dependencies: + apollo-link "^1.0.6" + apollo-link-http@1.3.2: version "1.3.2" resolved "https://registry.yarnpkg.com/apollo-link-http/-/apollo-link-http-1.3.2.tgz#63537ee5ecf9c004efb0317f1222b7dbc6f21559" @@ -54,7 +60,7 @@ apollo-link-ws@^1.0.4: dependencies: apollo-link "^1.0.7" -apollo-link@1.0.7, apollo-link@^1.0.0, apollo-link@^1.0.7: +apollo-link@1.0.7, apollo-link@^1.0.0, apollo-link@^1.0.6, apollo-link@^1.0.7: version "1.0.7" resolved "https://registry.yarnpkg.com/apollo-link/-/apollo-link-1.0.7.tgz#42cd38a7378332fc3e41a214ff6a6e5e703a556f" dependencies: @@ -420,7 +426,7 @@ strip-ansi@^3.0.0: dependencies: ansi-regex "^2.0.0" -subscriptions-transport-ws@^0.9.4: +subscriptions-transport-ws@0.9.4: version "0.9.4" resolved "https://registry.yarnpkg.com/subscriptions-transport-ws/-/subscriptions-transport-ws-0.9.4.tgz#2671c7339c17389c0ff47c73cd749949576d3dd5" dependencies: