0
votes

I'm learning GraphQL Mutations. I keep my data on mLab. I use GraphiQL for queries output. I don't know how to write Mutation for „update” data. Right now I have proper „create” and „delete” Mutations:

schema.js

addMovie: {
type: MovieType,
args: {
    title: {type: new GraphQLNonNull(GraphQLString)},
    genre: {type: new GraphQLNonNull(GraphQLString)}
},
resolve(parent, args) {
    let new_movie = new Movie({
        title: args.title,
        genre: args.genre
    });
    return new_movie.save();
}

}

GraphiQL

mutation {
addMovie(title: "Goodfellas", genre: "Drama"){
    title
    genre
}

}

schema.js

deleteMovie: {
type: MovieType,
args: {
    id: {type: new GraphQLNonNull(GraphQLString)}
},
resolve(parent, args) {
    return Movie.findOneAndDelete(args.id);
}

}

GraphiQL

mutation {
deleteMovie(id: "5c0djut25d81cbq17356628y"){
    id
}

}

I'm trying and trying to write „update” Mutation, the best what I did so far is this:

schema.js

editMovie: {
type: MovieType,
args: {
    id: {type: new GraphQLNonNull(GraphQLString)},
    title: {type: GraphQLString},
    genre: {type: GraphQLString}
},
resolve(parent, args) {
    return Movie.findOneAndUpdate(args.id);
}

}

GraphiQL

mutation {
editMovie(id: "5c0djut25d81cbq17356628y", title: "Goodfellas", genre: "Crime"){
    id
}

}

The Mutation above doesn't generate any errors, but it is still returns

genre: "Drama"

instead of

genre: "Crime"
1

1 Answers

0
votes

OK, I've got this:

mutation:

updateMovie: {
    type: MovieType,
    args: {
        id: {type: new GraphQLNonNull(GraphQLID)},
        title: {type: new GraphQLNonNull(GraphQLString)},
        genre: {type: new GraphQLNonNull(GraphQLString)}
    },
    resolve(parent, args) {
        return Movie.findByIdAndUpdate(args.id, {$set: {title: args.title, genre: args.genre}}).exec();
    }
}

query:

mutation {
    updateMovie(id: "3b0fhybd5d12fec157923290", title: "Kill Bill", genre: "crime")
    {
        id
        title
        genre
    }
}