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-average-query-time

v0.0.6

Published

Apollo Link for Tracking Average Query Times

Downloads

4

Readme

npm npm bundle size

Apollo Link Average Query Time

Installation

yarn add apollo-link-average-query-time

Usage

import { ApolloLink } from 'apollo-link';
import { InMemoryCache } from 'apollo-cache-inmemory';
import AverageQueryTimeLink from 'apollo-link-average-query-time';

const averageQueryTimeLink = new AverageQueryTimeLink({
  debug: true, // defaults to false, logs information about operations and query times
  queryCount: 5, // defaults to 10, average time is calculated from the last X queries
});

const link = ApolloLink.from([
  ...yourOtherLinks,
  averageQueryTimeLink,
]);

const cache = new InMemoryCache();

export default new ApolloClient({
  link,
  cache,
});

This will add a averageQueryTime property to your Apollo cache, a milliseconds value based on the average of the last queryCount queries.

A basic query will give you reactive updates to the averageQueryTime property:

const AverageQueryTimeQuery = gql`
  query AverageQueryTime {
    averageQueryTime
  }
`;

Sample React Component

If the averageQueryTime exceeds the slowThreshold prop, the component will show the dismiss button.

If the user clicks the button, then the button will be hidden until the dismissTime is exceeded again.

import React, { Fragment, PureComponent } from 'react';
import PropTypes from 'prop-types';
import gql from 'graphql-tag';
import { graphql } from 'react-apollo';

class SlowConnectionIndicator extends PureComponent {
  static propTypes = {
    averageQueryTime: PropTypes.number,
    dismissTime: PropTypes.number,
    slowThreshold: PropTypes.number,
  };

  static defaultProps = {
    averageQueryTime: 0, // Passed in via the query, a default of 0 will prevent the initial display
    dismissTime: 500, // How long to dismiss the component on click in milliseconds
    slowThreshold: 3000, // The default threshold in milliseconds
  };

  state = {
    lastClick: undefined,
  };

  get thresholdExceeded() {
    return this.props.averageQueryTime > this.props.slowThreshold;
  }

  get lastClickExceeded() {
    if (!this.state.lastClick) return true;
    return Date.now() - this.state.lastClick > (this.props.dismissTime);
  }

  handleDismissClick = () => {
    this.setState({ lastClick: Date.now() });
  }

  render() {
    if (this.thresholdExceeded && this.lastClickExceeded) {
      return (
        <button onClick={this.handleDismissClick}>
          {'Slow Connection Detected! Click to Dismiss'}
        </button>
      );
    }
    return null;
  }
}

const AverageQueryTimeQuery = gql`
  query AverageQueryTime {
    averageQueryTime
  }
`;

export default graphql(AverageQueryTimeQuery, {
  name: 'averageQueryTimeQueryData',
  options: () => ({
    fetchPolicy: 'cache-only',
    variables: {},
  }),
  props: ({averageQueryTimeQueryData }) => ({
    averageQueryTime: averageQueryTimeQueryData.averageQueryTime || 0,
  }),
})(SlowConnectionIndicator);