jaydenseric/apollo-upload-client
关于
用于 Apollo Client 的终止型 Apollo Link:如果 GraphQL 变量包含文件(默认支持 FileList、File 或 Blob 实例),则发送 GraphQL multipart 请求;否则根据配置和 GraphQL 操作发送常规的 GraphQL POST 或 GET 请求。
网站与应用开发实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- -1
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文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
- Node.js versions
^22.13.0 || ^24.0.0 || >=26.0.0. - Browsers matching the Browserslist query
> 0.5%, not OperaMini all, not dead.
Projects must configure TypeScript to use types from the ECMAScript modules that have a // @ts-check comment:
compilerOptions.allowJsshould betrue.compilerOptions.maxNodeModuleJsDepthshould be reasonably large, e.g.10.compilerOptions.moduleshould be"node16"or"nodenext".
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:
探索相关项目
加载中…