Skip to content

Commit

Permalink
modified resolvers and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
onprem committed Jun 20, 2019
1 parent 21749ed commit 2d53030
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 50 deletions.
1 change: 1 addition & 0 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const context = ({ req, datab }) => {
id: null,
db: datab
}
//console.log('wonka!: ', req.body, req.headers.authorization);
if (!req.headers.authorization) {
// ulta
} else {
Expand Down
124 changes: 75 additions & 49 deletions src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ const resolvers = {
},
User: {
name: async ( user, _, context ) => {
const users = await context.db.collection('users').find({ _id: ObjectId(user.id) }).project({ name: 1 }).toArray();
const users = await context.db.collection('users').find({ _id: ObjectId(user.id) }).project({ _id: 0, name: 1 }).toArray();
return users[0].name;
},
todos: async ( user, _, context ) => {
const users = await context.db.collection('users').find({ _id: ObjectId(user.id) }).project({ todos: 1 }).toArray();
const users = await context.db.collection('users').find({ _id: ObjectId(user.id) }).project({ _id: 0, todos: 1 }).toArray();
return users[0].todos;
}
},
Mutation: {
login: async ( _, args, context ) =>{
//console.log( args, context.token );
const data = await context.db.collection('users').find({ email: args.email }).project({ _id: 1, email: 1, hash: 1 }).toArray();
const data = await context.db.collection('users').find({ email: args.email }).project({ email: 1, hash: 1 }).toArray();
if( data.length === 1 ){
const match = await bcrypt.compare( args.password, data[0].hash );
if(match){
Expand Down Expand Up @@ -88,62 +88,88 @@ const resolvers = {
return null;
},
addTodo: async ( _, args, context ) => {
const toDo = {
id: `${context.id}-${getRandomInt(9999)}`,
title: args.title,
isComplete: false,
timestamp: Date.now()
}
await context.db.collection('users').updateOne(
{ _id: ObjectId(context.id) },
{ $push:
{ todos: toDo }
if( context.isValid ) {
const toDo = {
id: `${context.id}-${getRandomInt(9999)}`,
title: args.title,
isComplete: false,
timestamp: Date.now()
}
await context.db.collection('users').updateOne(
{ _id: ObjectId(context.id) },
{ $push:
{ todos: toDo }
}
)
return {
code: '200',
success: true,
message: 'successfully added ToDo',
user: {
id: context.id,
email: context.email
}
}
)
return {
code: '200',
success: true,
message: 'successfully added ToDo',
todo: toDo
}
else
throw new AuthenticationError('Authentication required!')
},
toggleTodo: async ( _, args, context ) => {
const [usr] = await context.db.collection('users').aggregate(
{ $match: { _id: ObjectId(context.id) } },
{ $unwind: '$todos' },
{ $match: { 'todos.id': args.id } }
).toArray();
const isComplete = !usr.todos.isComplete;
const toDo = {
id: usr.todos.id,
title: usr.todos.title,
isComplete: isComplete,
timestamp: usr.todos.timestamp
};
await context.db.collection('users').updateOne(
{ _id: ObjectId(context.id) },
{ $set: { "todos.$[elem].isComplete": isComplete } },
{ arrayFilters: [ { "elem.id": args.todoId } ] }
);
if( context.isValid ) {
console.log('toggling todo with id', args.todoId);
const [ usr ] = await context.db.collection('users').aggregate(
[
{ $match: { _id: ObjectId(context.id) } },
{ $unwind: '$todos' },
{ $match: { 'todos.id': args.todoId } }
]
).toArray();
const isComplete = !usr.todos.isComplete;
const toDo = {
id: usr.todos.id,
title: usr.todos.title,
isComplete: isComplete,
timestamp: usr.todos.timestamp
};
await context.db.collection('users').updateOne(
{ _id: ObjectId(context.id) },
{ $set: { "todos.$[elem].isComplete": isComplete } },
{ arrayFilters: [ { "elem.id": args.todoId } ] }
);

return {
code: '200',
success: true,
message: 'successfully toggled ToDo',
todo: toDo
return {
code: '200',
success: true,
message: 'successfully toggled ToDo',
user: {
id: context.id,
email: context.email
}
}
}
else
throw new AuthenticationError('Authentication required!');
},
removeTodo: async ( _, args, context ) => {
await context.db.collection('users').updateOne(
{ _id: ObjectId(context.id) },
{ $pull: { todos: { id: args.todoId } } }
);
if( context.isValid ) {
console.log('removing todo with id', args.todoId);
await context.db.collection('users').updateOne(
{ _id: ObjectId(context.id) },
{ $pull: { todos: { id: args.todoId } } }
);

return {
code: '200',
success: true,
message: 'successfully removed ToDo'
return {
code: '200',
success: true,
message: 'successfully removed ToDo',
user: {
id: context.id,
email: context.email
}
}
}
else
throw new AuthenticationError('Authentication required!')
}
},
MutationResponse: {
Expand Down
2 changes: 1 addition & 1 deletion src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const typeDefs = gql`
code: String
success: Boolean!
message: String
todo: Todo
user: User
}
`;

Expand Down

0 comments on commit 2d53030

Please sign in to comment.