نمذجة الكائنات والعلاقات المتداخلة
صمّم مخططات تمثّل البيانات الهرمية، وحدّد العلاقات بين الأنواع المختلفة.
نمذجة الكائنات والعلاقات المتداخلة درس مجاني في GraphQL APIs with Spring Boot على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في GraphQL APIs with Spring Boot، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة GraphQL APIs with Spring Boot 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Nested Data Matters
Real-world data is rarely flat. It has connections and hierarchies, like an author having multiple books, or a product having many reviews. GraphQL excels at modeling these complex relationships naturally.
This lesson explores how to design your schema to represent data with nested objects and various relationships, making your API more intuitive and powerful.
Understanding Nested Types
A nested object means one type contains another type as a field. For example, an Author might have a list of Book objects they've written.
This allows clients to fetch related data (like all books by an author) in a single GraphQL request, reducing over-fetching and under-fetching.
Defining Nested Types in SDL
In GraphQL's Schema Definition Language (SDL), you define nested types directly. Here, an Author type includes a list of Book objects.
Notice the [Book!]! syntax, meaning a non-null list of non-null books.
type Book {
id: ID!
title: String!
publicationYear: Int
}
type Author {
id: ID!
name: String!
books: [Book!]! # List of books by this author
}
type Query {
author(id: ID!): Author
}Java Models for Nested Data
In Spring Boot, your Java data models (POJOs or records) should mirror your GraphQL schema. This makes mapping data between your backend and the GraphQL API straightforward.
You'll typically have separate classes for each GraphQL type, with fields matching the schema.
// src/main/java/com/example/model/Author.java
class Author {
private String id;
private String name;
private List<Book> books;
// Constructor, getters, setters...
}
// src/main/java/com/example/model/Book.java
class Book {
private String id;
private String title;
private Integer publicationYear;
// Constructor, getters, setters...
}How Nested Fields are Resolved
When a client requests an Author and its books, Spring Boot GraphQL will first resolve the Author object. Then, it will look for a way to resolve the books field on that Author.
Often, if your Java object has a matching getter (e.g., getBooks()), Spring Boot GraphQL can automatically fetch the nested list.
Querying Nested Data
Clients can fetch all nested data with a single, clear query. This is a major advantage of GraphQL compared to traditional REST APIs, where you might need multiple requests or complex join logic on the client.
Notice how books and its fields are queried directly within the author field.
query GetAuthorWithBooks {
author(id: "author-1") {
id
name
books {
id
title
publicationYear
}
}
}Simulating Nested Data Resolution
This simplified Java code demonstrates how a data fetcher might prepare an Author object that already contains its associated Book objects. This structure is then ready for GraphQL to expose.
Run it to see how the nested data is represented.
import java.util.Arrays;
import java.util.List;
// Simplified Book class
class Book {
String id;
String title;
Integer publicationYear;
public Book(String id, String title, Integer publicationYear) {
this.id = id;
this.title = title;
this.publicationYear = publicationYear;
}
// Getters for GraphQL to access fields
public String getId() { return id; }
public String getTitle() { return title; }
public Integer getPublicationYear() { return publicationYear; }
}
// Simplified Author class
class Author {
String id;
String name;
List<Book> books;
public Author(String id, String name, List<Book> books) {
this.id = id;
this.name = name;
this.books = books;
}
// Getters for GraphQL to access fields
public String getId() { return id; }
public String getName() { return name; }
public List<Book> getBooks() { return books; }
}
public class Main {
public static void main(String[] args) {
// Create some books
Book book1 = new Book("b1", "The Martian", 2011);
Book book2 = new Book("b2", "Project Hail Mary", 2021);
List<Book> authorsBooks = Arrays.asList(book1, book2);
// Create an author with their books
Author author = new Author("a1", "Andy Weir", authorsBooks);
// Demonstrate accessing the nested data
System.out.println("Author: " + author.getName());
System.out.println("Books written:");
for (Book book : author.getBooks()) {
System.out.println(" - " + book.getTitle() + " (" + book.getPublicationYear() + ")");
}
}
}Understanding Relationships: One-to-Many
The Author to Book example we've used is a classic "one-to-many" relationship: one author can write many books.
GraphQL schema naturally supports this by defining a field as a list of another type (e.g., books: [Book!]!), making it easy to fetch all related items.
Referencing Related Objects
Sometimes, data isn't directly embedded or available in a single object. For instance, a Book might only store an authorId internally, not the full Author object.
You can still model this in GraphQL by making Author a field on Book. Spring Boot GraphQL will then resolve the Author field by fetching the complete Author object based on the authorId.
type Book {
id: ID!
title: String!
author: Author! # Reference to the Author type
}
type Author {
id: ID!
name: String!
}Check Your Understanding
Consider a GraphQL schema for an e-commerce application. A Product can have many Reviews, and each Review is written by a specific User.
Recap: Modeling Relationships
In this lesson, you learned how to model nested objects and relationships in your GraphQL schema. We covered:
- Defining fields that are lists of other types (e.g.,
books: [Book!]!) to represent one-to-many relationships. - Mirroring these hierarchical structures in your Spring Boot data models.
- Understanding how GraphQL automatically resolves these nested fields, often via Java getters.
- Querying complex, hierarchical data efficiently with a single GraphQL request.
Mastering nested objects and relationships is key to building powerful and intuitive GraphQL APIs that truly reflect your application's data structure.
الأسئلة الشائعة
هل درس «نمذجة الكائنات والعلاقات المتداخلة» مجاني؟
نعم — نص درس «نمذجة الكائنات والعلاقات المتداخلة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة GraphQL APIs with Spring Boot، انتقل إلى CoddyKit PRO. تتضمن دورة GraphQL APIs with Spring Boot 4 دروس في المجموع.
ماذا ستتعلم في «نمذجة الكائنات والعلاقات المتداخلة»؟
صمّم مخططات تمثّل البيانات الهرمية، وحدّد العلاقات بين الأنواع المختلفة. تتمرن على GraphQL APIs with Spring Boot مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ GraphQL APIs with Spring Boot؟
لا تُشترط خبرة سابقة. GraphQL APIs with Spring Boot على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «نمذجة الكائنات والعلاقات المتداخلة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس GraphQL APIs with Spring Boot هذا؟
نعم. كل درس في GraphQL APIs with Spring Boot يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- نمذجة الكائنات والعلاقات المتداخلة
- تطبيق أنواع Interfaces وUnion
- الاستفادة من Input Types في Mutations
- التعدادات والأنواع العددية المخصّصة