Registry /
storage / graphql-upload-commonjs
Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
graphqlUploadKoa
✓ import { graphqlUploadKoa } from 'graphql-upload-commonjs'
✗ const graphqlUploadKoa = require('graphql-upload-commonjs').graphqlUploadKoa
ESM import is preferred but named export also works with CommonJS require.
graphqlUploadExpress
✓ import { graphqlUploadExpress } from 'graphql-upload-commonjs'
✗ const { graphqlUploadExpress } = require('graphql-upload-commonjs')
Both ESM and CommonJS supported but ESM import is recommended.
processRequest
✓ import { processRequest } from 'graphql-upload-commonjs'
✗ const processRequest = require('graphql-upload-commonjs').processRequest
Use for custom middleware; exports both as named export.
GraphQLUpload
✓ import { GraphQLUpload } from 'graphql-upload-commonjs'
✗ import GraphQLUpload from 'graphql-upload-commonjs'
GraphQLUpload is a named export, not default.
Express server with graphql-upload middleware handling single file upload mutation.
import express from 'express';
import { graphqlHTTP } from 'express-graphql';
import { buildSchema, GraphQLScalarType } from 'graphql';
import { graphqlUploadExpress, GraphQLUpload } from 'graphql-upload-commonjs';
const schema = buildSchema(`
scalar Upload
type File {
filename: String
mimetype: String
encoding: String
}
type Query {
uploads: [File]
}
type Mutation {
singleUpload(file: Upload!): File!
}
`);
const resolvers = {
Upload: GraphQLUpload,
Query: {
uploads: () => []
},
Mutation: {
singleUpload: async (parent, { file }) => {
const { createReadStream, filename, mimetype, encoding } = await file;
const stream = createReadStream();
// Process stream...
return { filename, mimetype, encoding };
}
}
};
const app = express();
app.use(graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }));
app.use('/graphql', graphqlHTTP({ schema, rootValue: resolvers, graphiql: true }));
app.listen(4000, () => console.log('Server running on http://localhost:4000/graphql'));
Errors
Common errors & fixes
Cannot find module 'graphql-upload-commonjs'
Package not installed or version mismatch
fixRun npm install graphql-upload-commonjs graphql
TypeError: Cannot destructure property 'createReadStream' of 'file' as it is undefined.
Resolver not awaited the file promise
fixUse await file before destructuring: const { createReadStream, filename, mimetype, encoding } = await file; GraphQLUpload is not a constructor
Imported GraphQLUpload incorrectly as default instead of named export
fixUse import { GraphQLUpload } from 'graphql-upload-commonjs' Expected Upload to be a GraphQL scalar type
GraphQLUpload not assigned to the Upload scalar in schema resolvers
fixSet the resolvers.Upload property to GraphQLUpload
Upgrade
Version history
16.0.2-beta.3latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency for GraphQL schema building and execution
@types/expressoptionalPeer dependency for Express middleware types when using TypeScript
@types/koaoptionalPeer dependency for Koa middleware types when using TypeScript