0Pricing
NestJS Enterprise Backend APIs · 课时

GraphQL 基础

理解 GraphQL 的核心概念,包括查询、变更、订阅和模式定义语言(SDL)。

GraphQL 基础 是 CoddyKit 上的免费 NestJS Enterprise Backend APIs 课时。 这是第 1 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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!

常见问题解答

「GraphQL 基础」课时是免费的吗?

是的 — 「GraphQL 基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 NestJS Enterprise Backend APIs 课程的其余内容,请升级到 CoddyKit PRO。 NestJS Enterprise Backend APIs 课程共包含 3 节课。

「GraphQL 基础」这节课中我会学到什么?

理解 GraphQL 的核心概念,包括查询、变更、订阅和模式定义语言(SDL)。 你通过在浏览器中直接运行的动手代码来练习 NestJS Enterprise Backend APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 NestJS Enterprise Backend APIs 需要有经验吗?

无需任何先前经验。CoddyKit 上的 NestJS Enterprise Backend APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 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