التعدادات والأنواع العددية المخصّصة
وسّع مخطط GraphQL بما يتجاوز الأنواع المضمّنة عبر تعريف التعدادات لمجموعات القيم الثابتة والأنواع العددية المخصّصة لأنواع المجال، مثل التواريخ وعناوين URL، في Spring Boot.
التعدادات والأنواع العددية المخصّصة درس مجاني في GraphQL APIs with Spring Boot على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في GraphQL APIs with Spring Boot، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة GraphQL APIs with Spring Boot 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Beyond Built-in Types
GraphQL ships with scalars like Int, String, and Boolean. But real domains need more: a fixed set of statuses, a proper date type, or a validated email.
Enums and custom scalars let your schema express these precisely.
What Is a GraphQL Enum?
An enum restricts a field to a fixed list of named values. Clients can only send or receive one of those values, and tooling can offer autocompletion.
enum OrderStatus {
PENDING
SHIPPED
DELIVERED
CANCELLED
}Using an Enum in a Type
Reference the enum like any other type. The field is now guaranteed to hold a valid status.
type Order {
id: ID!
status: OrderStatus!
}Mapping Enums to Java
Spring for GraphQL maps a schema enum to a Java enum automatically when the names match exactly.
public enum OrderStatus {
PENDING, SHIPPED, DELIVERED, CANCELLED
}Why Custom Scalars?
The built-in scalars are limited. Representing a date as a String loses meaning and validation. A custom scalar defines how a domain value is serialized, deserialized, and validated.
Declaring a Scalar in the Schema
First declare the scalar name in your SDL, then use it on fields.
scalar DateTime
type Event {
id: ID!
startsAt: DateTime!
}Using a Pre-built Scalar Library
The graphql-java-extended-scalars library provides ready-made scalars for DateTime, Date, URL, and more, so you do not write them by hand.
// build.gradle
implementation 'com.graphql-java:graphql-java-extended-scalars:21.0'Registering a Scalar in Spring
Wire the scalar into the runtime with a RuntimeWiringConfigurer bean so the schema knows how to handle it.
@Bean
public RuntimeWiringConfigurer scalars() {
return wiring -> wiring.scalar(ExtendedScalars.DateTime);
}Writing Your Own Scalar
For a domain type you can define a GraphQLScalarType with a custom Coercing implementation controlling parse and serialize logic.
GraphQLScalarType.newScalar()
.name("Email")
.coercing(new EmailCoercing())
.build();Validation Inside Coercing
A scalar's Coercing can reject invalid input by throwing a CoercingParseValueException, giving you type-level validation for free.
if (!value.toString().contains("@")) {
throw new CoercingParseValueException("Invalid email");
}Best Practices
Use these types wisely:
- Prefer enums over free-form strings for fixed sets
- Reuse library scalars before writing your own
- Keep
Coercinglogic pure and predictable - Document each custom scalar's expected format
Quick Check
Test your knowledge of enums and scalars.
Recap
You extended your schema's type system:
- Enums restrict fields to a fixed value set
- They map automatically to Java enums by name
- Custom scalars model domain types like dates and emails
- Use library scalars or write a
Coercingfor your own - Register scalars via
RuntimeWiringConfigurer
Richer types make your API safer and more expressive.
الأسئلة الشائعة
هل درس «التعدادات والأنواع العددية المخصّصة» مجاني؟
نعم — نص درس «التعدادات والأنواع العددية المخصّصة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة GraphQL APIs with Spring Boot، انتقل إلى CoddyKit PRO. تتضمن دورة GraphQL APIs with Spring Boot 4 دروس في المجموع.
ماذا ستتعلم في «التعدادات والأنواع العددية المخصّصة»؟
وسّع مخطط GraphQL بما يتجاوز الأنواع المضمّنة عبر تعريف التعدادات لمجموعات القيم الثابتة والأنواع العددية المخصّصة لأنواع المجال، مثل التواريخ وعناوين URL، في Spring Boot. تتمرن على GraphQL APIs with Spring Boot مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ GraphQL APIs with Spring Boot؟
لا تُشترط خبرة سابقة. GraphQL APIs with Spring Boot على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «التعدادات والأنواع العددية المخصّصة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس GraphQL APIs with Spring Boot هذا؟
نعم. كل درس في GraphQL APIs with Spring Boot يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- نمذجة الكائنات والعلاقات المتداخلة
- تطبيق أنواع Interfaces وUnion
- الاستفادة من Input Types في Mutations
- التعدادات والأنواع العددية المخصّصة