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

graphql-requester

v1.0.1

Published

Axios-based library used to request data from grapql (now with middlewares!)

Downloads

18

Readme

graphql-requester · Travis CI Status codecov coverage GitHub license

Axios-based library used to request data from graphql (now with middlewares!).

const request = Requester({
  url: "https://countries.trevorblades.com/",
  middlewares: [dedupe]
})

await countries = request("countries", `{
  name
}`)

<...>

Example: /example/src/components/CountryList/index.js.

Usage

Create Requester instance

Firstly you have to create Requester instance, it receives object with three fields:

url

Basically, your graphql endpoint, e.g. https://countries.trevorblades.com/

options

Object of axios options for an axios instance, used, for example, if you need to pass specific header or something: { headers: { "X-Requested-With": "XMLHttpRequest"} } .

middlewares

Array of middlewares, built-in or your own, more on them later.

Example:

import { Requester, dedupe } from "graphql-requester"

const request = Requester({
  url: "https://countries.trevorblades.com/",
  middlewares: [dedupe]
})

export default request

Call instance

After that you're ready to call Requester instance you've configured.

Instance with three parameters:

Name of the query.

String. Used to name the query in requests tab of inspector. Also used in dedupe middleware in cacheKey, e.g. products

Graphql query.

String. e.g:

products {
  name
}
Options, used by middlewares.

Object. For example dedupe middleware uses multiple one: { multiple: true }

Middlewares

graphql-requester bundled with several middleware out of the box:

dataExtract

Extracts data from response object to work with it in the next promise chain.

errorExtract

Does the same with error.

dedupe

Deduplicates same requests for you, for example if you called same request several time and first call isn't finished, second call will be just the link to the first one and will not be called. But if you really need to place several calls in the same time you can pass multiple: true to the options.

loader

Is a function which accepts object with two callbacks, onBefore and onAfter and returns middleware. Used to wrap all requests with some kind of global loader. For example:

const loaderMiddleware = loader({
  onBefore: startLoader,
  onAfter: endLoader
});

const request = Requester({
  middlewares: [loaderMiddleware]
})

And after that your Requester instance will always invoke that callback on before and after the call, but if you don't need to invoke it, you can pass { withLoader: false } top options.

Writing your own middleware

Each middleware receives ctx as an first and only argument, it cointains such fields like:

call

Promise. An promise of the call which will be fired.

name

String. Name of the query, first argument of call of an instance.

query

String. Graphql query itself, second argument.

opts

Object. Call options, third argument.

cancelled

Boolean. If it will be mutated to false, chain of middlwares will be broken an call will fire right after.

preprocessors

Array of functions. Will be called in a sequence on a successful request, each of function receives resolved data modified by previous middleware and should return modified version of it.

errorHandlers

Array of functions. Basically the same as preprocessors, but for errors. Will be called in a sequence on a failed request.

TypeScript

Definitions included.

import {
  dataExtract,
  loader, LoaderOpts,
  dedupe, DedupeOpts,
} from "graphql-requester"

<...>

const request = Requester<LoaderOpts & DedupeOpts>({
  middlewares: [ dedupe, dataExtract loaderMiddleware ]
})

Licence

MIT