How to use the apollo-client.NetworkStatus.ready function in apollo-client

To help you get started, we’ve selected a few apollo-client examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github UnlyEd / GraphCMS-cache-boilerplate / src / functions / refresh-cache.js View on Github external
map(queriesResults, (queryResult, index) => {
    // XXX If the query failed (whether completely or partially), we don't update the cache
    if (queryResult.networkStatus !== NetworkStatus.ready) { // See https://github.com/apollographql/apollo-client/blob/master/packages/apollo-client/src/core/networkStatus.ts
      // When a query returns an error, we don't update the cache
      epsagon.setError(Error(`Cache refresh failed with "${JSON.stringify(queryResult.errors)}"`));
      logger.error(JSON.stringify(queryResult.errors, null, 2), 'graphcms-query-error');
      failedResults.push(queryResult);
    } else {
      const redisKey = redisKeys[index];
      const { body, headers } = extractDataFromRedisKey(redisKey);
      logger.debug(`Updating redis cache at index ${index} for query "${makeQueryHumanFriendly(body)}" and headers "${JSON.stringify(headers)}".`);

      // Otherwise, update the existing entry with the new values
      // XXX Executed async, no need to wait for result to continue
      updateItemInCache(redisClient, body, headers, queryResult)
        .then((result) => {
          if (result !== 'OK') {
            throw new Error(result);
          } else {
github UnlyEd / GraphCMS-cache-boilerplate / src / functions / cache-query.js View on Github external
} catch (e) {
      logger.debug(`An exception occurred while fetching GraphCMS API.`);
      logger.error(e);
      epsagon.setError(e);

      return {
        statusCode: 500,
        headers: responseHeaders,
        body: handleGraphCMSCompatibleErrorResponse(String(e)),
      };
    }

    // XXX If a GraphCMS query returns any kind of error we don't add it to the cache, to avoid storing persistent data that aren't reliable
    //  So, we only cache data when they're reliable, to avoid storing data that may not be reliable
    //  (and allow owner to fix it from GCMS, thus ensuring proper caching the next time that query is executed)
    if (queryResults.networkStatus === NetworkStatus.ready) { // See https://github.com/apollographql/apollo-client/blob/master/packages/apollo-client/src/core/networkStatus.ts
      // If the query was executed successfully, update the cache
      // XXX Asynchronous on purpose - Do not wait for the cache to be updated before returning the query results (perf++)
      addItemToCache(redisClient, body, forwardedHeaders, queryResults)
        .then((result) => {
          if (result !== 'OK') {
            const message = `Redis couldn't save the newer query results to the cache: "${result}"`;
            logger.error(message);
            epsagon.setError(Error(message));
          }
        })
        .catch((error) => {
          const message = `Redis couldn't save the newer query results to the cache, an error happened: "${error}"`;
          logger.error(message);
          epsagon.setError(Error(error));
        });
      logger.debug(`The GraphCMS query was executed successfully. Results are now sent to the client.`);
github strvcom / dep-manager-web / src / utils / hooks.ts View on Github external
export const useQuery = (
  options: WatchQueryOptions,
  inputs: ReadonlyArray = []
): ApolloQueryResult => {
  const [state, setState] = useState>({
    stale: false,
    networkStatus: NetworkStatus.ready,
    loading:
      !(options.fetchPolicy && options.fetchPolicy === 'cache-only'),
    data:
      options.fetchPolicy && options.fetchPolicy === 'cache-only'
        ? client.readQuery(options)
        : null
  })
  useEffect(() => {
    const subscription = client.watchQuery(options).subscribe(setState)
    return () => subscription.unsubscribe()
  }, inputs)
  return state
}
github apollographql / react-apollo / packages / hooks / src / data / QueryData.ts View on Github external
public executeLazy(): QueryTuple {
    return !this.runLazy
      ? [
          this.runLazyQuery,
          {
            loading: false,
            networkStatus: NetworkStatus.ready,
            called: false,
            data: undefined
          } as QueryResult
        ]
      : [this.runLazyQuery, this.execute()];
  }
github vendure-ecommerce / vendure / packages / admin-ui / src / app / data / query-result.ts View on Github external
            filter(result => result.networkStatus === NetworkStatus.ready),
            take(1),