Write a GraphQL Mutation

Share this video with your friends

Send Tweet

In order to change the data that we can query for in a GraphQL Schema, we have to define what is called a mutation in GraphQL. Mutations allow us to specify ways to create, update, and delete data. It also provides a way to fetch information after the mutation occurs. In this video, we’ll learn how to create a Mutation Type in GraphQL and specify the information we want returned.

Pieter Malan
Pieter Malan
~ 8 years ago

Great course! I was hoping this course would shed some light on how to handle business rule validations for mutations. Do you have any suggestions for handling custom validation? For example in your case you might not want duplicate items added with the same name. You could throw an exception which would be returned as an error, but this only handles a single validation rule at a time. Anyway do you have any ideas for validations?

Josh Black
Josh Black(instructor)
~ 8 years ago

Hi Ryan! First off, thanks for the watching the course! Hope it's been helpful 😄

I think you bring up a great question, and the answer definitely can stem from where GraphQL sits in your stack. If it's communicating through an API level, passing through the errors that it receives seems like it should be enough.

In your case, I think, when it's talking directly to a Database Layer I think you encounter this situation frequently. In projects that I've worked on, the ORM layer that we talk to directly communicates a set of errors (such as unique constraint violations) and we have a utility to format the set of errors in a friendly manner for client consumers.

In some projects, having resolvers defined outside of the schema might help for this. If you have a VideoResolver, for example, with #create, #update, or #delete methods, then you could point your resolve statements in Mutations towards these resolver methods that communicate to the ORM later and accumulate the set of errors from a given side-effect.

Sorry if this isn't that helpful, unfortunately this definitely can depend on your language runtime for GraphQL as well as the ORMs or Database modules that you're working with.

If you have any more questions, feel free to ask!

Aimn
Aimn
~ 8 years ago

Hi, Thanks for this excellent course. Could you please elaborate on this more. So suppose you have a UserType with firstName (String), lastName (String), age (Number) and suppose you have a mutation to update the user but all data passed to the ORM have errors and the ORM gives you back an object that looks like this: { errors: {firstName: "first name error", lastName: "last name error", age: "age error"} } and I want to pass this to graphql client so that it can show the errors in a form. Can you explain how to pass all the errors back to the client or to graphiql. Thanks

astercraker
astercraker
~ 6 years ago

Great course, I hope the lesson of integration with mongo or mongoatlas

Jeremy
Jeremy
~ 6 years ago

Just wanted to call to your attention that there's a lot of bugs in the transcript for this video:

        title: {
          type: new GraphQLNonNull(GraphQLString),
          description: 'The title of the video.',
        },
        duration: {
          type: new GraphQLNonNull(GraphQLString),
          description: 'The duration of the video (in seconds).',
        },
        released: {
          type: new GraphQLNonNull(GraphQLString),
          description: 'Whether or not the video is released.',
        },
      },

The types here are all GraphQLString instead of String/Int/Boolean respectively.

Down here

Mutation M {
  createVideo(title: "Foo", duration: 300, Boolean: false) {
    id,
    title
  }
}

The field is incorrectly labeled as Boolean instead of released

Md. Anam Hossain
Md. Anam Hossain
~ 5 years ago

When creating a video from mutation I got this error in my terminal. What to do ? DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

Thanks

Eleonora Lester
Eleonora Lester
~ 5 years ago

When creating a video from mutation I got this error in my terminal. What to do ? DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

new Buffer.from(title).toString("base64");
Leonel Galán
Leonel Galán
~ 5 years ago

@Eleonora, it should work because it's a deprecation warning, to remove the warning simply use, this is more clear when you visit the documentation for new Buffer(string[, encoding])

Buffer.from(title).toString("base64")