Zasady projektowania API GraphQL
Poznaj podstawy GraphQL, w tym schematy, zapytania, mutacje i subskrypcje, aby elastycznie projektować API.
Zasady projektowania API GraphQL to bezpłatna lekcja Node.js Backend Development Bootcamp na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Node.js Backend Development Bootcamp, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Node.js Backend Development Bootcamp zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
What is GraphQL?
Welcome to GraphQL API Design! GraphQL is a powerful query language for your APIs and a runtime for fulfilling those queries with your existing data.
Think of it as a way for clients (like your mobile app) to ask for exactly the data they need, no more, no less. It's an alternative to traditional REST APIs.

GraphQL vs. REST APIs
While REST APIs typically have multiple endpoints, each returning a fixed data structure, GraphQL uses a single endpoint.
- REST: Often leads to over-fetching (getting more data than needed) or under-fetching (needing multiple requests for related data).
- GraphQL: Solves this by allowing clients to specify the data shape, reducing network requests and improving efficiency.
The Core: GraphQL Schema
At the heart of every GraphQL API is its schema. The schema defines the entire API's capabilities: what data can be queried, what data can be modified, and what types of data exist.
It acts as a contract between the client and the server, ensuring both sides understand the available operations and data structures.
Schema Definition Language (SDL)
GraphQL schemas are written using the Schema Definition Language (SDL). It's a simple, intuitive language for defining types and operations.
Let's look at a basic example of defining a User type with some fields:
type User {
id: ID!
name: String!
email: String
age: Int
}Understanding SDL Types
In the previous example:
type User: Defines a new object type namedUser.id: ID!:idis a field of typeID. The!means it's non-nullable (always present).String,Int,ID: These are scalar types, GraphQL's built-in basic data types.- You can also define custom object types like
PostorComment.
Root Type: Query
The Query root type is special. It defines all the entry points for reading data from your API. Think of these as the 'GET' operations in REST.
Here's how you might add operations to fetch users or a single user by ID:
type Query {
users: [User!]!
user(id: ID!): User
}
type User {
id: ID!
name: String!
email: String
age: Int
}Executing a GraphQL Query
Once the Query type is defined, clients can request data. They specify which fields they want from the available operations. This is how you avoid over-fetching!
To get all user names and IDs:
query GetUsers {
users {
id
name
}
}Root Type: Mutation
The Mutation root type defines all the entry points for writing or changing data in your API. These are like 'POST', 'PUT', 'PATCH', and 'DELETE' operations in REST.
Mutations often take input arguments and return the modified object.
type Mutation {
createUser(name: String!, email: String, age: Int): User!
updateUser(id: ID!, name: String, email: String, age: Int): User
deleteUser(id: ID!): Boolean!
}Executing a GraphQL Mutation
Similar to queries, clients send mutations to perform data modifications. They specify the mutation name, its arguments, and what fields of the result they want back.
Here's an example to create a new user:
mutation CreateNewUser {
createUser(name: "Alice", email: "alice@example.com", age: 30) {
id
name
email
}
}Schema Design Quick Check
Consider the following GraphQL schema snippet. Which statements about it are TRUE?
type Book {
id: ID!
title: String!
author: Author!
}
type Author {
id: ID!
name: String!
books: [Book!]
}
type Query {
books: [Book!]!
book(id: ID!): Book
authors: [Author!]!
}
type Mutation {
createBook(title: String!, authorId: ID!): Book!
}Recap & Beyond
Great job! You've learned the core principles of GraphQL API design:
- GraphQL allows clients to request specific data.
- The Schema Definition Language (SDL) defines the API's contract.
- Object types define data structures.
- The
Queryroot type handles data fetching. - The
Mutationroot type handles data modification.
Next, we'll dive into building a GraphQL server with Apollo to bring these designs to life!
Ucz się JavaScript dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 22
- Lekcje
- 92
Często zadawane pytania
Czy lekcja „Zasady projektowania API GraphQL” jest bezpłatna?
Tak — pełny tekst „Zasady projektowania API GraphQL” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Node.js Backend Development Bootcamp, przejdź na CoddyKit PRO. Kurs Node.js Backend Development Bootcamp zawiera 4 lekcji w sumie.
Co nauczysz się w „Zasady projektowania API GraphQL”?
Poznaj podstawy GraphQL, w tym schematy, zapytania, mutacje i subskrypcje, aby elastycznie projektować API. Ćwiczysz Node.js Backend Development Bootcamp z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Node.js Backend Development Bootcamp?
Nie wymagamy żadnego doświadczenia. Node.js Backend Development Bootcamp w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Zasady projektowania API GraphQL”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Node.js Backend Development Bootcamp?
Tak. Każda lekcja Node.js Backend Development Bootcamp zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Wprowadzenie do serverless z Node.js
- Zasady projektowania API GraphQL
- Tworzenie serwera GraphQL z Apollo
- Subskrypcje GraphQL dla danych w czasie rzeczywistym