Skip to content

Commit

Permalink
Merge pull request #44 from kbrandwijk/feature/errorlogging
Browse files Browse the repository at this point in the history
fix(link): error logging restored using apollo-link-error
  • Loading branch information
schickling authored Jan 4, 2018
2 parents 69c31ff + 8397f5a commit effb9b5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
42 changes: 17 additions & 25 deletions src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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) => {
Expand All @@ -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])
}
}

Expand Down
10 changes: 8 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ ansi-styles@^3.1.0:
dependencies:
color-convert "^1.9.0"

[email protected]:
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"

[email protected]:
version "1.3.2"
resolved "https://registry.yarnpkg.com/apollo-link-http/-/apollo-link-http-1.3.2.tgz#63537ee5ecf9c004efb0317f1222b7dbc6f21559"
Expand All @@ -54,7 +60,7 @@ apollo-link-ws@^1.0.4:
dependencies:
apollo-link "^1.0.7"

[email protected], apollo-link@^1.0.0, apollo-link@^1.0.7:
[email protected], 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:
Expand Down Expand Up @@ -420,7 +426,7 @@ strip-ansi@^3.0.0:
dependencies:
ansi-regex "^2.0.0"

subscriptions-transport-ws@^0.9.4:
[email protected]:
version "0.9.4"
resolved "https://registry.yarnpkg.com/subscriptions-transport-ws/-/subscriptions-transport-ws-0.9.4.tgz#2671c7339c17389c0ff47c73cd749949576d3dd5"
dependencies:
Expand Down

0 comments on commit effb9b5

Please sign in to comment.