Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Throw an error if the client is missing (#84)
Browse files Browse the repository at this point in the history
The current error just states that it can't read initStore
of undefined, which is a pretty cryptic error if you're not
familiar with the internals of ApolloProvider.

This just throws a more friendly error.

Linting fix and test case

Use invariant to throw error
  • Loading branch information
aweary authored and James Baxley committed Jul 4, 2016
1 parent 26e30f2 commit 0ae7c70
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ApolloProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {

import ApolloClient from 'apollo-client';

import invariant = require('invariant');

export declare interface ProviderProps {
store?: Store<any>;
client: ApolloClient;
Expand All @@ -42,6 +44,13 @@ export default class ApolloProvider extends Component<ProviderProps, any> {

constructor(props, context) {
super(props, context);

invariant(
props.client,
'ApolloClient was not passed a client instance. Make ' +
'sure you pass in your client via the "client" prop.'
);

this.client = props.client;

if (props.store) {
Expand Down
13 changes: 13 additions & 0 deletions test/client/ApolloProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ describe('<ApolloProvider /> Component', () => {
expect(wrapper.contains(<div className='unique'/>)).to.equal(true);
});

it('should require a client', () => {
expect(() => {
shallow(
<ApolloProvider client={undefined}>
<div className='unique'/>
</ApolloProvider>
)
}).to.throw(
'ApolloClient was not passed a client instance. Make ' +
'sure you pass in your client via the "client" prop.'
);
})

it('should not require a store', () => {
const wrapper = shallow(
<ApolloProvider client={client}>
Expand Down

0 comments on commit 0ae7c70

Please sign in to comment.