-
Notifications
You must be signed in to change notification settings - Fork 38
/
schema.js
54 lines (50 loc) · 1.16 KB
/
schema.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
48
49
50
51
52
53
54
const { makeExecutableSchema } = require("@graphql-tools/schema");
// does not work
// const { GraphQLUpload } = require("graphql-upload");
// does work
const { GraphQLUpload } =require("@graphql-tools/links");
module.exports = makeExecutableSchema({
typeDefs: `
scalar Upload
type SomeFile {
filename: String
mimetype: String
content: String
}
type Mutation {
uploadFile(input: Upload!): SomeFile!
}
type Query {
errorCodes: [String!]!
}
`,
resolvers: {
Upload: GraphQLUpload,
Mutation: {
uploadFile: async (
_,
{ input }
) => {
const { createReadStream, filename, mimetype } = await input;
const chunks = [];
const stream = createReadStream();
for await (const chunk of stream) {
chunks.push(Buffer.from(chunk));
}
const buf = Buffer.concat(chunks);
return {
filename,
mimetype,
content: buf.toString()
}
},
},
Query: {
errorCodes: () => [
"NOT_FOUND",
"GRAPHQL_PARSE_FAILED",
"GRAPHQL_VALIDATION_FAILED",
],
},
},
});