apollo-upload-client
Enhances Apollo Client for intuitive file uploads via GraphQL mutations.
Last updated 7 years ago by jaydenseric .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install apollo-upload-client 
SYNC missed versions from official npm registry.

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.

Remove any uri, credentials, or headers options from the ApolloClient constructor.

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

Initialize the client with a terminating Apollo Link using the function createUploadLink.

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, useMutation } from "@apollo/client";

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

function UploadFiles() {
  const [mutate] = useMutation(MUTATION);

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

File

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

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

function UploadFile() {
  const [mutate] = useMutation(MUTATION);

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

Blob

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

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

function UploadFile() {
  const [mutate] = useMutation(MUTATION);

  return (
    <input
      type="text"
      required
      onChange={({ target: { validity, value } }) => {
        if (validity.valid) {
          const file = new Blob([value], { type: "text/plain" });

          // Optional, defaults to `blob`.
          file.name = "text.txt";

          mutate({
            variables: {
              file,
            },
          });
        }
      }}
    />
  );
}

Requirements

Consider polyfilling:

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:

Current Tags

  • 18.0.1                                ...           latest (a year ago)

61 Versions

  • 18.0.1                                ...           a year ago
  • 18.0.0                                ...           a year ago
  • 17.0.0                                ...           3 years ago
  • 16.0.0                                ...           3 years ago
  • 15.0.0                                ...           4 years ago
  • 14.1.3                                ...           4 years ago
  • 14.1.2                                ...           4 years ago
  • 14.1.1                                ...           4 years ago
  • 14.1.0                                ...           4 years ago
  • 14.0.1                                ...           4 years ago
  • 14.0.0                                ...           4 years ago
  • 13.0.0                                ...           5 years ago
  • 12.1.0                                ...           5 years ago
  • 12.0.0                                ...           5 years ago
  • 11.0.0                                ...           5 years ago
  • 10.0.1                                ...           6 years ago
  • 10.0.0                                ...           6 years ago
  • 9.1.0                                ...           6 years ago
  • 9.0.0                                ...           6 years ago
  • 8.1.0                                ...           6 years ago
  • 8.0.0                                ...           7 years ago
  • 7.1.0                                ...           7 years ago
  • 7.1.0-alpha.2                                ...           7 years ago
  • 7.1.0-alpha.1                                ...           7 years ago
  • 7.0.0-alpha.4                                ...           7 years ago
  • 7.0.0-alpha.3                                ...           7 years ago
  • 7.0.0-alpha.2                                ...           7 years ago
  • 7.0.0-alpha.1                                ...           7 years ago
  • 6.0.3                                ...           7 years ago
  • 6.0.2                                ...           7 years ago
  • 6.0.1                                ...           7 years ago
  • 6.0.0                                ...           7 years ago
  • 6.0.0-beta.3                                ...           7 years ago
  • 6.0.0-beta.2                                ...           7 years ago
  • 6.0.0-beta.1                                ...           7 years ago
  • 5.1.1                                ...           7 years ago
  • 5.1.0                                ...           7 years ago
  • 5.0.0                                ...           7 years ago
  • 5.0.0-alpha.1                                ...           7 years ago
  • 4.1.1                                ...           7 years ago
  • 4.1.0                                ...           7 years ago
  • 4.1.0-alpha.2                                ...           7 years ago
  • 4.1.0-alpha.1                                ...           7 years ago
  • 4.0.7                                ...           7 years ago
  • 4.0.6                                ...           7 years ago
  • 4.0.5                                ...           7 years ago
  • 4.0.4                                ...           7 years ago
  • 4.0.3                                ...           8 years ago
  • 4.0.2                                ...           8 years ago
  • 4.0.1                                ...           8 years ago
  • 4.0.0                                ...           8 years ago
  • 3.0.3                                ...           8 years ago
  • 3.0.2                                ...           8 years ago
  • 3.0.1                                ...           8 years ago
  • 3.0.0                                ...           8 years ago
  • 2.0.2                                ...           8 years ago
  • 2.0.1                                ...           8 years ago
  • 2.0.0                                ...           8 years ago
  • 1.0.2                                ...           8 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
Maintainers (1)
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)
Dev Dependencies (12)

© 2010 - cnpmjs.org x YWFE | Home | YWFE