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

Create useApolloClient.ts #2872

Merged
merged 16 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Change log

## 3.0.0

### Improvements

- `useApolloClient` can be used to return an `ApolloClient` instance from
React Apollo's context, assuming it was previously set using
`ApolloProvider`. <br/>
[@FredyC](https://github.com/FredyC) in [#2872](https://github.com/apollographql/react-apollo/pull/2872)


## vNEXT

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion examples/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"eject": "react-scripts eject"
},
"devDependencies": {
"react-testing-library": "6.0.0"
"react-testing-library": "6.0.2"
},
"browserslist": [
">0.2%",
Expand Down
2 changes: 1 addition & 1 deletion examples/mutation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"eject": "react-scripts eject"
},
"devDependencies": {
"react-testing-library": "6.0.0"
"react-testing-library": "6.0.2"
},
"browserslist": [
">0.2%",
Expand Down
23 changes: 11 additions & 12 deletions examples/rollup/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
},
"devDependencies": {
"@babel/preset-react": "7.0.0",
"rollup": "1.6.0",
"rollup": "1.7.0",
"rollup-plugin-babel": "4.3.2",
"rollup-plugin-commonjs": "9.2.1",
"rollup-plugin-jsx": "1.0.3",
"rollup-plugin-node-resolve": "4.0.1",
"rollup-plugin-replace": "2.1.0",
"rollup-plugin-replace": "2.1.1",
"rollup-plugin-terser": "4.0.4",
"source-map-explorer": "1.8.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"apollo-codegen": "0.20.2",
"react-scripts-ts": "3.1.0",
"react-test-renderer": "16.8.1",
"typescript": "3.1.6"
"typescript": "3.3.4000"
},
"scripts": {
"start": "react-scripts-ts start",
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
},
"sideEffects": false,
"devDependencies": {
"@types/enzyme": "3.9.0",
"@types/enzyme": "3.9.1",
"@types/enzyme-adapter-react-16": "1.0.5",
"@types/graphql": "14.0.7",
"@types/hoist-non-react-statics": "3.3.0",
Expand Down Expand Up @@ -144,7 +144,7 @@
"recompose": "0.30.0",
"recursive-rename": "2.0.0",
"rimraf": "2.6.3",
"rollup": "1.6.0",
"rollup": "1.7.0",
"rollup-plugin-commonjs": "9.2.1",
"rollup-plugin-filesize": "6.0.1",
"rollup-plugin-invariant": "0.4.2",
Expand All @@ -154,7 +154,7 @@
"ts-jest": "24.0.0",
"tsc-watch": "2.1.2",
"tslint": "5.14.0",
"typescript": "3.3.3333",
"typescript": "3.3.4000",
"typescript-require": "0.2.10",
"zen-observable-ts": "0.8.18"
},
Expand Down
3 changes: 1 addition & 2 deletions src/ApolloContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ export interface ApolloContextValue {
renderPromises?: RenderPromises;
}

export const ApolloContext =
React.createContext<ApolloContextValue | undefined>({});
export const ApolloContext = React.createContext<ApolloContextValue>({});
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export { withQuery } from './query-hoc';
export { withMutation } from './mutation-hoc';
export { withSubscription } from './subscription-hoc';

export { useApolloClient } from './useApolloClient';

export { default as withApollo } from './withApollo';
export * from './withApollo';

Expand Down
14 changes: 14 additions & 0 deletions src/useApolloClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { invariant } from 'ts-invariant';

import { ApolloContext } from './ApolloContext';

export function useApolloClient() {
const { client } = React.useContext(ApolloContext);
invariant(
!client,
'No Apollo Client instance can be found. Please ensure that you ' +
'have called `ApolloProvider` higher up in your tree.',
);
return client;
}