Javascript GraphQL解析器逻辑-从变异调用查询/变异?

Javascript GraphQL解析器逻辑-从变异调用查询/变异?,javascript,graphql,graphql-js,Javascript,Graphql,Graphql Js,我正在创建一个锻炼/锻炼日志,用户可以在其中向其帐户添加一组锻炼日志。用户还可以查看他们的训练和练习历史(使用设置数据)。我使用mongoDB来存储这些数据,使用GraphQL和mongoose来查询和修改数据。我将锻炼和锻炼分为各自的类型,因为锻炼对象将仅保存最近4小时(锻炼持续时间)记录的锻炼和设置,而锻炼对象将保存用户曾经记录的所有设置 类型定义 type Workout { id: ID! workoutName: String! username: String! cr

我正在创建一个锻炼/锻炼日志,用户可以在其中向其帐户添加一组锻炼日志。用户还可以查看他们的训练和练习历史(使用设置数据)。我使用mongoDB来存储这些数据,使用GraphQL和mongoose来查询和修改数据。我将锻炼和锻炼分为各自的类型,因为锻炼对象将仅保存最近4小时(锻炼持续时间)记录的锻炼和设置,而锻炼对象将保存用户曾经记录的所有设置

类型定义

type Workout {
  id: ID!
  workoutName: String!
  username: String!
  createdAt: String
  exercises: [Exercise]!
  notes: String
}
    
type Exercise {
  id: ID!
  exerciseName: String!
  username: String!
  sets: [Set]!
}

type Set {
  id: ID!
  reps: Int!
  weight: Float!
  createdAt: String!
  notes: String
}
我的问题在于用于添加集合(变异)的解析器代码。此解析器应:

  • 查询数据库用户是否做过练习,通过检查数据库中匹配的练习名称,如果有匹配,则添加用户输入的设置数据,否则,首先创建新的练习条目,然后将设置添加到数据库中
  • 查询数据库是否有在过去4小时内完成的训练。如果没有匹配项,请在数据库中创建新的训练条目。如果存在训练,请在训练对象上检查匹配的训练名称,以将设置数据添加到该训练对象或为其创建新的训练条目
  • 我意识到这种变异将是相当大的,并将数据的查询和变异结合在一起。所以我想知道我是否可以从我的addSet解析器中调用类似于函数调用的单独查询/突变?还是我应该用另一种方法来解决这个问题

    addSet解析器

    async addSet(_, { exerciseName, reps, weight, notes }, context) {
                const user = checkAuth(context); // Authenticates and gets logged in user's details
    
                if (exerciseName.trim() === '') {
                    throw new UserInputError('Empty Field', {
                        // Attached payload of errors - can be used on client side
                        errors: {
                            body: 'Choose an exercise'
                        }
                    })
                } else {
                    exerciseName = exerciseName.toLowerCase();
                    console.log(exerciseName);
                }
    
                if ((isNaN(reps) || reps === null) || (isNaN(weight) || reps === null)) {
                    throw new UserInputError('Empty Fields', {
                        // Attached payload of errors - can be used on client side
                        errors: {
                            reps: 'Enter the number of reps you did for this set',
                            weight: 'Enter the amount of weight you did for this set'
                        }
                    })
                }
    
                // TODO: Check to see if the exercise has been done before by the user. If it has, then update the entry by adding the set data to it. If not create a new entry for the 
                // exercise and then add the data to it - Completed and working.
                const exerciseExists = await Exercise.findOne({ exerciseName: exerciseName, username: user.username });
    
                if (exerciseExists) {
                    console.log("This exercise exists");
                    exerciseExists.sets.unshift({
                        reps,
                        weight,
                        username: user.username,
                        createdAt: Date.now(),
                        notes
                    })
                    await exerciseExists.save();
                    //return exerciseExists;
                } else {
                    console.log("I don't exist");
                    const newExercise = new Exercise({
                        exerciseName,
                        user: user.id,
                        username: user.username,
                    });
                    const exercise = await newExercise.save();
                    console.log("new exercise entry");
                    exercise.sets.unshift({
                        reps,
                        weight,
                        username: user.username,
                        createdAt: Date.now(),
                        notes
                    })
                    await exercise.save();
                    //return exercise;
                }
    
                // TODO: Get the most recent workout from the user and check if the time it was done was from the last 4 hours. If it wasn't, create a new workout entry for the user. 
                // If it was within the ast 4 hours, check to see if the workout has an exercise that matches with one the user inputted. If there isn't an exercise, create a new entry 
                // and add the set data to it, otherwise update the existing entry for the exercise.
                const workoutExists = await Workout.findOne({ username: username }).sort({ createdAt: -1 }); // Gets most recent workout by user
                const now = Date.now();
                if (now > workoutExists.createdAt + 14400000) { // Checks to see if the workout was less than 4 hours ago
                    console.log("workout was from another session");
                    // rest of code not implemented yet
                } else {
                    console.log("workout is still in progress");
                    // rest of code not implemented yet
                }
    
            },