Skip to content
This repository has been archived by the owner on Aug 22, 2020. It is now read-only.

Commit

Permalink
fix(ApolloClient): no default error logging; examples for overriding …
Browse files Browse the repository at this point in the history
…static methods
  • Loading branch information
thealjey committed Mar 27, 2019
1 parent 01d90b0 commit d1b297f
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 177 deletions.
152 changes: 121 additions & 31 deletions ApolloClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,9 @@ const { WebSocketLink } = require('apollo-link-ws')
const { getMainDefinition } = require('apollo-utilities')
const { InMemoryCache } = require('apollo-cache-inmemory')
const compact = require('lodash/compact')
const noop = require('lodash/noop')
const { NativeWebSocket, isNode } = require('./constants')

const defaultWSOptions = { reconnect: true }

const testOperation = ({ query }) => {
const { kind, operation } = getMainDefinition(query)

return kind === 'OperationDefinition' && operation === 'subscription'
}

const defaultErrorCallback = ({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
graphQLErrors.map(({ message: m, locations: l, path: p }) => console.log(
`[GraphQL error]: Message: ${m}, Location: ${l}, Path: ${p}`
))
}
if (networkError) {
console.log(`[Network error]: ${networkError}`)
}
}

/*::
import ApolloLink from 'apollo-link'
import { ApolloCache } from 'apollo-cache'
Expand Down Expand Up @@ -151,31 +133,81 @@ class ApolloClient extends Client {
})
}

/** creates the final instance of ApolloLink */
static testOperation ({ query }/*: Object */) {
const { kind, operation } = getMainDefinition(query)

return kind === 'OperationDefinition' && operation === 'subscription'
}

/**
* creates the final instance of ApolloLink
*
* @example
* // override to a default behaviour (will have no effect)
* import { split } from 'apollo-link'
*
* ApolloClient.createLink = options => {
* const wsLink = ApolloClient.createWSLink(options)
* const httpLink = ApolloClient.createHttpLink(options)
*
* return wsLink ? split(ApolloClient.testOperation, wsLink, httpLink) : httpLink
* }
*/
static createLink (options/*: ApolloClientOptions */)/*: ApolloLink */ {
const httpLink = ApolloClient.createHttpLink(options)
const wsLink = ApolloClient.createWSLink(options)
const httpLink = ApolloClient.createHttpLink(options)

return wsLink ? split(testOperation, wsLink, httpLink) : httpLink
return wsLink ? split(ApolloClient.testOperation, wsLink, httpLink) : httpLink
}

/** creates an instance of ApolloCache */
/**
* creates an instance of ApolloCache
*
* @example
* // override to a default behaviour (will have no effect)
* import { InMemoryCache } from 'apollo-cache-inmemory'
*
* ApolloClient.createCache = ({ cache = {} }) => new InMemoryCache().restore(cache)
*/
static createCache (options/*: ApolloClientOptions */)/*: ApolloCache */ {
const { cache = {} } = options

return new InMemoryCache().restore(cache)
}

/** creates an instance of ApolloLink */
/**
* creates an instance of ApolloLink
*
* @example
* // override to a default behaviour (will have no effect)
* import { from } from 'apollo-link'
* import compact from 'lodash/compact'
*
* ApolloClient.createHttpLink = options => from(compact(ApolloClient.getLinks(options)))
*/
static createHttpLink (options/*: ApolloClientOptions */)/*: ApolloLink */ {
return from(compact(ApolloClient.getLinks(options)))
}

/** creates an instance of WebSocketLink */
/**
* creates an instance of WebSocketLink
*
* @example
* // override to a default behaviour (will have no effect)
* import { NativeWebSocket } from 'secondwheel/constants'
* import { WebSocketLink } from 'apollo-link-ws'
*
* ApolloClient.createWSLink = ({ wsUri, wsOptions = { reconnect: true }, webSocketImpl = NativeWebSocket }) =>
* wsUri && webSocketImpl && new WebSocketLink({
* uri: wsUri,
* options: wsOptions,
* webSocketImpl
* })
*/
static createWSLink (options/*: ApolloClientOptions */)/*: ?ApolloLink */ {
const { wsUri, wsOptions = defaultWSOptions, webSocketImpl } = options
const { wsUri, wsOptions = { reconnect: true }, webSocketImpl = NativeWebSocket } = options

return wsUri && (webSocketImpl || NativeWebSocket) && new WebSocketLink({
return wsUri && webSocketImpl && new WebSocketLink({
uri: wsUri,
options: wsOptions,
webSocketImpl
Expand All @@ -185,6 +217,14 @@ class ApolloClient extends Client {
/**
* returns an array of ApolloLink to be used in the creation of the final link
* falsy values are filtered out
*
* @example
* // override to a default behaviour (will have no effect)
* ApolloClient.getLinks = options => [
* ApolloClient.createErrorLink(options),
* ApolloClient.createRetryLink(options),
* ApolloClient.createBatchLink(options)
* ]
*/
static getLinks (options/*: ApolloClientOptions */)/*: Array<?ApolloLink> */ {
return [
Expand All @@ -194,21 +234,71 @@ class ApolloClient extends Client {
]
}

/** creates an instance of ErrorLink */
/**
* creates an instance of ErrorLink
*
* @example
* // override to a default behaviour (will have no effect)
* import noop from 'lodash/noop'
* import { onError } from 'apollo-link-error'
*
* ApolloClient.createErrorLink = ({ onError: errorCallback = noop }) => onError(errorCallback)
*/
static createErrorLink (options/*: ApolloClientOptions */)/*: ?ApolloLink */ {
const { onError: errorCallback = defaultErrorCallback } = options
const { onError: errorCallback = noop } = options

return onError(errorCallback)
}

/** creates an instance of RetryLink */
/**
* creates an instance of RetryLink
*
* @example
* // override to a default behaviour (will have no effect)
* import { RetryLink } from 'apollo-link-retry'
*
* ApolloClient.createRetryLink = ({ delay, attempts }) => new RetryLink({ delay, attempts })
*/
static createRetryLink (options/*: ApolloClientOptions */)/*: ?ApolloLink */ {
const { delay, attempts } = options

return new RetryLink({ delay, attempts })
}

/** creates an instance of HttpLink */
/**
* creates an instance of HttpLink
*
* @example
* // override to a default behaviour (will have no effect)
* import { isNode } from 'secondwheel/constants'
* import { BatchHttpLink } from 'apollo-link-batch-http'
*
* ApolloClient.createBatchLink = ({
* uri = '/graphql',
* browserUri = uri,
* serverUri = browserUri,
* includeExtensions,
* headers = {},
* credentials = 'same-origin',
* fetchOptions = {},
* useGETForQueries,
* batchMax,
* batchInterval,
* batchKey
* }) =>
* new BatchHttpLink({
* uri: isNode ? serverUri : browserUri,
* includeExtensions,
* fetch,
* headers,
* credentials,
* fetchOptions,
* useGETForQueries,
* batchMax,
* batchInterval,
* batchKey
* })
*/
static createBatchLink (options/*: ApolloClientOptions */)/*: ?ApolloLink */ {
const {
uri = '/graphql',
Expand Down
Loading

0 comments on commit d1b297f

Please sign in to comment.