Skip to content

Commit

Permalink
Works with no config
Browse files Browse the repository at this point in the history
  • Loading branch information
peggyrayzis committed Feb 14, 2018
1 parent 76afb36 commit e44a82d
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions packages/apollo-client-preset/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,51 @@ export default class DefaultClient<
constructor(config: PresetConfig) {
const cache = new InMemoryCache();

const stateLink = config.clientState
? withClientState({ ...config.clientState, cache })
: false;
if (config) {
const stateLink = config.clientState
? withClientState({ ...config.clientState, cache })
: false;

const errorLink = config.onError
? onError(config.onError)
: 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}`);
});
const errorLink = config.onError
? onError(config.onError)
: 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}`);
});

const requestHandler = config.request
? new ApolloLink((operation, forward) => {
const { ...request } = operation;
const requestHandler = config.request
? new ApolloLink((operation, forward) => {
const { ...request } = operation;

return new Observable(observer => {
let handle: any;
Promise.resolve(request)
.then(req => config.request(req))
.then(() => {
handle = forward(operation).subscribe({
next: observer.next.bind(observer),
error: observer.error.bind(observer),
complete: observer.complete.bind(observer),
});
})
.catch(observer.error.bind(observer));
return new Observable(observer => {
let handle: any;
Promise.resolve(request)
.then(req => config.request(req))
.then(() => {
handle = forward(operation).subscribe({
next: observer.next.bind(observer),
error: observer.error.bind(observer),
complete: observer.complete.bind(observer),
});
})
.catch(observer.error.bind(observer));

return () => {
if (handle) handle.unsubscribe;
};
});
})
: false;
return () => {
if (handle) handle.unsubscribe;
};
});
})
: false;
}

const httpLink = new HttpLink({
uri: config.uri || '/graphql',
fetchOptions: config.fetchOptions || {},
uri: (config && config.uri) || '/graphql',
fetchOptions: (config && config.fetchOptions) || {},
credentials: 'same-origin',
});

Expand Down

0 comments on commit e44a82d

Please sign in to comment.