Skip to content

Commit

Permalink
Step 8.4: Add Input Types to Resolvers and Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
srtucker22 authored and Simon Tucker committed Aug 26, 2018
1 parent 1d075ca commit 0033e7c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
18 changes: 13 additions & 5 deletions server/data/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export const messageLogic = {
to(message) {
return message.getGroup({ attributes: ['id', 'name'] });
},
createMessage(_, { text, groupId }, ctx) {
createMessage(_, createMessageInput, ctx) {
const { text, groupId } = createMessageInput.message;

return getAuthenticatedUser(ctx)
.then(user => user.getGroups({ where: { id: groupId }, attributes: ['id'] })
.then((group) => {
Expand All @@ -38,7 +40,9 @@ export const groupLogic = {
users(group) {
return group.getUsers({ attributes: ['id', 'username'] });
},
messages(group, { first, last, before, after }) {
messages(group, { messageConnection = {} }) {
const { first, last, before, after } = messageConnection;

// base query -- get messages from the right group
const where = { groupId: group.id };

Expand Down Expand Up @@ -105,7 +109,9 @@ export const groupLogic = {
}],
}));
},
createGroup(_, { name, userIds }, ctx) {
createGroup(_, createGroupInput, ctx) {
const { name, userIds } = createGroupInput.group;

return getAuthenticatedUser(ctx)
.then(user => user.getFriends({ where: { id: { $in: userIds } } })
.then((friends) => { // eslint-disable-line arrow-body-style
Expand Down Expand Up @@ -158,8 +164,10 @@ export const groupLogic = {
});
});
},
updateGroup(_, { id, name }, ctx) {
return getAuthenticatedUser(ctx).then((user) => { // eslint-disable-line arrow-body-style
updateGroup(_, updateGroupInput, ctx) {
const { id, name } = updateGroupInput.group;

return getAuthenticatedUser(ctx).then((user) => { // eslint-disable-line arrow-body-style
return Group.findOne({
where: { id },
include: [{
Expand Down
8 changes: 6 additions & 2 deletions server/data/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ export const resolvers = {
updateGroup(_, args, ctx) {
return groupLogic.updateGroup(_, args, ctx);
},
login(_, { email, password }, ctx) {
login(_, signinUserInput, ctx) {
// find user by email
const { email, password } = signinUserInput.user;

return User.findOne({ where: { email } }).then((user) => {
if (user) {
// validate password
Expand All @@ -80,7 +82,9 @@ export const resolvers = {
return Promise.reject('email not found');
});
},
signup(_, { email, password, username }, ctx) {
signup(_, signinUserInput, ctx) {
const { email, password, username } = signinUserInput.user;

// find user by email
return User.findOne({ where: { email } }).then((existing) => {
if (!existing) {
Expand Down

0 comments on commit 0033e7c

Please sign in to comment.