-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
47 lines (38 loc) · 1.1 KB
/
app.js
1
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
38
39
40
41
42
43
44
45
46
47
const { ApolloServer, gql } = require("apollo-server-express");
const { createWriteStream, existsSync, mkdirSync } = require("fs");
const path = require("path");
const express = require("express");
const files = [];
const typeDefs = gql`
type Query {
files: [String]
}
type Mutation {
uploadFile(file: Upload!): Boolean
}
`;
const resolvers = {
Query: {
files: () => files
},
Mutation: {
uploadFile: async (_, { file }) => {
const { createReadStream, filename } = await file;
await new Promise(res =>
createReadStream()
.pipe(createWriteStream(path.join(__dirname, "public/images", filename)))
.on("close", res)
);
files.push(filename);
return true;
}
}
};
existsSync(path.join(__dirname, "../images")) || mkdirSync(path.join(__dirname, "../images"));
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
app.use("/images", express.static(path.join(__dirname, "../images")));
server.applyMiddleware({ app });
app.listen(9000, () => {
console.log(`🚀 Server ready at http://localhost:9000/`);
});