mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2026-02-06 09:55:50 +00:00
This is a curl request from a folder where I have a file `avatar.jpg`:
```
curl localhost:4000/ \
-F operations='{ "query": "mutation($id: ID!, $file: Upload) { UpdateUser(id: $id, avatarUpload: $file) { id name avatar } }", "variables": { "id": "u3", "file": null } }' \
-F map='{ "0": ["variables.file"] }' \
-F 0=@avatar.jpg
```
It uploads the avatar to the public folder. We should
* Write tests
* Change the `user.avatar` url and send it to `/api` in the frontend
30 lines
801 B
JavaScript
30 lines
801 B
JavaScript
import { neo4jgraphql } from 'neo4j-graphql-js'
|
|
import { createWriteStream } from 'fs'
|
|
|
|
|
|
const storeUpload = ({ stream, fileLocation}) =>
|
|
new Promise((resolve, reject) =>
|
|
stream
|
|
.pipe(createWriteStream(`public${fileLocation}`))
|
|
.on("finish", () => resolve())
|
|
.on("error", reject)
|
|
);
|
|
|
|
export default {
|
|
Mutation: {
|
|
UpdateUser: async (object, params, context, resolveInfo) => {
|
|
const { avatarUpload } = params
|
|
|
|
if (avatarUpload) {
|
|
const { stream, filename } = await avatarUpload ;
|
|
const fileLocation = `/uploads/${filename}`
|
|
await storeUpload({ stream, fileLocation });
|
|
delete params.avatarUpload
|
|
|
|
params.avatar = fileLocation
|
|
}
|
|
return await neo4jgraphql(object, params, context, resolveInfo, false)
|
|
}
|
|
},
|
|
};
|