From 2d530300329905881d849b80b6caf3b069438f7a Mon Sep 17 00:00:00 2001 From: Prem Kumar Date: Thu, 20 Jun 2019 13:25:53 +0530 Subject: [PATCH] modified resolvers and bug fixes --- src/context.js | 1 + src/resolvers.js | 124 ++++++++++++++++++++++++++++------------------- src/schema.js | 2 +- 3 files changed, 77 insertions(+), 50 deletions(-) diff --git a/src/context.js b/src/context.js index bff795a..254dfeb 100644 --- a/src/context.js +++ b/src/context.js @@ -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 { diff --git a/src/resolvers.js b/src/resolvers.js index aed9594..24719a5 100644 --- a/src/resolvers.js +++ b/src/resolvers.js @@ -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){ @@ -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: { diff --git a/src/schema.js b/src/schema.js index 5a9ed52..0aacba0 100644 --- a/src/schema.js +++ b/src/schema.js @@ -42,7 +42,7 @@ const typeDefs = gql` code: String success: Boolean! message: String - todo: Todo + user: User } `;