Hi everyone, I’m try implement graphql with apollo server lambda in my enterprise but i have problems with upload files.
When I upload one file, he is empty and corrupted. for this implementation I use the code below.
Anybody can help me?
Server
const express = require('express');
import { ApolloServer } from 'apollo-server-lambda';
const { graphqlUploadExpress } = require('graphql-upload');
import typeDefs from './graphql/typeDefs';
import resolvers from './graphql/resolvers';
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({}),
});
export const graphqlHandler = server.createHandler({
expressAppFromMiddleware(middleware) {
const app = express();
app.use(graphqlUploadExpress());
app.use(middleware);
return app;
},
});
Schema
scalar Upload
type File {
filename: String!
mimetype: String!
encoding: String!
}
type Query {
otherFields: Boolean!
}
type Mutation {
singleUpload(file: Upload!): File!
}
Resolver
const { finished } = require('stream/promises');
const { GraphQLUpload } = require('graphql-upload');
const resolvers = {
Upload: GraphQLUpload,
Mutation: {
singleUpload: async (parent: any, { file }: any) => {
const { createReadStream, filename, mimetype, encoding } = await file;
const stream = createReadStream();
const out = require('fs').createWriteStream(filename);
stream.pipe(out);
await finished(out);
return { filename, mimetype, encoding };
},
},
};
export default resolvers;