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-batch-http

v1.2.14

Published

Batch HTTP transport layer for GraphQL

Downloads

102,233

Readme


title: apollo-link-batch-http description: Batch multiple operations into a single HTTP request

apollo-link-batch-http is a terminating link that combines multiple GraphQL operations into a single HTTP request. This link batches together individual operations into an array that is sent to a single GraphQL endpoint.

import { BatchHttpLink } from "apollo-link-batch-http";

const link = new BatchHttpLink({ uri: "/graphql" });

Options

The batch http link accepts an object with some options to customize the behavior of the link. There are two different categories of options: http and batch. The http options follow the same structure as the apollo-link-http:

  • uri: the URI key is a string endpoint -- will default to "/graphql" if not specified
  • includeExtensions: allow passing the extensions field to your graphql server, defaults to false
  • fetch: a fetch compatible API for making a request
  • headers: an object representing values to be sent as headers on the request
  • credentials: a string representing the credentials policy you want for the fetch call
  • fetchOptions: any overrides of the fetch options argument to pass to the fetch call. Note that you cannot use batching with the GET HTTP method.

The batching options indicate how operations are batched together, the size of batches, and the maximum time a batch will wait before automatically being sent over the network.

  • batchMax: a max number of items to batch, defaults at 10
  • batchInterval: the interval at which to batch (in ms), defaults to 10
  • batchKey: a function that accepts an operation and returns a string key, which uniquely names the batch the operation belongs to, defaults to returning the same string

Fetch polyfill

The batch http link relies on having fetch present in your runtime environment. If you are running on react-native, or modern browsers, this should be no problem. If you are targeting an environment without fetch such as older browsers or the server, you will need to pass your own fetch to the link through the options. We recommend unfetch for older browsers and node-fetch for running in Node.

Context

The Batch Http Link currently uses the context in two different ways, per batch and per query. The context fields below are used per batch and taken from the first operation in the batch. They are applied to the fetch options in a similar manner as apollo-link-http.

  • headers: an object representing values to be sent as headers on the request
  • credentials: a string representing the credentials policy you want for the fetch call
  • uri: a string of the endpoint you want to fetch from
  • fetchOptions: any overrides of the fetch options argument to pass to the fetch call
  • response: this is the raw response from the fetch request after it is made.

For each query, the http field is used to modify each individual query in the batch, such as persisted queries (see below)

Persisted queries

The batch http link supports an advanced GraphQL feature called persisted queries. This allows you to not send the stringified query over the wire, but instead send some kind of identifier of the query. To support this you need to attach the id somewhere to the extensions field and pass the following options to the context:

operation.setContext({
  http: {
    includeExtensions: true,
    includeQuery: false,
  }
})

The http object on context currently supports two keys:

  • includeExtensions: Send the extensions object for this request.
  • includeQuery: Don't send the query field for this request.

One way to use persisted queries is with apollo-link-persisted-queries and Apollo Engine.

Errors

The batch http link handles errors on a per batch basis with the same semantics found in apollo-link-http.

Custom fetching

You can use the fetch option when creating an http-link to do a lot of custom networking. This is useful if you want to modify the request based on the calculated headers or calculate the uri based on the operation:

Custom auth

const customFetch = (uri, options) => {
  const { header } = Hawk.client.header(
    "http://example.com:8000/resource/1?b=1&a=2",
    "POST",
    { credentials: credentials, ext: "some-app-data" }
  );
  options.headers.Authorization = header;
  return fetch(uri, options);
};

const link = new BatchHttpLink({ fetch: customFetch });

Dynamic URI

const customFetch = (uri, options) => {
  const operationNames = JSON.parse(options.body).map(operation => operation.operationName);
  return fetch(`${uri}/graph/graphql?opname=${operationNames}`, options);
};

const link = new BatchHttpLink({ fetch: customFetch });