UpdateUser with avatarUpload

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
This commit is contained in:
Robert Schäfer 2019-05-20 19:32:53 +02:00
parent f2577bb06c
commit 4dbf1b2a2c
5 changed files with 38 additions and 3 deletions

View File

@ -106,4 +106,4 @@
"nodemon": "~1.19.0",
"supertest": "~4.0.2"
}
}
}

View File

@ -12,6 +12,7 @@ import rewards from './resolvers/rewards.js'
import socialMedia from './resolvers/socialMedia.js'
import notifications from './resolvers/notifications'
import comments from './resolvers/comments'
import users from './resolvers/users'
export const typeDefs = fs
.readFileSync(
@ -36,6 +37,7 @@ export const resolvers = {
...rewards.Mutation,
...socialMedia.Mutation,
...notifications.Mutation,
...comments.Mutation
...comments.Mutation,
...users.Mutation
}
}

View File

@ -3,7 +3,7 @@ import { UserInputError } from 'apollo-server'
const USERNAME_MIN_LENGTH = 3
const validateUsername = async (resolve, root, args, context, info) => {
if (args.name && args.name.length >= USERNAME_MIN_LENGTH) {
if (!('name' in args) || args.name && args.name.length >= USERNAME_MIN_LENGTH) {
/* eslint-disable-next-line no-return-await */
return await resolve(root, args, context, info)
} else {

View File

@ -0,0 +1,29 @@
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)
}
},
};

View File

@ -1,3 +1,5 @@
scalar Upload
type Query {
isLoggedIn: Boolean!
# Get the currently logged in User based on the given JWT Token
@ -18,6 +20,7 @@ type Query {
)
CommentByPost(postId: ID!): [Comment]!
}
type Mutation {
# Get a JWT Token for the given Email and password
login(email: String!, password: String!): String!
@ -99,6 +102,7 @@ type User {
slug: String
password: String!
avatar: String
avatarUpload: Upload
deleted: Boolean
disabled: Boolean
disabledBy: User @relation(name: "DISABLED", direction: "IN")