Back to directory
jaydenseric avatar

apollo-upload-client

A terminating Apollo Link for Apollo Client that fetches a GraphQL multipart request if the GraphQL variables contain files (by default FileList, File, or Blob instances), or else fetches a regular GraphQL POST or GET request (depending on the config and GraphQL operation).. Read more below about its uses, features, and usage.

Clone repository

git clone https://github.com/jaydenseric/apollo-upload-client.git

1,541

Stars

154

Forks

16

Watchers

—

License

Apollo upload logo

apollo-upload-client

A terminating Apollo Link for Apollo Client that fetches a GraphQL multipart request if the GraphQL variables contain files (by default FileList, File, or Blob instances), or else fetches a regular GraphQL POST or GET request (depending on the config and GraphQL operation).

Installation

To install with npm, run:

npm install apollo-upload-client

Polyfill any required globals (see Requirements) that are missing in your server and client environments.

Apollo Client can only have 1 terminating Apollo Link that sends the GraphQL requests; if one such as HttpLink is already setup, remove it.

Construct ApolloClient with a terminating Apollo Link using the class UploadHttpLink. For client awareness features, compose the Apollo Link ClientAwarenessLink before the terminating link.

Also ensure the GraphQL server implements the GraphQL multipart request spec and that uploads are handled correctly in resolvers.

Examples

Use FileList, File, or Blob instances anywhere within query or mutation variables to send a GraphQL multipart request.

See also the example API and client.

FileList

import { gql } from "@apollo/client";
import { useMutation } from "@apollo/client/react";

/** React component for uploading a file list. */
function UploadFileList() {
  const [mutate] = useMutation<
    {
      uploadFiles: {
        success: boolean;
      };
    },
    {
      files: FileList;
    }
  >(mutation);

  return (
    <input
      type="file"
      multiple
      required
      onChange={({ target: { validity, files } }) => {
        if (validity.valid && files?.length)
          mutate({
            variables: {
              files,
            },
          });
      }}
    />
  );
}

const mutation = gql`
  mutation ($files: [Upload!]!) {
    uploadFiles(files: $files) {
      success
    }
  }
`;

File

import { gql } from "@apollo/client";
import { useMutation } from "@apollo/client/react";

/** React component for uploading a file. */
function UploadFile() {
  const [mutate] = useMutation<
    {
      uploadFile: {
        success: boolean;
      };
    },
    {
      file: File;
    }
  >(mutation);

  return (
    <input
      type="file"
      required
      onChange={({ target: { validity, files } }) => {
        if (validity.valid && files?.[0])
          mutate({
            variables: {
              file: files[0],
            },
          });
      }}
    />
  );
}

const mutation = gql`
  mutation ($file: Upload!) {
    uploadFile(file: $file) {
      success
    }
  }
`;

Blob

import { gql } from "@apollo/client";
import { useMutation } from "@apollo/client/react";

/** React component for uploading a blob. */
function UploadBlob() {
  const [mutate] = useMutation<
    {
      uploadFile: {
        success: boolean;
      };
    },
    {
      file: Blob;
    }
  >(mutation);

  return (
    <button
      type="button"
      onClick={() => {
        mutate({
          variables: {
            file: new Blob(["Content here."], {
              type: "text/plain",
            }),
          },
        });
      }}
    >
      Upload
    </button>
  );
}

const mutation = gql`
  mutation ($file: Upload!) {
    uploadFile(file: $file) {
      success
    }
  }
`;

To avoid the upload default file name blob, replace the Blob approach with File:

new File(
  [value],
  // Custom file name.
  "text.txt",
  {
    type: "text/plain",
  },
);

Requirements

Projects must configure TypeScript to use types from the ECMAScript modules that have a // @ts-check comment:

Exports

The npm package apollo-upload-client features optimal JavaScript module design. It doesn’t have a main index module, so use deep imports from the ECMAScript modules that are exported via the package.json field exports:

Releases

Jul 29, 2026

Version 20.0.0

Download .zip

Major

Updated Node.js support to ^22.13.0 || ^24.0.0 || >=26.0.0. Updated the graphql peer dependency to ^16.0.0 || ^17.0.0, fixing #368. Updated the dependency extract-files to v14.

Patch

Updated...

Aug 29, 2025

Version 19.0.0

Download .zip

Major

Updated Node.js support to ^20.9.0 || >=22.0.0.

Use the TypeScript v5.5+ JSDoc tag @import to import types in modules. To migrate: Upgrade TypeScript to v5.5+.

Updated the peer dependency...

Oct 24, 2023

Version 18.0.1

Download .zip

Patch

Corrected the function createUploadLink option uri type, fixing #316. Prefixed unused parameters with _, fixing #317. Fixed a typo in the changelog entry for v18.0.0.

Oct 23, 2023

Version 18.0.0

Download .zip

Major

Updated Node.js support to ^18.15.0 || >=20.4.0.

Updated the @apollo/client peer dependency to ^3.8.0.

Updated the extract-files dependency to v13.

React Native is no longer supported ou...

Dec 7, 2021

Version 17.0.0

Download .zip

Major

Updated Node.js support to ^12.22.0 || ^14.17.0 || >= 16.0.0. Updated dev dependencies, some of which require newer Node.js versions than previously supported. Removed ./package from the packag...