GraphQL Introduction – part 3

Hi, continuing with the third post and this is the final post about the GraphQL introduction. 

So, there is a way to alter the data because for now we only know to get data with queries, but to change, update, mutate the data we need to implement “mutations” in GraphQL this is really easy to do. 

1.- Go inside the Schema and insert type mutation like this

const schema = buildSchema(`
  type Mutation {
    updateCourseTopic(id: Int!, topic: String!): Course

2.- Then we need to create the function that will update the data

// function that update a course
let updateCourseTopic = ({ id, topic}) => {
  // go through the array => {
    // if the id that Im passing match
    // with some course
    if ( === id) {

      // then replace the topic with the new topic
      course.topic = topic;

      return course;

  // Returning the new data
  return courses.filter((course) => === id)[0];

3.- Then call the updateCourseTopic in root object like this

const root = {
  message: () => "Hello world!",
  course: getCourse,
  courses: getCourses,
  updateCourseTopic: updateCourseTopic

4.- Re-start the server with

node index.js

5.- Let’s go to the browser and update the course id: 1 and change the topic to Node Js


All the code in this repo:

You can read the part 1 and 2 here:

By Cristina Rojas.