Use graphql-upload instead of the outdated apollo-server setup.

For context see: https://github.com/jaydenseric/graphql-upload/issues/109.
This commit is contained in:
Jayden Seric 2018-10-29 21:35:56 +11:00
parent 6b56aa3819
commit 921aa517ba
5 changed files with 39 additions and 3 deletions

16
api/package-lock.json generated
View File

@ -1952,6 +1952,11 @@
"integrity": "sha1-g8YK/Fi5xWmXAH7Rp2izqzA6RP4=",
"dev": true
},
"fs-capacitor": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/fs-capacitor/-/fs-capacitor-1.0.1.tgz",
"integrity": "sha512-XdZK0Q78WP29Vm3FGgJRhRhrBm51PagovzWtW2kJ3Q6cYJbGtZqWSGTSPwvtEkyjIirFd7b8Yes/dpOYjt4RRQ=="
},
"fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@ -2653,6 +2658,17 @@
"uuid": "^3.1.0"
}
},
"graphql-upload": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/graphql-upload/-/graphql-upload-8.0.0.tgz",
"integrity": "sha512-DlEi6+Kblj6gAAA5XJahJl1+UfcXICiFypJYIxd4zK26W2/LKm6nwLmSWjECwbAkz/OtB+oSWGb5gqXCDQOLqg==",
"requires": {
"busboy": "^0.2.14",
"fs-capacitor": "^1.0.0",
"http-errors": "^1.7.1",
"object-path": "^0.11.4"
}
},
"has": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",

View File

@ -14,6 +14,7 @@
"apollo-server-koa": "^2.1.0",
"dotenv": "^6.1.0",
"graphql": "^14.0.2",
"graphql-upload": "^8.0.0",
"koa": "^2.6.1",
"lowdb": "^1.0.0",
"mkdirp": "^0.5.1",

View File

@ -1,4 +1,5 @@
import fs from 'fs'
import { GraphQLUpload } from 'graphql-upload'
import promisesAll from 'promises-all'
import mkdirp from 'mkdirp'
import shortid from 'shortid'
@ -39,12 +40,14 @@ const storeDB = file =>
.write()
const processUpload = async upload => {
const { stream, filename, mimetype } = await upload
const { createReadStream, filename, mimetype } = await upload
const stream = createReadStream()
const { id, path } = await storeFS({ stream, filename })
return storeDB({ id, filename, mimetype, path })
}
export default {
Upload: GraphQLUpload,
Query: {
uploads: () => db.get('uploads').value()
},

View File

@ -2,14 +2,28 @@ import Koa from 'koa'
import apolloServerKoa from 'apollo-server-koa'
import typeDefs from './types.mjs'
import resolvers from './resolvers.mjs'
import { graphqlUploadKoa } from 'graphql-upload'
const app = new Koa()
const server = new apolloServerKoa.ApolloServer({ typeDefs, resolvers })
const app = new Koa().use(
graphqlUploadKoa({
maxFileSize: 10000000, // 10 MB
maxFiles: 20
})
)
const server = new apolloServerKoa.ApolloServer({
typeDefs,
resolvers,
// Disable outdated built in uploads, to setup graphql-upload instead.
uploads: false
})
server.applyMiddleware({ app })
app.listen(process.env.PORT, error => {
if (error) throw error
// eslint-disable-next-line no-console
console.info(
`Serving http://localhost:${process.env.PORT} for ${process.env.NODE_ENV}.`

View File

@ -1,4 +1,6 @@
export default /* GraphQL */ `
scalar Upload
type File {
id: ID!
path: String!