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