Beginning GraphQL with React, NodeJS and Apollo by Lim Greg

Beginning GraphQL with React, NodeJS and Apollo by Lim Greg

Author:Lim, Greg
Language: eng
Format: epub
Published: 2023-03-02T00:00:00+00:00


Figure 1

If you go to your MongoDB Atlas dashboard, under ‘Collections’, you will see the record added (fig. 2).

Figure 2

So we have our GraphQL API connected to our database and we are able to add a todo.

Go ahead and create more todos and see what happens!

Next, let’s implement deleting a todo.

Delete Todo

In /server/schema.js, in mutation, under addTodo, add the below code:

const mutation = new GraphQLObjectType({

name: 'Mutation',

fields:{

addTodo: {

…

},

deleteTodo: {

type: TodoType,

args: {

id: { type: GraphQLNonNull(GraphQLID) },

},

resolve(parent, args) {

return Todo.findByIdAndRemove(args.id);

},

},

}

})

deleteTodo will take in one id argument, and again we specify it to be non-null. For resolve, we use the Mongoose’s findByIdAndRemove method to remove that specified todo.

Running your App

In GraphiQL, run the below delete mutation:

mutation{

deleteTodo(id:"63cf2f87ba6f07707d1f8775"){

id,

title,

completed,

}

}

Include your own id (should be in double quotes “”). And when you run the query to fetch all todos eg.:

{

todos{

id,

title,

completed

}

}

that todo won’t be there.

Next, let’s implement updating a todo.

Updating a Todo

In /server/schema.js, add:

const mutation = new GraphQLObjectType({

name: 'Mutation',

fields:{

addTodo: {

…

},

deleteTodo: {

…

},

updateTodo: {

type: TodoType,

args: {

id: { type: GraphQLNonNull(GraphQLID) },

title: { type: GraphQLString },

},

resolve(parent, args) {

return Todo.findByIdAndUpdate(

args.id,

{

$set: {

title: args.title,

},

}

);

},

},

}

})

updateTodo is similar to addTodo and deleteTodo, except that in resolve, we use findByIdAndUpdate and provide the id of the todo we want to update.

We then set title with args.title.

Running our App

In GraphiQL, run a mutation to change the title:

mutation{

updateTodo(id:"63cf2fdcff71b20840649061",title: "edit graphql article"){

id,

title,

completed

}

}

And when we run a query to retrieve all todos, you will see the todo’s title updated.

Marking Todos Complete

We will next implement marking a todo as complete and vice-versa. In schema.js, add:

const mutation = new GraphQLObjectType({

name: 'Mutation',

fields:{

addTodo: {

…

},

deleteTodo: {

…

},

updateTodo: {

…

},

toggleTodo: {

type: TodoType,

args: {

id: { type: GraphQLNonNull(GraphQLID) },

},

async resolve(parent, args) {

const todo = await Todo.findById(args.id)

return Todo.findByIdAndUpdate(

args.id,

{

$set: {

completed: !todo.completed

},

}

);

},

},

}

})

Again, toggleTodo is similar to updateTodo, except that in resolve, we first retrieve the existing todo’s completed status, invert it (from false to true and vice-versa), and again use findByIdAndUpdate to set the new completed status.

Overall Reflection

Now, let’s pause for a while and go over what we have done so far.

In schema.js, we have a Todo type.

const TodoType = new GraphQLObjectType({

name: 'Todo',

fields: {

id: { type: GraphQLID },

title: { type: GraphQLString },

completed: { type: GraphQLBoolean },

}

});

For queries, we have a todos query to get all todos, and a todo query that accepts an argument to get a single todo.

const RootQueryType = new GraphQLObjectType({

name: 'Query',

fields: {

todos: {

type: new GraphQLList(TodoType),

resolve: (parent, args) => {

return Todo.find()

}

},

todo: {

type: TodoType,

args:{id: {type: GraphQLID}},

resolve: (parent, args) => { ​

return Todo.findById(args.id)

}

}

}

});

For mutations, we can add a new todo, delete a todo, update a todo, and toggle a todo as completed. So we have full CRUD functionality for todos.

const mutation = new GraphQLObjectType({

name: 'Mutation',

fields:{

addTodo: {

…

},

deleteTodo: {

…

},

updateTodo: {

​ …

},

toggleTodo: {

…

},

}

})

Later in the book, we will see how objects can be connected together. Eg. we will define a schema about an employee and their associated todos.

For now, let’s see how we can implement full CRUD functionality via a React frontend.

*You can obtain the source code of the completed project by contacting [email protected].



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.