طفرات GraphQL: تغيير البيانات
تجاوز قراءة البيانات وتعلّم تعريف طفرات GraphQL وتنفيذها لإنشاء البيانات وتحديثها وحذفها في Spring Boot.
طفرات GraphQL: تغيير البيانات درس مجاني في GraphQL APIs with Spring Boot على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في GraphQL APIs with Spring Boot، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة GraphQL APIs with Spring Boot 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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.
تعلم GraphQL APIs with Spring Boot مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 48
الأسئلة الشائعة
هل درس «طفرات GraphQL: تغيير البيانات» مجاني؟
نعم — نص درس «طفرات GraphQL: تغيير البيانات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة GraphQL APIs with Spring Boot، انتقل إلى CoddyKit PRO. تتضمن دورة GraphQL APIs with Spring Boot 4 دروس في المجموع.
ماذا ستتعلم في «طفرات GraphQL: تغيير البيانات»؟
تجاوز قراءة البيانات وتعلّم تعريف طفرات GraphQL وتنفيذها لإنشاء البيانات وتحديثها وحذفها في Spring Boot. تتمرن على GraphQL APIs with Spring Boot مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ GraphQL APIs with Spring Boot؟
لا تُشترط خبرة سابقة. GraphQL APIs with Spring Boot على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «طفرات GraphQL: تغيير البيانات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس GraphQL APIs with Spring Boot هذا؟
نعم. كل درس في GraphQL APIs with Spring Boot يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تحديد أنواع البيانات المخصصة
- تطبيق محلّلات البيانات
- تنفيذ استعلامات GraphQL البسيطة
- طفرات GraphQL: تغيير البيانات