NestJS Enterprise Backend APIs · บทเรียน

พื้นฐาน GraphQL

ทำความเข้าใจแนวคิดหลักของ GraphQL ซึ่งรวมถึงการสืบค้น การกลายข้อมูล การสมัครรับข้อมูล และภาษานิยามสคีมา (SDL)

บทเรียน 1 จาก 312 ขั้นตอน

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

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

What is GraphQL?

Welcome to GraphQL! It's a powerful query language for APIs and a runtime for fulfilling those queries with your existing data.

Think of it as asking for exactly what you need, nothing more, nothing less. This helps make your APIs efficient and flexible.

GraphQL vs. REST APIs

Traditionally, REST APIs often require multiple requests to fetch all necessary data, leading to "over-fetching" or "under-fetching".

  • Over-fetching: Getting more data than you need.
  • Under-fetching: Needing multiple requests to get all data.

GraphQL solves this by letting clients define the exact data structure they want in a single request.

The Core: GraphQL Schema

At the heart of every GraphQL API is its schema. This schema acts as a contract between the client and the server.

It defines all the data types and operations (queries, mutations, subscriptions) that clients can perform. It's like a blueprint for your API.

Defining Schemas with SDL

GraphQL schemas are written using the Schema Definition Language (SDL). It's a simple, human-readable syntax.

SDL allows you to specify your API's capabilities and data structures clearly. It's language-agnostic, meaning it works with any backend language.

schema {
  query: Query
}

type Query {
  hello: String
}

Building Blocks: Object Types

The most fundamental component of a GraphQL schema is the Object Type. It represents a type of object you can fetch from your service.

Each object type has fields, which are specific pieces of data it can hold. Fields can also be other object types!

type User {
  id: ID!
  name: String!
  email: String
  posts: [Post!]
}

type Post {
  id: ID!
  title: String!
  content: String
}

Data Types: Scalars & Lists

Scalar types are the basic building blocks that resolve to a single value, like String, Int, Float, Boolean, and ID (a unique identifier).

List types allow a field to return a list of a certain type, indicated by []. The ! means a field is Non-Null, so it must always return a value.

type Product {
  id: ID!         # Non-null ID
  name: String!   # Non-null String
  price: Float!   # Non-null Float
  tags: [String!] # List of non-null Strings
  inStock: Boolean
}

Fetching Data with Queries

Queries are how clients request data from a GraphQL server. You specify the exact fields you want to receive.

This ensures you only get the data you ask for, avoiding over-fetching common in REST APIs.

query GetProductNames {
  products {
    id
    name
  }
}

Dynamic Queries with Arguments

Fields in GraphQL can take arguments, allowing you to pass parameters to customize the data fetched. This is useful for filtering or finding specific items.

Arguments are defined in the schema and used by the client when making a query.

query GetProductById {
  product(id: "prod123") {
    id
    name
    price
  }
}

Modifying Data with Mutations

While queries are for reading data, mutations are for writing, updating, or deleting data on the server.

Like queries, mutations also specify what data to return after the operation, ensuring immediate feedback on changes.

mutation CreateNewProduct {
  createProduct(name: "New Gadget", price: 99.99) {
    id
    name
  }
}

Real-time Updates: Subscriptions

Subscriptions are a special type of operation that allows clients to receive real-time updates from the server when specific events occur.

This is perfect for features like live chat, notifications, or real-time dashboards. It typically uses WebSockets.

Schema Fundamentals Check

Consider the following GraphQL SDL snippet for an Author type:

type Author {
  id: ID!
  name: String!
  books: [Book!]
}

Which of the following statements are true about the books field?

GraphQL Fundamentals Recap

Great job! You've learned the core concepts of GraphQL:

  • It's a query language for APIs, offering precise data fetching.
  • The schema defines the API's capabilities using SDL.
  • You use queries to read, mutations to write, and subscriptions for real-time data.

Next, we'll see how to integrate GraphQL into a NestJS application!

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

เรียนรู้ TypeScript ด้วย AI tutor — ฟรี

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

คอร์ส
20
บทเรียน
76

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

บทเรียน “พื้นฐาน GraphQL” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “พื้นฐาน GraphQL”

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

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

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

บทเรียน “พื้นฐาน GraphQL” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน NestJS Enterprise Backend APIs นี้ได้ไหม

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

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

  1. พื้นฐาน GraphQL
  2. การตั้งค่า NestJS GraphQL
  3. ตัวแก้ไขและการสมัครรับข้อมูล
← กลับไปที่ NestJS Enterprise Backend APIs