import StoriesItemType from '../types/StoriesItemType'
import RankingInputItemType from '../types/RankingInputItemType'
import {Story, User, Ranking} from '../models'

const addRanking = {
  name: 'addRanking',
  type: StoriesItemType,
  args: {
    story: {type: RankingInputItemType}
  },
  async resolve (value, {story}) {
    let userId = value.request.user.id
    let storyId = story.id
    let ranking = await Ranking.findOne({where: {userId, storyId}})
    if (! ranking){
      Ranking.create({userId, storyId})
    }
    let result = await Story.findById(storyId, {
      include: [{
        model: User,
        as: 'user'
      },{
        model: Ranking,
        as: 'rankings'
      }]
    })
    return result
  }
}

export default addRanking

@@ -381,24 +381,84 @@
  = story.id%0A
+    let user = await User.findById(userId)%0A    if (user)%7B%0A  
     let rank
@@ -513,16 +513,18 @@
 ryId%7D%7D)%0A
+  
     if (
@@ -541,16 +541,18 @@
 %7B%0A      
+  
 Ranking.
@@ -577,16 +577,18 @@
 oryId%7D)%0A
+  
     %7D%0A  
@@ -585,24 +585,26 @@
       %7D%0A    
+  
 let result =
@@ -642,16 +642,18 @@
 %7B%0A      
+  
 include:
@@ -660,24 +660,26 @@
  %5B%7B%0A        
+ 
+ 
 model: User,
@@ -683,24 +683,26 @@
 er,%0A        
+  
 as: 'user'%0A 
@@ -710,12 +710,16 @@
     
+ 
+ 
 %7D,%7B%0A
+  
     
@@ -742,24 +742,26 @@
 ng,%0A        
+ 
+ 
 as: 'ranking
@@ -773,18 +773,24 @@
     
+  
 %7D%5D%0A    
+ 
+ 
 %7D)%0A
+  
     
@@ -803,16 +803,61 @@
  result%0A
+    %7Delse%7B%0A      throw 'Invalid user!'%0A    %7D%0A
   %7D%0A%7D%0A%0Ae

Add user check to add ranking