Artifact image

Nested mutations

Mutations are only exposed in the root type in GraphQL. As a consequence, the root type becomes heavily bloated, containing fields with nothing in common among themselves other than being mutations (which is a technical matter, not an interface design decision).

Nested mutations make the schema more logical and browsable, by enabling to perform mutations on any type, and not only on the root type. They help performance too, by allowing you to modify data on the result from another mutation, thus avoiding the latency from executing multiple requests.

This GraphQL query demonstrates a nested mutation:

mutation {
  createPost(input: {
    title: "First title"
  }) {
    status
    postID
    post {
      update(input: {
        title: "Second title",
        contentAs: { html: "Some content" }
      }) {
        status
        post {
          title
          content
          addComment(input: {
            commentAs: { html: "My first comment" }
          }) {
            status
            commentID
            comment {
              content
              date
            }
          }
        }
      }
    }
  }
}