0Pricing
GraphQL APIs with Spring Boot · Lektion

GraphQL-Mutationen: Daten ändern

Gehen Sie über das Lesen von Daten hinaus: Lernen Sie, GraphQL-Mutationen zu definieren und in Spring Boot zu implementieren, um Daten zu erstellen, zu aktualisieren und zu löschen.

GraphQL-Mutationen: Daten ändern ist eine kostenlose GraphQL APIs with Spring Boot-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des GraphQL APIs with Spring Boot-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der GraphQL APIs with Spring Boot-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

From Reading to Writing

You can now define types, resolvers, and run queries. But real APIs must also change data.

In GraphQL, every write goes through a mutation — the counterpart to a query.

Query vs Mutation

The shapes look similar, but intent differs:

  • Query: read data, no side effects, may run in parallel.
  • Mutation: change data, executed sequentially top to bottom.

Declaring a Mutation in the Schema

Mutations live under the special Mutation type and usually return the affected object.

type Mutation {
  createBook(title: String!, author: String!): Book
}

Input Types for Complex Arguments

For many fields, group arguments into an input type instead of a long argument list.

input BookInput {
  title: String!
  author: String!
}

type Mutation {
  createBook(book: BookInput!): Book
}

Implementing the Resolver

In Spring Boot, annotate the handler with @MutationMapping.

@Controller
class BookMutation {
    @MutationMapping
    Book createBook(@Argument String title, @Argument String author) {
        return new Book(title, author);
    }
}

Returning the Result

Returning the created or updated object lets the client immediately read back any fields it needs.

mutation {
  createBook(title: "Clean Code", author: "Martin") {
    id
    title
  }
}

Update and Delete Mutations

The same pattern covers all writes.

type Mutation {
  updateBook(id: ID!, book: BookInput!): Book
  deleteBook(id: ID!): Boolean
}

A Self-Contained Logic Example

The resolver logic is plain Java; here is the core idea as a runnable snippet.

import java.util.*;
public class Main {
  static Map<Integer,String> store = new HashMap<>();
  static int seq = 0;
  static int createBook(String title){ int id = ++seq; store.put(id, title); return id; }
  public static void main(String[] a){
    int id = createBook("Clean Code");
    System.out.println("Created id=" + id + " title=" + store.get(id));
  }
}

Validating Input

Mutations are the right place to enforce rules: reject empty titles, check permissions, ensure referenced records exist.

Return clear errors so clients can react meaningfully.

Sequential Execution Matters

Unlike query fields, top-level mutation fields run one after another.

This guarantees that if a single request contains several mutations, earlier ones complete before later ones begin — important for consistency.

Best Practices

  • Use input types for multi-field arguments.
  • Return the affected object so clients can re-read fields.
  • Validate and authorize inside resolvers.
  • Name mutations as verbs: createBook, updateBook, deleteBook.

Quick Check

Test your understanding of GraphQL mutations.

Recap

You learned to change data with GraphQL mutations.

  • Define them under the Mutation type, often with input types.
  • Implement with @MutationMapping in Spring Boot.
  • They run sequentially and should validate and return the affected object.

Häufig gestellte Fragen

Ist die Lektion „GraphQL-Mutationen: Daten ändern“ kostenlos?

Ja — der vollständige Text von „GraphQL-Mutationen: Daten ändern“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des GraphQL APIs with Spring Boot-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der GraphQL APIs with Spring Boot-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „GraphQL-Mutationen: Daten ändern“?

Gehen Sie über das Lesen von Daten hinaus: Lernen Sie, GraphQL-Mutationen zu definieren und in Spring Boot zu implementieren, um Daten zu erstellen, zu aktualisieren und zu löschen. Du übst GraphQL APIs with Spring Boot mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um GraphQL APIs with Spring Boot zu starten?

Keine Vorkenntnisse erforderlich. GraphQL APIs with Spring Boot auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „GraphQL-Mutationen: Daten ändern“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser GraphQL APIs with Spring Boot-Lektion Code schreiben und ausführen?

Ja. Jede GraphQL APIs with Spring Boot-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Benutzerdefinierte Datentypen definieren
  2. Daten-Resolver implementieren
  3. Einfache GraphQL-Abfragen ausführen
  4. GraphQL-Mutationen: Daten ändern
← Zurück zu GraphQL APIs with Spring Boot