npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-link-network-error

v0.0.5

Published

An Apollo Link that you can dynamic ignore/change the network error.

Downloads

4,188

Readme

apollo-link-network-error

An Apollo Link that you can dynamic ignore/change the network error. and you can easy to implement the Network-first feature(with network-only fetchPolicy) and Offline-first feature (with cache-and-network fetchPolicy)

Basically this means your UI's queries will always work if the requested data is available in the local cache and it will always keep the cached data consistent with your server data if it can be reached.

NOTE: Currently we only tested it on react-native. If there are some issues on browser, just create an issue or make a PR

Install

npm install --save apollo-link-network-error

Basic Usage

Setup

import { ApolloClient } from 'apollo-client'
import { NetworkErrorLink } from 'apollo-link-network-error'

const onNetworkError = ({ error, operation }) => {
  if ('some_condition_1') {
    // option1: throw a new error to replace
    throw error
  }

  if ('some_condition_2') {
    // option2: not return error and will take following object as response
    return {
      info: somedata,
    }
  }

  return Promise(....) // option3: you can return a promise
}

const errorIgnoreLink = new NetworkErrorLink(onNetworkError)
const client = new ApolloClient({
  cache,
  errorIgnoreLink,
})

Offline solution

Setup

import { ApolloClient, ApolloLink } from '@apollo/client'
import { cacheFirstNetworkErrorLink } from 'apollo-link-network-error'

const cache = new InMemoryCache()
// Create the cache first network error link
const errorIgnoreLink = cacheFirstNetworkErrorLink(cache)
const link = ApolloLink.from([errorLink, errorIgnoreLink, httpLink])
const client = new ApolloClient({
  cache,
  link,
})

persistCache({ cache, storage: AsyncStorage })

Note: Once set up, you can add the flag in context (i.e. context: { __skipErrorAccordingCache__: true }).

Network-first Example

NOTE: As all we know, apollo client provide the fetchPolicy: 'network-only', this policy will try visit remote data, if success, it will store the data to cache

'network-only' + cacheFirstNetworkErrorLink: This a kind of like the network-first behavior. it will try to visit network data first. if the server can't be reached or no network, it will use the cache data. if no cache data, it will throw a network error

// Note: You can not use 'cache-and-network' on client.query(). this is the limitation from apollo.
client.query({
  fetchPolicy: 'network-only',
  query: /* Your query here */,
  // Enable error ignore
  context: { __skipErrorAccordingCache__: true }
  variables: /* Your variables here */
});

React-hook

const { data, error } = useQuery(YOUR_QUERY, {
  variables: YOUR_VARIBLES,
  fetchPolicy: 'network-only',
  context: { __skipErrorAccordingCache__: true },
})

Offline-first Example

NOTE: As all we know, apollo client provide the fetchPolicy: 'cache-and-network'. It will use local data and then try to fetch the network data. However, it errors if the server can't be reached or no network connection

'cache-and-network' + cacheFirstNetworkErrorLink: if the server can't be reached or no network, it still try to use cache data. if no cached data, it will throw a network error.

React-Apollo

<Query
  query=YOUR_QUERY
  variables=YOUR_VARIBLES
  fetchPolicy="cache-and-network"
  context={{__skipErrorAccordingCache__: true}}
>
  {props.children}
</Query >

React-hook

const { data, error } = useQuery(YOUR_QUERY, {
  variables: YOUR_VARIBLES,
  fetchPolicy: 'cache-and-network',
  context: { __skipErrorAccordingCache__: true },
})