All Projects → molindo → React Apollo Network Status

molindo / React Apollo Network Status

Licence: mit
Brings information about the global network status from Apollo into React.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Apollo Network Status

React Graphql Github Apollo
🚀 A React + Apollo + GraphQL GitHub Client. Your opportunity to learn about these technologies in a real world application.
Stars: ✭ 1,563 (+669.95%)
Mutual labels:  apollo-client
Typescript Expo Apollo Boilerplate
Clean boilerplate for TypeScript + Expo (React Native) + React Apollo (GraphQL)
Stars: ✭ 144 (-29.06%)
Mutual labels:  apollo-client
Ddp Apollo
DDP link for Apollo with GraphQL Subscriptions support
Stars: ✭ 163 (-19.7%)
Mutual labels:  apollo-client
Nextjs Hasura Boilerplate
🎨 Boilerplate for building applications using Hasura and Next.js
Stars: ✭ 126 (-37.93%)
Mutual labels:  apollo-client
Fullstack Apollo React Boilerplate
💥A sophisticated Apollo in React boilerplate project.
Stars: ✭ 136 (-33%)
Mutual labels:  apollo-client
Apollo Link Logger
A logger for Apollo Link that resembles redux-logger
Stars: ✭ 148 (-27.09%)
Mutual labels:  apollo-client
Nextjs Headless Wordpress
🔥 Nextjs Headless WordPress
Stars: ✭ 110 (-45.81%)
Mutual labels:  apollo-client
Prisma Auth0 Example
Boilerplate Prisma Startup
Stars: ✭ 184 (-9.36%)
Mutual labels:  apollo-client
Graphql Directive
Use custom directives in your GraphQL schema and queries 🎩
Stars: ✭ 142 (-30.05%)
Mutual labels:  apollo-client
Next Graphql Blog
🖊 A Blog including a server and a client. Server is built with Node, Express & a customized GraphQL-yoga server. Client is built with React, Next js & Apollo client.
Stars: ✭ 152 (-25.12%)
Mutual labels:  apollo-client
Awesome Apollo Graphql
A curated list of amazingly awesome things regarding Apollo GraphQL ecosystem 🌟
Stars: ✭ 126 (-37.93%)
Mutual labels:  apollo-client
React Redux Graphql Apollo Bootstrap Webpack Starter
react js + redux + graphQL + Apollo + react router + hot reload + devTools + bootstrap + webpack starter
Stars: ✭ 127 (-37.44%)
Mutual labels:  apollo-client
Reactql
Universal React+GraphQL starter kit: React 16, Apollo 2, MobX, Emotion, Webpack 4, GraphQL Code Generator, React Router 4, PostCSS, SSR
Stars: ✭ 1,833 (+802.96%)
Mutual labels:  apollo-client
Fakebooker Frontend
Stars: ✭ 124 (-38.92%)
Mutual labels:  apollo-client
Agollo
An elegant Go client for Ctrip Apollo
Stars: ✭ 167 (-17.73%)
Mutual labels:  apollo-client
Graphql Codegen Hasura
code-generator plugins for hasura/apollo-gql/typescript development
Stars: ✭ 113 (-44.33%)
Mutual labels:  apollo-client
Apollo Php Client
携程Apollo配置中心PHP客户端
Stars: ✭ 147 (-27.59%)
Mutual labels:  apollo-client
Apollo Offline
An offline toolkit for the Apollo client
Stars: ✭ 186 (-8.37%)
Mutual labels:  apollo-client
Apollo Cache Redux
Redux cache for Apollo Client 2.0. This project is no longer maintained.
Stars: ✭ 179 (-11.82%)
Mutual labels:  apollo-client
Mobx Apollo
A MobX and Apollo Client integration utility.
Stars: ✭ 152 (-25.12%)
Mutual labels:  apollo-client

react-apollo-network-status

Brings information about the global network status from Apollo into React.

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.

Usage

import React from 'react';
import ReactDOM from 'react-dom';
import {ApolloClient, InMemoryCache, createHttpLink, ApolloProvider} from '@apollo/client';
import {createNetworkStatusNotifier} from 'react-apollo-network-status';

const {link, useApolloNetworkStatus} = createNetworkStatusNotifier();

function GlobalLoadingIndicator() {
  const status = useApolloNetworkStatus();

  if (status.numPendingQueries > 0) {
    return <p>Loading </p>;
  } else {
    return null;
  }
}

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: link.concat(createHttpLink())
});

const element = (
  <ApolloProvider client={client}>
    <GlobalLoadingIndicator />
    <App />
  </ApolloProvider>
);
ReactDOM.render(element, document.getElementById('root'));

Note: The current version of this library supports the latest @apollo-client package. 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).

Returned data

The hook useApolloNetworkStatus provides an object with the following properties:

type NetworkStatus = {
  // The number of queries which are currently in flight.
  numPendingQueries: number;
  
  // The number of mutations which are currently in flight.
  numPendingMutations: number;

  // The latest query error that has occured. This will be reset once the next query starts.
  queryError?: OperationError;

  // The latest mutation error that has occured. This will be reset once the next mutation starts.
  mutationError?: OperationError;
};

type OperationError = {
  networkError?: Error | ServerError | ServerParseError;
  operation?: Operation;
  response?: ExecutionResult;
  graphQLErrors?: ReadonlyArray<GraphQLError>;
};

Subscriptions currently don't affect the status returned by useApolloNetworkStatus.

Useful applications are for example integrating with NProgress.js or showing errors with snackbars from Material UI.

Advanced usage

Limit handling to specific operations

The default configuration enables an opt-out behaviour per operation by setting a context variable:

// Somewhere in a React component
mutate({context: {useApolloNetworkStatus: false}});

You can configure an opt-in behaviour by specifying an operation whitelist like this:

// Inside the component handling the network events
useApolloNetworkStatus({
  shouldHandleOperation: (operation: Operation) =>
    operation.getContext().useApolloNetworkStatus === true
});

// Somewhere in a React component
mutate({context: {useApolloNetworkStatus: true}});

Custom state

You can fully control how operations are mapped to state by providing a custom reducer to a separate low-level hook.

const {link, useApolloNetworkStatusReducer} = createNetworkStatusNotifier();

const initialState = 0;

function reducer(state: number, action: NetworkStatusAction) {
  switch (action.type) {
    case ActionTypes.REQUEST:
      return state + 1;

    case ActionTypes.ERROR:
    case ActionTypes.SUCCESS:
    case ActionTypes.CANCEL:
      return state - 1;
  }
}

function GlobalLoadingIndicator() {
  const numPendingQueries = useApolloNetworkStatusReducer(reducer, initialState);
  return <p>Pending queries: {numPendingQueries}</p>;
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].