GraphQL APIs with Spring Boot · บทเรียน

การออกแบบสคีมา GraphQL แรกของคุณ

เรียนรู้การกำหนดชนิดข้อมูล ฟิลด์พื้นฐาน และทำความเข้าใจภาษานิยามสคีมา (SDL)

บทเรียน 3 จาก 411 ขั้นตอน

การออกแบบสคีมา GraphQL แรกของคุณ เป็นบทเรียน GraphQL APIs with Spring Boot ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน GraphQL APIs with Spring Boot และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What's a GraphQL Schema?

A GraphQL schema is the blueprint of your API: it defines what data clients can ask for and how it’s structured. It’s the core of every GraphQL service.

Meet SDL: Schema Language

You write schemas in the Schema Definition Language (SDL) — a simple, readable syntax for declaring object types, their fields, and how they relate.

Building Blocks: Object Types

The core building block is the object type, declared with the type keyword. Like a class, it represents a kind of object you can fetch.

type User {
  # Fields will go here
}

Adding Fields with Scalar Types

Fields hold data via built-in scalar types: String, Int, Float, Boolean, and ID for unique identifiers.

type User {
  id: ID
  name: String
  age: Int
  isActive: Boolean
  rating: Float
}

Essential Data: Non-Null Fields

Add ! after a type to make a field non-null — it must always have a value. Query it and the server must return one or it errors.

type Product {
  id: ID!
  name: String!
  price: Float!
  description: String
}

Collections: Lists in GraphQL

Wrap a type in [] for a list. Stack the ! too: [String!]! means a non-null list of non-null strings.

type Book {
  id: ID!
  title: String!
  pages: Int
}

type Author {
  id: ID!
  name: String!
  books: [Book] # A list of books, can be empty or null
  tags: [String!]! # A list of non-null strings, and the list itself is non-null
}

Entry Point: The Query Type

Every schema needs the root Query type — it defines all the entry points for clients to read data. Queries always start here.

type Query {
  # Query fields will go here
}

Defining Query Fields

Fields on Query are the data you can fetch, and they take arguments to filter — like book(id: ID!): Book to grab one book by id.

type Author {
  id: ID!
  name: String!
}

type Book {
  id: ID!
  title: String!
}

type Query {
  allAuthors: [Author!]!
  bookById(id: ID!): Book
}

Your First Complete Schema

Here it all comes together: Author and Book types, a Query entry point, and a Book.author field that links the two. Relationships!

type Author {
  id: ID!
  name: String!
  books: [Book!]! # An author has a list of non-null books
}

type Book {
  id: ID!
  title: String!
  pages: Int
  author: Author # A book has one author
}

type Query {
  allBooks: [Book!]!
  book(id: ID!): Book
  allAuthors: [Author!]!
  author(id: ID!): Author
}

Schema Check-up

What does ! mean, and where does Query fit? Prove it.

Schema Design Recap

You designed a real schema: object types with the type keyword, scalar fields, ! for non-null, [] for lists, and Query as the read entry point.

เริ่มต้นได้ฟรี

เรียนรู้ GraphQL APIs with Spring Boot ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การออกแบบสคีมา GraphQL แรกของคุณ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การออกแบบสคีมา GraphQL แรกของคุณ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส GraphQL APIs with Spring Boot ให้อัปเกรดเป็น CoddyKit PRO คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การออกแบบสคีมา GraphQL แรกของคุณ”

เรียนรู้การกำหนดชนิดข้อมูล ฟิลด์พื้นฐาน และทำความเข้าใจภาษานิยามสคีมา (SDL) คุณปฏิบัติ GraphQL APIs with Spring Boot ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน GraphQL APIs with Spring Boot หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน GraphQL APIs with Spring Boot บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การออกแบบสคีมา GraphQL แรกของคุณ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน GraphQL APIs with Spring Boot นี้ได้ไหม

ได้ บทเรียน GraphQL APIs with Spring Boot ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. GraphQL คืออะไร
  2. การตั้งค่า Spring Boot สำหรับ GraphQL
  3. การออกแบบสคีมา GraphQL แรกของคุณ
  4. GraphQL ปะทะ REST: การเลือกแนวทางที่เหมาะสม
← กลับไปที่ GraphQL APIs with Spring Boot