The UploadHttpLink class is a terminating Apollo Link that handles GraphQL file uploads by automatically detecting files in variables and creating multipart requests, or falling back to regular GraphQL requests when no files are present.
Creates a terminating Apollo Link for handling file uploads and regular GraphQL requests.
/**
* A terminating Apollo Link for Apollo Client that fetches a GraphQL multipart
* request if the GraphQL variables contain files, or else fetches a regular
* GraphQL POST or GET request.
*/
export default class UploadHttpLink extends ApolloLink {
constructor(options?: UploadHttpLinkOptions);
}
interface UploadHttpLinkOptions {
/** GraphQL endpoint URI. Defaults to "/graphql" */
uri?: string;
/** Should GET be used to fetch queries, if there are no files to upload */
useGETForQueries?: boolean;
/** Matches extractable files in the GraphQL operation. Defaults to isExtractableFile */
isExtractableFile?: ExtractableFileMatcher;
/** FormData class. Defaults to the FormData global */
FormData?: typeof FormData;
/** Customizes how extracted files are appended to the FormData instance. Defaults to formDataAppendFile */
formDataAppendFile?: FormDataFileAppender;
/** Prints the GraphQL query or mutation AST to a string for transport. Defaults to defaultPrinter */
print?: BaseHttpLinkPrinter;
/** fetch implementation. Defaults to the fetch global */
fetch?: typeof fetch;
/** fetch options; overridden by upload requirements */
fetchOptions?: RequestInit;
/** Overrides credentials in fetchOptions */
credentials?: string;
/** Merges with and overrides headers in fetchOptions */
headers?: { [headerName: string]: string };
/** Toggles sending extensions fields to the GraphQL server. Defaults to false */
includeExtensions?: boolean;
/** Toggles including unused GraphQL variables in the request. Defaults to false */
includeUnusedVariables?: boolean;
}Usage Examples:
import UploadHttpLink from "apollo-upload-client/UploadHttpLink.mjs";
import { ApolloClient } from "@apollo/client/core";
import { InMemoryCache } from "@apollo/client/cache";
// Basic setup
const client = new ApolloClient({
cache: new InMemoryCache(),
link: new UploadHttpLink(),
});
// Advanced configuration
const client = new ApolloClient({
cache: new InMemoryCache(),
link: new UploadHttpLink({
uri: "https://api.example.com/graphql",
useGETForQueries: true,
credentials: "include",
headers: {
"Authorization": "Bearer token",
},
fetchOptions: {
timeout: 30000,
},
}),
});
// Custom file handling
import formDataAppendFile from "apollo-upload-client/formDataAppendFile.mjs";
import isExtractableFile from "apollo-upload-client/isExtractableFile.mjs";
const customIsExtractableFile = (value) =>
isExtractableFile(value) ||
(typeof CustomFile !== "undefined" && value instanceof CustomFile);
const link = new UploadHttpLink({
isExtractableFile: customIsExtractableFile,
formDataAppendFile: (formData, fieldName, file) => {
// Custom file appending logic
if (file.type?.startsWith('image/')) {
formData.append(fieldName, file, `image_${Date.now()}.${file.type.split('/')[1]}`);
} else {
formDataAppendFile(formData, fieldName, file);
}
},
});string"/graphql"booleanfalsetrue, uses GET requests for queries that don't contain files. Mutations always use POST.ExtractableFileMatcherisExtractableFile from apollo-upload-client/isExtractableFile.mjstypeof FormDataFormDataFormDataFileAppenderformDataAppendFile from apollo-upload-client/formDataAppendFile.mjsBaseHttpLink.PrinterdefaultPrinter from Apollo Clienttypeof fetchfetchRequestInitundefinedstringundefined{ [headerName: string]: string }undefinedbooleanfalsebooleanfalseThe link automatically scans GraphQL variables for extractable files using the configured isExtractableFile function. When files are detected:
formDataAppendFile functionWhen no files are detected in variables:
useGETForQueries is true, otherwise POSTThe link integrates with Apollo Client's error handling system: