Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Loading Flag for Apollo Client

Is there a global loading flag available anywhere for react-apollo client? I have a “page wrapper” component that i’d like to apply ui effects to after all the child components have received their data.

I have set up apollo with redux so have ready access to the store (http://dev.apollodata.com/react/redux.html)

I could quite easily dispatch state changes from each component that receives data from apollo but I'd like this page wrapper component to not have any knowledge of its children nor their queries.

I have investigated using withApollo - http://dev.apollodata.com/react/higher-order-components.html#withApollo - but don't see an api for a global is loading.

like image 728
elmpp Avatar asked May 14 '17 14:05

elmpp


People also ask

What is difference between useQuery and useLazyQuery?

Unlike with useQuery , when you call useLazyQuery , it does not immediately execute its associated query. Instead, it returns a query function in its result tuple that you call whenever you're ready to execute the query.

What is Apolloclient?

Apollo Client is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. Use it to fetch, cache, and modify application data, all while automatically updating your UI.

What is Apollo Client URI?

Apollo Client is a comprehensive state management library for JavaScript. It enables you to use GraphQL to manage both local and remote data.

What is Refetch useQuery?

refetch() The fetchApi() function in our useQuery Hook is the function we'll want to run again if we needed to refetch query information and update state. In the useQuery. ts file, we'll restructure how our Hook is set up to have the fetchApi() function be returned from the Hook.

What is Apollo client?

Apollo Client is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. Use it to fetch, cache, and modify application data, all while automatically updating your UI.

What is Apollo GraphQL client?

Introduction to Apollo Client - Client (React) - Apollo GraphQL Docs Introduction to Apollo Client Apollo Client is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. Use it to fetch, cache, and modify application data, all while automatically updating your UI.

What is local state management in Apollo client?

Apollo Client includes local state management features out of the box, that allow you to use your Apollo cache as the single source of truth for data in your application. Managing all your data with Apollo Client allows you to take advantage of GraphQL as a unified interface to all of your data.

What are the built-in errors in Apollo server?

These built-in error subclasses inherit from the generic ApolloError class, and they're all defined in the apollo-server-errors package. You can also create your own custom errors and codes. The GraphQL operation string contains a syntax error. The GraphQL operation is not valid against the server's schema.


2 Answers

I've just released a library that solves this for Apollo 2: react-apollo-network-status.

The gist is:

import React, {Fragment} from 'react';
import ReactDOM from 'react-dom';
import {ApolloClient} from 'apollo-client';
import {createNetworkStatusNotifier} from 'react-apollo-network-status';
import {createHttpLink} from 'apollo-link-http';

const {
  NetworkStatusNotifier,
  link: networkStatusNotifierLink
} = createNetworkStatusNotifier();

const client = new ApolloClient({
  link: networkStatusNotifierLink.concat(createHttpLink())
});

// Render the notifier along with the app. The
// `NetworkStatusNotifier` can be placed anywhere.
const element = (
  <Fragment>
    <NetworkStatusNotifier render={({loading, error}) => (
      <div>
        {loading && <p>Loading …</p>}
        {error && <p>Error: {JSON.stringify(error)}</p>}
      </div>
    )} />
    <ApolloProvider client={client}>
      <App />
    </ApolloProvider>
  </Fragment>
);
const node = document.getElementById('root');
ReactDOM.render(element, node);
like image 88
amann Avatar answered Oct 26 '22 23:10

amann


You can achieve this in two ways:

One way is to use the middleware/afterware of Apollo's network interface.

The other way is to wrap Apollo's network interface to include your custom logic. Specifically you would wrap the query method.

like image 42
Clay Avatar answered Oct 27 '22 01:10

Clay