2015-09-23 75 views
0
打印自定義消息

嗨,我有GraphQl正與ES6的組合。 從graphql中刪除特定的記錄時,我得到已刪除記錄的詳細信息(值),我想打印一些自定義消息,如「記錄已刪除」。請相應地幫助我。如何GraphQL

這裏是我的graphQL代碼:

removeUser:{ 
     type: UserType, 
     args: { 
      _id: { 
       description: 'The _id of the user', 
       type: GraphQLString, 
      }, 
     }, 
     resolve: (obj, {_id}) =>{   
      return new Promise((resolve, reject) => { 
       User.findOne({_id:_id},(err,res)=> { 
        if(err || res == null) { 
         reject('User was not found') 
        } 
        else { 
         User.remove({_id: _id},(err,result)=>{ 
          err ? reject(err) : reject('User removed successfully') 
         }); 
        } 
       }) 
      })   
     } 
    } 

回答

1

您聲明UserType作爲removeUser字段的類型。顯然,字符串'User removed successfully'不是UserType;它是一個String類型。

而且,如果刪除操作是成功的,你應該叫在無極resolve,不reject

我覺得這樣的事情應該工作:

removeUser:{ 
    type: GraphQLString, 
    args: { 
     _id: { 
      description: 'The _id of the user', 
      type: GraphQLString, 
     }, 
    }, 
    resolve: (obj, {_id}) =>{   
     return new Promise((resolve, reject) => { 
      User.findOne({_id:_id},(err,res)=> { 
       if(err || res == null) { 
        reject('User was not found') 
       } 
       else { 
        User.remove({_id: _id},(err,result)=>{ 
         err ? reject(err) : resolve('User removed successfully') 
        }); 
       } 
      }) 
     })   
    } 
}