graphql-upload-nextjs 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +311 -364
- package/index.d.ts +37 -23
- package/index.js +216 -110
- package/package.json +71 -62
package/README.md
CHANGED
|
@@ -1,364 +1,311 @@
|
|
|
1
|
-
# graphql-upload-nextjs
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
-
|
|
9
|
-
|
|
10
|
-
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
import
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
```
|
|
314
|
-
|
|
315
|
-
*GraphQL Variables:*
|
|
316
|
-
Similarly, the key `"files"` must match the argument name (`$files`).
|
|
317
|
-
When using a GraphQL client library:
|
|
318
|
-
* You'll pass an array of `File` objects as the value for the `files` variable.
|
|
319
|
-
* The client library handles the multipart request construction.
|
|
320
|
-
|
|
321
|
-
The `{"files": [null, null]}` structure illustrates the `operations` part, with `null` placeholders for file content sent separately.
|
|
322
|
-
|
|
323
|
-
*Example with a client library (conceptual):*
|
|
324
|
-
```javascript
|
|
325
|
-
// In your frontend code
|
|
326
|
-
const UPLOAD_FILES_MUTATION = gql`
|
|
327
|
-
mutation UploadFiles($files: [Upload!]!) {
|
|
328
|
-
uploadFiles(files: $files) { fileName }
|
|
329
|
-
}
|
|
330
|
-
`;
|
|
331
|
-
|
|
332
|
-
function MyMultiUploader() {
|
|
333
|
-
const [uploadFilesMutation] = useMutation(UPLOAD_FILES_MUTATION);
|
|
334
|
-
|
|
335
|
-
const handleChange = (event) => {
|
|
336
|
-
const files = Array.from(event.target.files);
|
|
337
|
-
if (files.length > 0) {
|
|
338
|
-
uploadFilesMutation({ variables: { files } });
|
|
339
|
-
}
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
return <input type="file" multiple onChange={handleChange} />;
|
|
343
|
-
}
|
|
344
|
-
```
|
|
345
|
-
|
|
346
|
-
## Example
|
|
347
|
-
|
|
348
|
-
An example project demonstrating how to integrate GraphQL file uploads into a typical Next.js starter application is available in the repository under `graphql-upload-nextjs/examples/example-graphql-upload-nextjs/`.
|
|
349
|
-
|
|
350
|
-
## Contributing
|
|
351
|
-
|
|
352
|
-
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
|
|
353
|
-
|
|
354
|
-
## License
|
|
355
|
-
|
|
356
|
-
This project is licensed under the MIT License. See the [LICENSE](https://github.com/lafittemehdy/graphql-upload-nextjs/blob/master/LICENSE) file for more information.
|
|
357
|
-
|
|
358
|
-
## Acknowledgements
|
|
359
|
-
|
|
360
|
-
I would like to express my sincere gratitude to [meabed](https://github.com/meabed) for their excellent work on [graphql-upload-ts](https://github.com/meabed/graphql-upload-ts), which served as a valuable reference and inspiration for this project. I am also grateful to [jaydenseric](https://github.com/jaydenseric) for developing the original specifications for [graphql-upload](https://github.com/jaydenseric/graphql-upload).
|
|
361
|
-
|
|
362
|
-
While this project deviates from the official specifications to prioritize compatibility with Next.js routes, I am committed to refining it further to align with those specifications as closely as possible. Notable enhancements include built-in security features, such as file type verification, as well as support and an example for GraphQL authentication.
|
|
363
|
-
|
|
364
|
-
Finally, I would like to extend my heartfelt gratitude to my mom for her unwavering support, which has allowed me to dedicate my time to working on open-source software.
|
|
1
|
+
# graphql-upload-nextjs
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/graphql-upload-nextjs)
|
|
4
|
+
[](https://github.com/lafittemehdy/graphql-upload-nextjs/actions/workflows/ci.yml)
|
|
5
|
+
[](https://github.com/lafittemehdy/graphql-upload-nextjs/blob/master/LICENSE)
|
|
6
|
+
[](https://github.com/jaydenseric/graphql-multipart-request-spec)
|
|
7
|
+
[](https://nodejs.org)
|
|
8
|
+
[](https://www.typescriptlang.org)
|
|
9
|
+
|
|
10
|
+
A [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec) implementation for [Next.js](https://nextjs.org) App Router with [Apollo Server](https://www.apollographql.com/docs/apollo-server). Enables file uploads via GraphQL mutations using the `Upload` scalar, with built-in MIME type verification via magic bytes.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- Implements the [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
15
|
+
- Designed for Next.js App Router route handlers.
|
|
16
|
+
- Supports single file uploads, multiple file uploads, and operation batching.
|
|
17
|
+
- File deduplication (one file mapped to multiple operation paths).
|
|
18
|
+
- MIME type verified via magic bytes using [file-type](https://npm.im/file-type), not trusting client headers.
|
|
19
|
+
- Configurable `allowedTypes`, `maxFileSize`, and `maxFiles`.
|
|
20
|
+
- Only reads 4KB for MIME detection — streams the rest directly from `Blob`.
|
|
21
|
+
- Spec-compatible property names: `filename`, `mimetype`, `encoding`, `createReadStream`.
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install graphql-upload-nextjs
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Migrating from graphql-upload
|
|
30
|
+
|
|
31
|
+
[graphql-upload](https://github.com/jaydenseric/graphql-upload) uses Express/Koa middleware that is incompatible with Next.js App Router route handlers. This package provides the same `Upload` scalar and file object interface for the Next.js environment.
|
|
32
|
+
|
|
33
|
+
Property names match the original: `filename`, `mimetype`, `encoding`, `createReadStream`. An additional `fileSize` property is also available.
|
|
34
|
+
|
|
35
|
+
**Before (graphql-upload with Express):**
|
|
36
|
+
```typescript
|
|
37
|
+
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs'
|
|
38
|
+
app.use(graphqlUploadExpress())
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**After (graphql-upload-nextjs with App Router):**
|
|
42
|
+
```typescript
|
|
43
|
+
import { GraphQLUpload, uploadProcess } from 'graphql-upload-nextjs'
|
|
44
|
+
|
|
45
|
+
// In your route handler:
|
|
46
|
+
if (request.headers.get("content-type")?.includes("multipart/form-data")) {
|
|
47
|
+
return await uploadProcess(request, context, server);
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Resolver code stays the same — `filename`, `mimetype`, `encoding`, and `createReadStream` work identically.
|
|
52
|
+
|
|
53
|
+
## Usage
|
|
54
|
+
|
|
55
|
+
### Schema and Resolvers
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import path from 'node:path'
|
|
59
|
+
import { createWriteStream } from 'node:fs'
|
|
60
|
+
import { pipeline } from 'node:stream'
|
|
61
|
+
import { gql } from '@apollo/client' // Optional: syntax highlighting only.
|
|
62
|
+
import { ApolloServer } from '@apollo/server'
|
|
63
|
+
import { startServerAndCreateNextHandler } from '@as-integrations/next'
|
|
64
|
+
import { type File, GraphQLUpload, uploadProcess } from 'graphql-upload-nextjs'
|
|
65
|
+
import type { NextRequest } from 'next/server.js'
|
|
66
|
+
|
|
67
|
+
const typeDefs = gql`
|
|
68
|
+
scalar Upload
|
|
69
|
+
type File {
|
|
70
|
+
encoding: String!
|
|
71
|
+
filename: String!
|
|
72
|
+
fileSize: Int!
|
|
73
|
+
mimetype: String!
|
|
74
|
+
uri: String!
|
|
75
|
+
}
|
|
76
|
+
type Query {
|
|
77
|
+
default: Boolean!
|
|
78
|
+
}
|
|
79
|
+
type Mutation {
|
|
80
|
+
uploadFile(file: Upload!): File!
|
|
81
|
+
uploadFiles(files: [Upload!]!): [File!]!
|
|
82
|
+
}
|
|
83
|
+
`
|
|
84
|
+
|
|
85
|
+
interface FileResponse {
|
|
86
|
+
encoding: string;
|
|
87
|
+
filename: string;
|
|
88
|
+
fileSize: number;
|
|
89
|
+
mimetype: string;
|
|
90
|
+
uri: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const resolvers = {
|
|
94
|
+
Mutation: {
|
|
95
|
+
uploadFile: async (
|
|
96
|
+
_parent: undefined,
|
|
97
|
+
{ file }: { file: Promise<File> },
|
|
98
|
+
): Promise<FileResponse> => {
|
|
99
|
+
const { createReadStream, encoding, filename, fileSize, mimetype } = await file;
|
|
100
|
+
const safeName = path.basename(filename);
|
|
101
|
+
return new Promise((resolve, reject) => {
|
|
102
|
+
pipeline(
|
|
103
|
+
createReadStream(),
|
|
104
|
+
createWriteStream(`./uploads/${safeName}`),
|
|
105
|
+
(error) => {
|
|
106
|
+
if (error) reject(new Error("Error during file upload."));
|
|
107
|
+
else resolve({ encoding, filename: safeName, fileSize, mimetype, uri: `/${safeName}` });
|
|
108
|
+
},
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
uploadFiles: async (
|
|
113
|
+
_parent: undefined,
|
|
114
|
+
{ files }: { files: Promise<File>[] },
|
|
115
|
+
): Promise<FileResponse[]> => {
|
|
116
|
+
const resolvedFiles = await Promise.all(files);
|
|
117
|
+
return Promise.all(resolvedFiles.map(async ({ createReadStream, encoding, filename, fileSize, mimetype }) => {
|
|
118
|
+
const safeName = path.basename(filename);
|
|
119
|
+
return new Promise<FileResponse>((resolve, reject) => {
|
|
120
|
+
pipeline(
|
|
121
|
+
createReadStream(),
|
|
122
|
+
createWriteStream(`./uploads/${safeName}`),
|
|
123
|
+
(error) => {
|
|
124
|
+
if (error) reject(new Error(`Error during upload of ${safeName}.`));
|
|
125
|
+
else resolve({ encoding, filename: safeName, fileSize, mimetype, uri: `/${safeName}` });
|
|
126
|
+
},
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
}));
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
Query: { default: async () => true },
|
|
133
|
+
Upload: GraphQLUpload,
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
> **Security:** Always sanitize filenames with `path.basename()` to prevent path traversal attacks. Never write uploaded files to publicly accessible directories in production.
|
|
138
|
+
|
|
139
|
+
### Route Handler
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
const server = new ApolloServer({ resolvers, typeDefs });
|
|
143
|
+
|
|
144
|
+
const handler = startServerAndCreateNextHandler<NextRequest>(server, {
|
|
145
|
+
context: async (req: NextRequest) => ({
|
|
146
|
+
ip: req.headers.get("x-forwarded-for") || "",
|
|
147
|
+
req,
|
|
148
|
+
}),
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const requestHandler = async (request: NextRequest) => {
|
|
152
|
+
if (request.headers.get("content-type")?.includes("multipart/form-data")) {
|
|
153
|
+
const context = { ip: request.headers.get("x-forwarded-for") || "", req: request };
|
|
154
|
+
return await uploadProcess(request, context, server);
|
|
155
|
+
}
|
|
156
|
+
return handler(request);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export const GET = requestHandler;
|
|
160
|
+
export const POST = requestHandler;
|
|
161
|
+
export const OPTIONS = requestHandler;
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Configuration
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
await uploadProcess(request, context, server, {
|
|
168
|
+
allowedTypes: ["image/jpeg", "image/png", "text/plain"],
|
|
169
|
+
maxFiles: 10,
|
|
170
|
+
maxFileSize: 10 * 1024 * 1024, // 10MB
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
| Option | Type | Default | Description |
|
|
175
|
+
|---|---|---|---|
|
|
176
|
+
| `allowedTypes` | `string[]` | `undefined` | Restrict MIME types. Verified via magic bytes. |
|
|
177
|
+
| `maxFiles` | `number` | `undefined` | Max files per request. Returns 413 if exceeded. |
|
|
178
|
+
| `maxFileSize` | `number` | `undefined` | Max file size in bytes. Returns 413 if exceeded. |
|
|
179
|
+
|
|
180
|
+
### Client Usage
|
|
181
|
+
|
|
182
|
+
When using a GraphQL client library ([apollo-upload-client](https://npm.im/apollo-upload-client), [urql](https://npm.im/@urql/exchange-multipart-fetch), [extract-files](https://npm.im/extract-files)), file uploads are constructed automatically per the spec.
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
import { gql, useMutation } from '@apollo/client';
|
|
186
|
+
|
|
187
|
+
const UPLOAD = gql`
|
|
188
|
+
mutation UploadFile($file: Upload!) {
|
|
189
|
+
uploadFile(file: $file) { filename mimetype fileSize }
|
|
190
|
+
}
|
|
191
|
+
`;
|
|
192
|
+
|
|
193
|
+
function Uploader() {
|
|
194
|
+
const [upload] = useMutation(UPLOAD);
|
|
195
|
+
return <input type="file" onChange={(e) => {
|
|
196
|
+
const file = e.target.files?.[0];
|
|
197
|
+
if (file) upload({ variables: { file } });
|
|
198
|
+
}} />;
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## API Reference
|
|
203
|
+
|
|
204
|
+
### Core Exports
|
|
205
|
+
|
|
206
|
+
| Export | Type | Description |
|
|
207
|
+
|---|---|---|
|
|
208
|
+
| `GraphQLUpload` | `GraphQLScalarType` | The `Upload` scalar for your GraphQL schema. |
|
|
209
|
+
| `uploadProcess` | `function` | Processes a multipart request with file uploads. |
|
|
210
|
+
| `Upload` | `class` | Holds a promise that resolves with file upload details. |
|
|
211
|
+
|
|
212
|
+
### File Object
|
|
213
|
+
|
|
214
|
+
The resolved file object passed to resolvers:
|
|
215
|
+
|
|
216
|
+
| Property | Type | Description |
|
|
217
|
+
|---|---|---|
|
|
218
|
+
| `createReadStream` | `() => ReadableStream` | Creates a Node.js readable stream of the file contents. Replayable. |
|
|
219
|
+
| `encoding` | `string` | Transfer encoding (always `'binary'` with FormData API). |
|
|
220
|
+
| `filename` | `string` | Original file name from the client. |
|
|
221
|
+
| `fileSize` | `number` | File size in bytes. |
|
|
222
|
+
| `mimetype` | `string` | MIME type verified via magic bytes. |
|
|
223
|
+
|
|
224
|
+
### Utility Exports
|
|
225
|
+
|
|
226
|
+
| Export | Type | Description |
|
|
227
|
+
|---|---|---|
|
|
228
|
+
| `bufferToStream` | `(buffer: Buffer) => ReadableStream` | Converts a Buffer to a Node.js readable stream. |
|
|
229
|
+
| `parseOperationsJSON` | `(input: string) => object \| object[]` | Parses the operations field (object or array for batching). |
|
|
230
|
+
| `sanitizeAndValidateJSON` | `(input: string) => object` | Parses JSON and validates it's a non-null, non-array object. |
|
|
231
|
+
| `setValueAtPath` | `(obj, path, value) => void` | Sets a value at a dot-notation path in a nested object. |
|
|
232
|
+
| `streamToBuffer` | `(stream: ReadableStream) => Promise<Buffer>` | Collects a readable stream into a Buffer. |
|
|
233
|
+
| `validateMap` | `(map: object) => string \| null` | Validates map entries are arrays of string paths. Returns error or null. |
|
|
234
|
+
|
|
235
|
+
### TypeScript Interfaces
|
|
236
|
+
|
|
237
|
+
| Export | Description |
|
|
238
|
+
|---|---|
|
|
239
|
+
| `File` | Resolved file object with `filename`, `mimetype`, `encoding`, `fileSize`, `createReadStream`. |
|
|
240
|
+
| `FileStream` | FormData file entry with a `stream()` method. |
|
|
241
|
+
| `FormDataFile` | Raw file entry from multipart FormData. |
|
|
242
|
+
| `MinimalRequest` | Request interface compatible with Next.js and Web API. |
|
|
243
|
+
|
|
244
|
+
## Spec Compliance
|
|
245
|
+
|
|
246
|
+
Implements the [GraphQL multipart request specification](https://github.com/jaydenseric/graphql-multipart-request-spec) by [jaydenseric](https://github.com/jaydenseric).
|
|
247
|
+
|
|
248
|
+
The spec defines a [multipart form field structure](https://github.com/jaydenseric/graphql-multipart-request-spec#multipart-form-field-structure) with three ordered fields (`operations`, `map`, file fields) and enables nesting files anywhere within operations, operation batching, file deduplication, and file upload streams in resolvers.
|
|
249
|
+
|
|
250
|
+
### Supported Capabilities
|
|
251
|
+
|
|
252
|
+
| Spec Capability | Status | Details |
|
|
253
|
+
|---|---|---|
|
|
254
|
+
| `operations` field ([JSON-encoded GraphQL operation](https://github.com/jaydenseric/graphql-multipart-request-spec#multipart-form-field-structure)) | Supported | Parsed and validated as object or array. |
|
|
255
|
+
| `map` field ([file-to-path mapping](https://github.com/jaydenseric/graphql-multipart-request-spec#multipart-form-field-structure)) | Supported | Validated: each entry must be an array of string paths. |
|
|
256
|
+
| [Single file upload](https://github.com/jaydenseric/graphql-multipart-request-spec#single-file) | Supported | File mapped via `"variables.file"` path. |
|
|
257
|
+
| [File list upload](https://github.com/jaydenseric/graphql-multipart-request-spec#file-list) | Supported | Files mapped via `"variables.files.0"`, `"variables.files.1"`, etc. |
|
|
258
|
+
| [Batching](https://github.com/jaydenseric/graphql-multipart-request-spec#batching) | Supported | Operations as array, paths prefixed with operation index (`"0.variables.file"`). |
|
|
259
|
+
| File deduplication | Supported | One file field mapped to multiple operation paths. |
|
|
260
|
+
| [`object-path`](https://npm.im/object-path) dot-notation | Supported | Handles nested objects and array indices. |
|
|
261
|
+
| File upload streams in resolvers | Supported | `createReadStream()` returns a Node.js readable stream. |
|
|
262
|
+
| Missing file → rejected promise | Supported | Upload promise rejected with `"File missing in the request."` |
|
|
263
|
+
| `maxFiles` / `maxFileSize` limits | Supported | Returns HTTP 413 when exceeded. |
|
|
264
|
+
|
|
265
|
+
### Additions Beyond the Spec
|
|
266
|
+
|
|
267
|
+
| Feature | Description |
|
|
268
|
+
|---|---|
|
|
269
|
+
| MIME magic byte verification | Real file type detected via [file-type](https://npm.im/file-type), not trusting client-provided `Content-Type`. |
|
|
270
|
+
| `allowedTypes` filtering | Server-side restriction of accepted MIME types. |
|
|
271
|
+
| `fileSize` property | File size in bytes available on the resolved file object. |
|
|
272
|
+
|
|
273
|
+
### Limitations
|
|
274
|
+
|
|
275
|
+
These are inherent trade-offs of using the Web `FormData` API for Next.js App Router compatibility. They do not affect normal spec usage.
|
|
276
|
+
|
|
277
|
+
| Limitation | Reason |
|
|
278
|
+
|---|---|
|
|
279
|
+
| **No field ordering validation** | The [spec requires](https://github.com/jaydenseric/graphql-multipart-request-spec#multipart-form-field-structure) `operations` → `map` → files ordering. The Web `FormData` API retrieves fields by name (`formData.get('operations')`), not by position, so ordering cannot be validated. In practice, all major client libraries ([apollo-upload-client](https://npm.im/apollo-upload-client), [extract-files](https://npm.im/extract-files)) send fields in the correct order. |
|
|
280
|
+
| **No `maxFieldSize`** | The original [graphql-upload](https://github.com/jaydenseric/graphql-upload) limits non-file field sizes via `busboy`. Next.js parses the full request body into `FormData` before our code runs, so field size limiting is not possible at this layer. |
|
|
281
|
+
| **`encoding` is always `'binary'`** | `FormData` does not expose the transfer encoding of file parts. The original `graphql-upload` reads this from `busboy`'s stream events. In practice, transfer encoding is rarely used by resolvers. |
|
|
282
|
+
| **No mid-stream abort** | The original uses `fs-capacitor` to buffer uploads to disk, allowing resolvers to abort an in-progress upload. With `FormData`, the entire request body is already parsed by the runtime. However, resolvers can still choose not to call `createReadStream()` to skip processing. |
|
|
283
|
+
| **`Blob.stream()` instead of `busboy` streaming** | Next.js route handlers receive a `Request` object (Web API), not a Node.js `IncomingMessage`. There is no access to the raw request stream. `Blob.stream()` provides a replayable readable stream per file. |
|
|
284
|
+
|
|
285
|
+
## Example
|
|
286
|
+
|
|
287
|
+
A full example project is available at [`examples/example-graphql-upload-nextjs/`](examples/example-graphql-upload-nextjs/).
|
|
288
|
+
|
|
289
|
+
## Contributing
|
|
290
|
+
|
|
291
|
+
Contributions are welcome! To get started:
|
|
292
|
+
|
|
293
|
+
```bash
|
|
294
|
+
git clone https://github.com/lafittemehdy/graphql-upload-nextjs.git
|
|
295
|
+
cd graphql-upload-nextjs
|
|
296
|
+
npm install
|
|
297
|
+
npm run build
|
|
298
|
+
npm test
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
CI runs automatically on push and pull requests via GitHub Actions (build + test on Node.js 22/24, example build + lint).
|
|
302
|
+
|
|
303
|
+
## License
|
|
304
|
+
|
|
305
|
+
MIT. See [LICENSE](https://github.com/lafittemehdy/graphql-upload-nextjs/blob/master/LICENSE).
|
|
306
|
+
|
|
307
|
+
## Acknowledgements
|
|
308
|
+
|
|
309
|
+
Sincere gratitude to [jaydenseric](https://github.com/jaydenseric) for the [GraphQL multipart request specification](https://github.com/jaydenseric/graphql-multipart-request-spec) and [graphql-upload](https://github.com/jaydenseric/graphql-upload), and to [meabed](https://github.com/meabed) for [graphql-upload-ts](https://github.com/meabed/graphql-upload-ts) which served as a valuable reference.
|
|
310
|
+
|
|
311
|
+
Finally, heartfelt gratitude to my mom for her unwavering support, which has allowed me to dedicate my time to working on open-source software.
|