@apollographql/apollo-upload-server

中文文档
5.0.3发布于 2018/08/21MITESM + CJS近 30 天下载 5716

Enhances Apollo GraphQL Server for intuitive file uploads via GraphQL mutations.

原始使用文档
文档目录8 节
  1. Setup
  2. Options
  3. Koa
  4. Express
  5. Custom middleware
  6. Upload scalar
  7. Usage
  8. Support

Apollo upload logo

apollo-upload-server

npm version Licence Github issues Github stars Travis status

Enhances Apollo for intuitive file uploads via GraphQL queries or mutations. Use with a GraphQL multipart request spec client implementation such as apollo-upload-client.

Setup

Install with peer dependencies using npm:

npm install apollo-upload-server graphql

Add the middleware just before graphql-server.

Options

  • maxFieldSize (integer): Max allowed non-file multipart form field size in bytes; enough for your queries (default: 1 MB).
  • maxFileSize (integer): Max allowed file size in bytes (default: Infinity).
  • maxFiles (integer): Max allowed number of files (default: Infinity).

Koa

import { apolloUploadKoa } from 'apollo-upload-server'

// …

router.post(
  '/graphql',
  koaBody(),
  apolloUploadKoa(/* Options */),
  graphqlKoa(/* … */)
)

Express

import { apolloUploadExpress } from 'apollo-upload-server'

// …

app.use(
  '/graphql',
  bodyParser.json(),
  apolloUploadExpress(/* Options */),
  graphqlExpress(/* … */)
)

Custom middleware

Middleware wraps the async function processRequest which accepts a Node.js request and an optional options object as arguments. It returns a promise that resolves an operations object for a GraphQL server to consume (usually as the request body). Import it to create custom middleware:

import { processRequest } from 'apollo-upload-server'

Upload scalar

A file upload promise that resolves an object containing:

  • stream
  • filename
  • mimetype
  • encoding

It must be added to your types and resolvers:

import { makeExecutableSchema } from 'graphql-tools'
import { GraphQLUpload } from 'apollo-upload-server'

const schema = makeExecutableSchema({
  typeDefs: `scalar Upload`,
  resolvers: { Upload: GraphQLUpload }
})

Usage

Files uploaded via a GraphQL multipart request appear as Upload scalars in resolver arguments. The upload streams can be used to store the files in the local filesystem or in the cloud. See also apollo-upload-client usage and the example API and client.

Support

  • Node.js v6.10+, see package.json engines.

版本与发布记录

@apollographql/apollo-upload-server 共发布 3 个版本,首次发布于 2018/08/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.3latest2018/08/218 年前
5.0.22018/08/218 年前
5.0.12018/08/218 年前

关于 @apollographql/apollo-upload-server 的常见问题

@apollographql/apollo-upload-server 是什么?

Enhances Apollo GraphQL Server for intuitive file uploads via GraphQL mutations.

如何安装 @apollographql/apollo-upload-server?

在项目目录运行 npm install @apollographql/apollo-upload-server,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:graphql(^0.13.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@apollographql/apollo-upload-server 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/apollographql__apollo-upload-server 是否存在。

@apollographql/apollo-upload-server 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@apollographql/apollo-upload-server 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6.10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@apollographql/apollo-upload-server 可以商用吗?

@apollographql/apollo-upload-server 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@apollographql/apollo-upload-server 的下载量有多少?

根据 npm 官方下载统计,@apollographql/apollo-upload-server 在 2026/10/07 – 2026/11/05 这 30 天内被下载 5,716 次,日均约 191 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apollographql/apollo-upload-server 还在维护吗?

最新版本 5.0.3 发布于 2018/08/21(8 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/08/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。