diff --git a/Changelog.md b/Changelog.md
index eedb8e58c4..5eb02b5ce0 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -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`.
+ [@FredyC](https://github.com/FredyC) in [#2872](https://github.com/apollographql/react-apollo/pull/2872)
+
+
## vNEXT
### Bug Fixes
diff --git a/examples/components/package.json b/examples/components/package.json
index 325b5a5ea8..61acb2dac4 100644
--- a/examples/components/package.json
+++ b/examples/components/package.json
@@ -17,7 +17,7 @@
"eject": "react-scripts eject"
},
"devDependencies": {
- "react-testing-library": "6.0.0"
+ "react-testing-library": "6.0.2"
},
"browserslist": [
">0.2%",
diff --git a/examples/mutation/package.json b/examples/mutation/package.json
index 5e261a133c..4980f1181d 100644
--- a/examples/mutation/package.json
+++ b/examples/mutation/package.json
@@ -17,7 +17,7 @@
"eject": "react-scripts eject"
},
"devDependencies": {
- "react-testing-library": "6.0.0"
+ "react-testing-library": "6.0.2"
},
"browserslist": [
">0.2%",
diff --git a/examples/rollup/package-lock.json b/examples/rollup/package-lock.json
index edb7d02f9b..1835e78ace 100644
--- a/examples/rollup/package-lock.json
+++ b/examples/rollup/package-lock.json
@@ -128,9 +128,9 @@
"dev": true
},
"@types/node": {
- "version": "11.11.3",
- "resolved": "https://registry.npmjs.org/@types/node/-/node-11.11.3.tgz",
- "integrity": "sha512-wp6IOGu1lxsfnrD+5mX6qwSwWuqsdkKKxTN4aQc4wByHAKZJf9/D4KXPQ1POUjEbnCP5LMggB0OEFNY9OTsMqg==",
+ "version": "11.11.6",
+ "resolved": "https://registry.npmjs.org/@types/node/-/node-11.11.6.tgz",
+ "integrity": "sha512-Exw4yUWMBXM3X+8oqzJNRqZSwUAaS4+7NdvHqQuFi/d+synz++xmX3QIf+BFqneW8N31R8Ky+sikfZUXq07ggQ==",
"dev": true
},
"@types/zen-observable": {
@@ -1896,9 +1896,9 @@
}
},
"rollup": {
- "version": "1.6.0",
- "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.6.0.tgz",
- "integrity": "sha512-qu9iWyuiOxAuBM8cAwLuqPclYdarIpayrkfQB7aTGTiyYPbvx+qVF33sIznfq4bxZCiytQux/FvZieUBAXivCw==",
+ "version": "1.7.0",
+ "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.7.0.tgz",
+ "integrity": "sha512-hjuWSCgoQsFSTsmsNP4AH1l1kfkFqW82gW00V9nL81Zr3JtnKn3rvxh18jUAAEMb7qNoHj21PR5SqbK2mhBgMg==",
"dev": true,
"requires": {
"@types/estree": "0.0.39",
@@ -1986,14 +1986,13 @@
}
},
"rollup-plugin-replace": {
- "version": "2.1.0",
- "resolved": "https://registry.npmjs.org/rollup-plugin-replace/-/rollup-plugin-replace-2.1.0.tgz",
- "integrity": "sha512-SxrAIgpH/B5/W4SeULgreOemxcpEgKs2gcD42zXw50bhqGWmcnlXneVInQpAqzA/cIly4bJrOpeelmB9p4YXSQ==",
+ "version": "2.1.1",
+ "resolved": "https://registry.npmjs.org/rollup-plugin-replace/-/rollup-plugin-replace-2.1.1.tgz",
+ "integrity": "sha512-IS5ZYBb3px0UfbDCYzKaKxelLd5dbPHhfplEXbymfvGlz9Ok44At4AjTOWe2qEax73bE8+pnMZN9C7PcVpFNlw==",
"dev": true,
"requires": {
- "magic-string": "^0.25.1",
- "minimatch": "^3.0.2",
- "rollup-pluginutils": "^2.0.1"
+ "magic-string": "^0.25.2",
+ "rollup-pluginutils": "^2.4.1"
}
},
"rollup-plugin-terser": {
diff --git a/examples/rollup/package.json b/examples/rollup/package.json
index a8d4371514..f02effb5fb 100644
--- a/examples/rollup/package.json
+++ b/examples/rollup/package.json
@@ -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"
},
diff --git a/examples/typescript/package.json b/examples/typescript/package.json
index e9d2f6f847..fd746c5350 100644
--- a/examples/typescript/package.json
+++ b/examples/typescript/package.json
@@ -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",
diff --git a/package-lock.json b/package-lock.json
index d183bbc4f4..9b3b079d2a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -742,9 +742,9 @@
}
},
"@types/enzyme": {
- "version": "3.9.0",
- "resolved": "https://registry.npmjs.org/@types/enzyme/-/enzyme-3.9.0.tgz",
- "integrity": "sha512-o0C7ooyBtj9NKyMzn2BWN53W4J21KPhO+/v+qqQX28Pcz0Z1B3DjL9bq2ZR4TN70PVw8O7gkhuFtC7VN3tausg==",
+ "version": "3.9.1",
+ "resolved": "https://registry.npmjs.org/@types/enzyme/-/enzyme-3.9.1.tgz",
+ "integrity": "sha512-CasnOP73BFE3/5JvGkod+oQtGOD1+CVWz9BV2iAqDFJ+sofL5gTiizSr8ZM3lpDY27ptC8yjAdrUCdv8diKKqw==",
"dev": true,
"requires": {
"@types/cheerio": "*",
@@ -7926,9 +7926,9 @@
}
},
"rollup": {
- "version": "1.6.0",
- "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.6.0.tgz",
- "integrity": "sha512-qu9iWyuiOxAuBM8cAwLuqPclYdarIpayrkfQB7aTGTiyYPbvx+qVF33sIznfq4bxZCiytQux/FvZieUBAXivCw==",
+ "version": "1.7.0",
+ "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.7.0.tgz",
+ "integrity": "sha512-hjuWSCgoQsFSTsmsNP4AH1l1kfkFqW82gW00V9nL81Zr3JtnKn3rvxh18jUAAEMb7qNoHj21PR5SqbK2mhBgMg==",
"dev": true,
"requires": {
"@types/estree": "0.0.39",
@@ -9152,9 +9152,9 @@
}
},
"typescript": {
- "version": "3.3.3333",
- "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.3.3333.tgz",
- "integrity": "sha512-JjSKsAfuHBE/fB2oZ8NxtRTk5iGcg6hkYXMnZ3Wc+b2RSqejEqTaem11mHASMnFilHrax3sLK0GDzcJrekZYLw==",
+ "version": "3.3.4000",
+ "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.3.4000.tgz",
+ "integrity": "sha512-jjOcCZvpkl2+z7JFn0yBOoLQyLoIkNZAs/fYJkUG6VKy6zLPHJGfQJYFHzibB6GJaF/8QrcECtlQ5cpvRHSMEA==",
"dev": true
},
"typescript-require": {
diff --git a/package.json b/package.json
index 8f0919f4ee..7783fce69b 100644
--- a/package.json
+++ b/package.json
@@ -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",
@@ -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",
@@ -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"
},
diff --git a/src/ApolloContext.ts b/src/ApolloContext.ts
index 4e6cd314f6..9ae703c296 100644
--- a/src/ApolloContext.ts
+++ b/src/ApolloContext.ts
@@ -8,5 +8,4 @@ export interface ApolloContextValue {
renderPromises?: RenderPromises;
}
-export const ApolloContext =
- React.createContext({});
+export const ApolloContext = React.createContext({});
diff --git a/src/index.ts b/src/index.ts
index a5cb60a58b..90488c9d27 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -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';
diff --git a/src/useApolloClient.ts b/src/useApolloClient.ts
new file mode 100644
index 0000000000..3c9d747d3a
--- /dev/null
+++ b/src/useApolloClient.ts
@@ -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;
+}