├── .babelrc
├── .eslintrc
├── .gitignore
├── CHANGELOG.md
├── LICENSE
├── README.md
├── example
├── .eslintrc
├── DataFetcher.tsx
├── DataUpdater.tsx
├── LazyRender.tsx
├── NetworkStatusNotifier.tsx
├── NetworkStatusReporter.tsx
├── createClient.ts
├── index.html
├── index.tsx
└── schema.ts
├── package.json
├── src
├── .eslintrc
├── ActionTypes.ts
├── ApolloLinkNetworkStatus.ts
├── Dispatcher.ts
├── NetworkStatusAction.ts
├── __tests__
│ └── index-test.tsx
├── createNetworkStatusNotifier.ts
├── index.ts
├── useApolloNetworkStatus.ts
├── useApolloNetworkStatusReducer.ts
├── useEventCallback.ts
└── useIsomorphicLayoutEffect.ts
├── tsconfig.json
├── webpack.config.js
└── yarn.lock
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | ["@babel/preset-env", {"modules": false}],
4 | "@babel/preset-react",
5 | "@babel/preset-typescript"
6 | ],
7 | "plugins": [
8 | "@babel/plugin-proposal-class-properties"
9 | ],
10 | "env": {
11 | "test": {
12 | "plugins": ["@babel/plugin-transform-modules-commonjs"]
13 | },
14 | "production": {
15 | "plugins" : [
16 | "@babel/plugin-external-helpers"
17 | ]
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "extends": ["molindo/react"],
3 | "parser": "@typescript-eslint/parser",
4 | "parserOptions": {
5 | "sourceType": "module"
6 | },
7 | "plugins": ["react-hooks", "@typescript-eslint"],
8 | "env": {
9 | "es6": true,
10 | "jest": true
11 | },
12 | "settings": {
13 | "react": {
14 | "version": "detect"
15 | },
16 | "import/resolver": {
17 | "typescript": {}
18 | }
19 | },
20 | "rules": {
21 | "@typescript-eslint/no-unused-vars": "error",
22 | "no-undef": "off",
23 | "no-unused-vars": "off",
24 | "react-hooks/exhaustive-deps": "error",
25 | "react-hooks/rules-of-hooks": "error",
26 | "valid-jsdoc": "off"
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | dist
3 | .idea
4 | .cache
5 | .rts2_cache_cjs
6 | .rts2_cache_es
7 | .rts2_cache_umd
8 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | ## 5.3.1
4 |
5 | ### Fixes
6 |
7 | - Fix types
8 |
9 | ## 5.3.0
10 |
11 | ### Features
12 |
13 | - React 19 support
14 |
15 | ## 5.2.1
16 |
17 | ### Fixes
18 |
19 | - Set `homepage` field in `package.json`
20 |
21 | ## 5.2.0
22 |
23 | ### Features
24 |
25 | - React 18 support
26 | - GraphQL 16 support
27 |
28 | ## 5.1.0
29 |
30 | ### Features
31 |
32 | - React 17 support (@alecdwm in [#49](https://github.com/molindo/react-apollo-network-status/pull/49))
33 |
34 | ## 5.0.1
35 |
36 | ### Fixes
37 |
38 | - Avoid layout effect warning on the server side (@mufasa71 in [#35](https://github.com/molindo/react-apollo-network-status/pull/35))
39 |
40 | ## 5.0
41 |
42 | ### Breaking changes
43 |
44 | - The library now supports [the new `@apollo/client@3`](https://www.apollographql.com/docs/react/migrating/apollo-client-3-migration/). Older versions are no longer supported.
45 | - Remove UMD build, as Apollo also doesn't support one anymore.
46 |
47 | Special thanks to [luuksommers](https://github.com/luuksommers) and [erosval](https://github.com/erosval) for beta testing this release in their apps.
48 |
49 | ## 4.0.1
50 |
51 | ### Fixes
52 |
53 | - Compatibility with React 16.13
54 |
55 | ## 4.0
56 |
57 | ### Improvements
58 |
59 | - Improved types for `NetworkStatusAction`.
60 | - A more reliable integration with the Apollo cache. This fixes [#22](https://github.com/molindo/react-apollo-network-status/issues/22) and [#28](https://github.com/molindo/react-apollo-network-status/issues/28).
61 |
62 | ### Breaking changes
63 |
64 | - The usage of the library has changed from using a provider to configuring a link that needs to be passed to the `ApolloClient` constructor (see [README](./README.md)).
65 | - Scoping network status handling to a subtree was removed along with the `enableBubbling` option. The network status handling is scoped to the usage of the `ApolloClient` instance.
66 |
67 | ## 3.0
68 |
69 | ### New features
70 |
71 | - Support for `@apollo/react-*` packages.
72 | - Export `NetworkStatus` and `OperationError` types for TypeScript users.
73 |
74 | ### Breaking changes
75 |
76 | - Raised required peer dependency version of `apollo-client` to `^2.6.0`.
77 | - You need to depend on a React integration from one of the `@apollo/react-*` packages. See [upgrade guide](https://www.apollographql.com/docs/react/migrating/hooks-migration/).
78 |
79 | Special thanks to [Matth10](https://github.com/Matth10), [rcohen-unext](https://github.com/rcohen-unext) and [MasterKale](https://github.com/MasterKale) for beta testing this release in their apps and code review.
80 |
81 | ## 2.0
82 |
83 | Compatible with `react-apollo@^2`. See [usage instructions](https://github.com/molindo/react-apollo-network-status/tree/e08e7b43e2e3447ec0d9399262d17b162162805e#react-apollo-network-status).
84 |
85 | ### New features
86 |
87 | - Use hooks for reading the network status.
88 | - Simplified API, so you no longer have to setup the link manually.
89 | - TypeScript support.
90 | - The reported network status is now more granular, allowing for more flexible usage.
91 | - You can now scope the reporting of the network status to a subtree instead of being forced to handle all operations globally.
92 | - You can nest the new `` in order to have multiple boundaries where network status will be reported (with optional bubbling configurable with the `enableBubbling` prop).
93 |
94 | ### Breaking changes
95 |
96 | - Updated peer dependencies. Please make sure you fulfill them.
97 | - The network status can only be read within the ``.
98 | - Queries now only reset the new `queryError` property if it was present before (same for mutations). Previously there was only a single `error` property which was affected by both types of operations.
99 | - The opt-out property `context: {useNetworkStatusNotifier: false}` was renamed to `useApolloNetworkStatus`.
100 | - If you provide a custom reducer, there's now a new signature where you only provide one function which handles action types instead of separate functions. This pattern composes better since you usually have to cover all network events to implement a given feature.
101 |
102 | ## 1.1
103 |
104 | Subscription operations no longer affect the loading property, but they can potentially set the error property (@shurik239 in [#9](https://github.com/molindo/react-apollo-network-status/pull/9)).
105 |
106 | ## 1.0
107 |
108 | Compatible with `react-apollo@<=2`. See [usage instructions](https://github.com/molindo/react-apollo-network-status/tree/583a00f6344e05edcfee90bee0823a7736f56021#react-apollo-network-status).
109 |
110 | Initial stable release.
111 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Molindo GmbH
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # react-apollo-network-status
2 |
3 | > Brings information about the global network status from Apollo into React.
4 |
5 | [](https://www.npmjs.com/package/react-apollo-network-status)
6 |
7 | This library helps with implementing global loading indicators like progress bars or adding global error handling, so you don't have to respond to every error in a component that invokes an operation.
8 |
9 | ## Usage
10 |
11 | ```js
12 | import React from 'react';
13 | import ReactDOM from 'react-dom';
14 | import {ApolloClient, InMemoryCache, createHttpLink, ApolloProvider} from '@apollo/client';
15 | import {createNetworkStatusNotifier} from 'react-apollo-network-status';
16 |
17 | const {link, useApolloNetworkStatus} = createNetworkStatusNotifier();
18 |
19 | function GlobalLoadingIndicator() {
20 | const status = useApolloNetworkStatus();
21 |
22 | if (status.numPendingQueries > 0) {
23 | return
Loading …
;
24 | } else {
25 | return null;
26 | }
27 | }
28 |
29 | const client = new ApolloClient({
30 | cache: new InMemoryCache(),
31 | link: link.concat(createHttpLink())
32 | });
33 |
34 | const element = (
35 |
36 |
37 |
38 |
39 | );
40 | ReactDOM.render(element, document.getElementById('root'));
41 | ```
42 |
43 | > **Note:** The current version of this library supports the latest [`@apollo-client` package](https://www.apollographql.com/docs/react/migrating/apollo-client-3-migration/). If you're using an older version of React Apollo and don't want to upgrade, you can use an older version of this library (see [changelog](./CHANGELOG.md)).
44 |
45 | ## Returned data
46 |
47 | The hook `useApolloNetworkStatus` provides an object with the following properties:
48 |
49 | ```tsx
50 | type NetworkStatus = {
51 | // The number of queries which are currently in flight.
52 | numPendingQueries: number;
53 |
54 | // The number of mutations which are currently in flight.
55 | numPendingMutations: number;
56 |
57 | // The latest query error that has occured. This will be reset once the next query starts.
58 | queryError?: OperationError;
59 |
60 | // The latest mutation error that has occured. This will be reset once the next mutation starts.
61 | mutationError?: OperationError;
62 | };
63 |
64 | type OperationError = {
65 | networkError?: Error | ServerError | ServerParseError;
66 | operation?: Operation;
67 | response?: ExecutionResult;
68 | graphQLErrors?: ReadonlyArray;
69 | };
70 | ```
71 |
72 | Subscriptions currently don't affect the status returned by `useApolloNetworkStatus`.
73 |
74 | Useful applications are for example integrating with [NProgress.js](http://ricostacruz.com/nprogress/) or showing errors with [snackbars from Material UI](http://www.material-ui.com/#/components/snackbar).
75 |
76 | ## Advanced usage
77 |
78 | ### Limit handling to specific operations
79 |
80 | The default configuration enables an **opt-out** behaviour per operation by setting a context variable:
81 |
82 | ```js
83 | // Somewhere in a React component
84 | mutate({context: {useApolloNetworkStatus: false}});
85 | ```
86 |
87 | You can configure an **opt-in** behaviour by specifying an operation whitelist like this:
88 |
89 | ```js
90 | // Inside the component handling the network events
91 | useApolloNetworkStatus({
92 | shouldHandleOperation: (operation: Operation) =>
93 | operation.getContext().useApolloNetworkStatus === true
94 | });
95 |
96 | // Somewhere in a React component
97 | mutate({context: {useApolloNetworkStatus: true}});
98 | ```
99 |
100 | ### Custom state
101 |
102 | You can fully control how operations are mapped to state by providing a custom reducer to a separate low-level hook.
103 |
104 | ```tsx
105 | const {link, useApolloNetworkStatusReducer} = createNetworkStatusNotifier();
106 |
107 | const initialState = 0;
108 |
109 | function reducer(state: number, action: NetworkStatusAction) {
110 | switch (action.type) {
111 | case ActionTypes.REQUEST:
112 | return state + 1;
113 |
114 | case ActionTypes.ERROR:
115 | case ActionTypes.SUCCESS:
116 | case ActionTypes.CANCEL:
117 | return state - 1;
118 | }
119 | }
120 |
121 | function GlobalLoadingIndicator() {
122 | const numPendingQueries = useApolloNetworkStatusReducer(reducer, initialState);
123 | return