Mutacje GraphQL: modyfikowanie danych
Wyjdą Państwo poza odczytywanie danych: nauczą się definiować i implementować mutacje GraphQL do tworzenia, aktualizowania i usuwania danych w Spring Boot.
Mutacje GraphQL: modyfikowanie danych to bezpłatna lekcja GraphQL APIs with Spring Boot na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej GraphQL APIs with Spring Boot, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs GraphQL APIs with Spring Boot zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Ucz się GraphQL APIs with Spring Boot dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 12
- Lekcje
- 48
Często zadawane pytania
Czy lekcja „Mutacje GraphQL: modyfikowanie danych” jest bezpłatna?
Tak — pełny tekst „Mutacje GraphQL: modyfikowanie danych” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu GraphQL APIs with Spring Boot, przejdź na CoddyKit PRO. Kurs GraphQL APIs with Spring Boot zawiera 4 lekcji w sumie.
Co nauczysz się w „Mutacje GraphQL: modyfikowanie danych”?
Wyjdą Państwo poza odczytywanie danych: nauczą się definiować i implementować mutacje GraphQL do tworzenia, aktualizowania i usuwania danych w Spring Boot. Ćwiczysz GraphQL APIs with Spring Boot z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć GraphQL APIs with Spring Boot?
Nie wymagamy żadnego doświadczenia. GraphQL APIs with Spring Boot w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Mutacje GraphQL: modyfikowanie danych”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji GraphQL APIs with Spring Boot?
Tak. Każda lekcja GraphQL APIs with Spring Boot zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Definiowanie niestandardowych typów danych
- Implementowanie resolverów danych
- Wykonywanie prostych zapytań GraphQL
- Mutacje GraphQL: modyfikowanie danych