/
File uploads
Enabling file uploads in Apollo Server
Note: This feature is incompatible with
graphql-tools
' schema stitching. See this issue for additional details.
For server integrations that support file uploads (e.g. Express, hapi, Koa), Apollo Server enables file uploads by default. To enable file uploads, reference the Upload
type in the schema passed to the Apollo Server construction.
const { ApolloServer, gql } = require('apollo-server');
const typeDefs = gql`
type File {
filename: String!
mimetype: String!
encoding: String!
}
type Query {
uploads: [File]
}
type Mutation {
singleUpload(file: Upload!): File!
}
`;
const resolvers = {
Query: {
uploads: (parent, args) => {},
},
Mutation: {
singleUpload: (parent, args) => {
return args.file.then(file => {
//Contents of Upload scalar: https://github.com/jaydenseric/graphql-upload#class-graphqlupload
//file.stream is a node stream that contains the contents of the uploaded file
//node stream api: https://nodejs.org/api/stream.html
return file;
});
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Note: When using
typeDefs
, Apollo Server addsscalar Upload
to your schema, so any existing declaration ofscalar Upload
in the type definitions should be removed. If you create your schema withmakeExecutableSchema
and pass it toApolloServer
constructor using theschema
param, make sure to includescalar Upload
.