I'm learning GraphQL and for practice purpose I have developed a backend that would create a planner with the following inputs, tasks which is an array of type Task, dayDescription and waterGlassCount which are Strings. I'm getting issues in TypeDefinition.js where I have created an input with Tasks having the type [Task]. Please have a look at my TypeDefinition.js
const {gql} = require('apollo-server-express')
const typeDefs = gql`
type Planner{
id: ID
tasks: [Task]
dayDescription: String
waterGlassCount: String
}
type Task{
id: ID
taskDescription: String
shouldStrike: Boolean
}
type Query {
getAllPlanners: [Planner]
getPlanner(id:ID): Planner
getAllTasksOfPlanner(id:ID): [Task]
getEachTaskOfTasksOfPlanner(id: ID): Task
}
input TaskInput{
taskDescription: String
shouldStrike: Boolean
}
input PlannerInput{
tasks: [Task]
dayDescription: String
waterGlassCount: String
}
type Mutation {
createPlanner(planner: PlannerInput) : Planner
createTask(task: TaskInput): Task
deletePlanner(id:ID): String
deleteAllPlanners : String
toggleStrike(id:ID): Task
updatePlanner(id:ID, planner: PlannerInput): Planner
deleteAllTasks(id:ID): String
}
`;
module.exports=typeDefs;
This is the code of Resolvers.js.
const Planner = require('./Models/PlannerModel');
const Task = require('./Models/TasksModel')
const resolvers = {
Query: {
getAllPlanners: async ()=>{
return await Planner.find()
},
getPlanner: async(parent, {id}, context, info)=> {
return await Planner.findById(id)
},
getAllTasksOfPlanner: async (parent, {id}, context, info) => {
const planner = await Planner.findById(id);
return planner.tasks
},
getEachTaskOfTasksOfPlanner: async (parent, {id}, context, info) => {
return await Task.findById(id)
},
},
Mutation:{
createPlanner: async(parent, args, context, info)=>{
const { dayDescription, waterGlassCount} = args.planner
const planner = new Planner({ dayDescription, waterGlassCount})
await planner.save()
return planner
},
createTask : async(parent, {taskDescription, shouldStrike}, context, info) => {
const task = new Task({taskDescription, shouldStrike})
await task.save();
return task
},
deletePlanner: async (parent, {id}, context, info) => {
await Planner.findByIdAndDelete(id);
return 'Planner Deleted'
},
deleteAllPlanners: async() => {
await Planner.deleteMany({})
},
toggleStrike : async (parent, {id}, context, info) =>{
const task = await Task.findById(id);
task.shouldStrike = !task.shouldStrike
return task
},
updatePlanner: async(parent, args, context, info)=>{
const {id}= args;
const {tasks, dayDescription, waterGlassCount} = args.planner;
const updates ={};
if(tasks !== undefined){
updates.tasks = tasks
}
if(dayDescription!== undefined){
updates.dayDescription = dayDescription
}
if(waterGlassCount !== undefined){
updates.waterGlassCount = waterGlassCount
}
const planner = Planner.findByIdAndUpdate(id, updates, {new:true});
return planner
},
}
};
module.exports = resolvers;
The following is the code for PlannerModel and TaskModel
const mongoose = require("mongoose");
const PlannerSchema = new mongoose.Schema({
tasks: {
type: Array,
},
dayDescription: {
type: String,
},
waterGlassCount: {
type: String,
},
});
const Planner = mongoose.model("planner", PlannerSchema);
module.exports = Planner;
const mongoose = require("mongoose");
const TaskSchema = new mongoose.Schema({
taskDescription: {
type: String,
},
shouldStrike:{
type: Boolean
}
});
const Task = mongoose.model("task", TaskSchema);
module.exports = Task;
The error I get in the terminal is "Error: The type of PlannerInput.tasks must be Input Type but got: [Task]". How do I resolve this issue?
CodePudding user response:
When you're using an input
type, all fields inside this type must also be input
types (or scalars). In your case Task
is not an input
type. Maybe it should be TaskInput
?