Mutations

In this section, you’ll learn how to implement a mutation to create new Link elements through your application schema.

Mutation for posting new links

A nice thing about GraphQL is that despite the semantic difference between queries and mutations, on a technical level these two concepts are actually almost identical. When adding a mutation to your API, you need to add a corresponding field to the Mutation type of your GraphQL schema and then implement the resolver for that field. Just like with queries!

This mutation allows to create (post) a new Link item. The next step is to implement the resolver for the new post field.

This implementation follows the same approach as the feed query from the previous chapter. You’re retrieving the url and description input arguments and passing them on to the createLink mutation from the Prisma API. Easy as pie! 🍰

The last step is to also add the Mutation resolvers to the constructor of GraphQLServer in index.js.

This is it. To test this mutation, you can run yarn start again and send the following mutation in the default Playground in the app section:

mutation {
  post(url: "https://www.howtographql.com", description: "Fullstack tutorial website for GraphQL") {
    id
  }
}
Unlock the next chapter
Which of these is false about GraphQL field arguments?
They are how clients pass data to the server
They must be included in the field schema definition
They can be accessed inside resolvers
Only mutation fields can have them