Skip to content

Commit

Permalink
28-03-mutation-resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
leo41271 committed Jul 30, 2024
1 parent 43a4114 commit b797dbd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
29 changes: 24 additions & 5 deletions 28 GraphQL/backend/graphql/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
const bcrypt = require('bcryptjs');

const User = require('../models/user');

/**
* All ROOT RESOLVERS RECEIVE (args, context, info) ARGS
* NOTE CONFIGURATION IN app.js INJECTING req,res INTO context ARG
*/
module.exports = {
hello() {
return {
text: 'Hello World!',
views: 1,
};
createUser: async ({ userInput }, _context, _info) => {
// const email = source.userInput.email;

This comment has been minimized.

Copy link
@leo41271

leo41271 Aug 30, 2024

Author Owner

不用解構的話 代表的是 args 你傳的物件 在前面的schema 定義中它是 一個 userInput,我們再拿取此type 的 email 。

const existingUser = await User.findOne({ email: userInput.email });
if (existingUser) {
const error = new Error('User exists already!');
throw error;
}

const hashPw = await bcrypt.hash(userInput.password, 12);
const user = new User({
email: userInput.email,
name: userInput.name,
password: hashPw,
});
const createdUser = await user.save();
return { ...createdUser._doc, _id: createdUser._id.toString() };

This comment has been minimized.

Copy link
@leo41271

leo41271 Aug 30, 2024

Author Owner

mongoose 的資料回傳格式(排除meta data 的 乾淨 USER 資料)

},
};
5 changes: 5 additions & 0 deletions 28 GraphQL/backend/graphql/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ module.exports = buildSchema(`
password: String!
}
type RootQuery {
hello: String
}
type RootMutation {
createUser(userInput: UserData): User
}
schema {
query: RootQuery
mutation: RootMutation
}
`);

0 comments on commit b797dbd

Please sign in to comment.