The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Write a GraphQL Mutation

Write a GraphQL Mutation

5:14
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.
Watch this lesson now
Avatar
egghead.io

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.

Avatar
Ryan

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?

Avatar
Josh Black

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!

In reply to Ryan
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?