执行简单的 GraphQL 查询
练习构建并执行基本查询,使用 GraphiQL 或 GraphQL Playground 等工具获取数据。
执行简单的 GraphQL 查询 是 CoddyKit 上的免费 GraphQL APIs with Spring Boot 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 GraphQL APIs with Spring Boot 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 GraphQL APIs with Spring Boot 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What are GraphQL Queries?
Welcome to executing GraphQL queries! In this lesson, we'll learn how to fetch data from a GraphQL server.
A GraphQL Query is how a client application asks the server for data. Unlike traditional REST APIs where you often get fixed data structures, GraphQL lets you specify exactly what data you need.
Interactive Query Tools
To execute queries, we often use interactive tools. Two popular ones are GraphiQL and GraphQL Playground.
- These are browser-based IDEs (Integrated Development Environments).
- They provide features like schema exploration, auto-completion for queries, and documentation.
- They connect to your GraphQL server's endpoint to send queries and display results.
The Basic Query Structure
A GraphQL query starts by selecting fields from the root Query type. You specify the fields you want, and the server returns only that data.
The query keyword is optional for simple queries, but it's good practice to include it for clarity and to name your operations.
Selecting Specific Fields
Let's say our schema has a Book type with title and author fields. To get a list of all books with their titles and authors, we'd write:
query {
allBooks {
title
author
}
}Notice the curly braces {} define a selection set for the allBooks field.
Querying Nested Data
One of GraphQL's strengths is querying nested data in a single request. If our author field is an object type with its own fields (like name and nationality), we can select them too:
query {
allBooks {
title
author {
name
nationality
}
}
}This fetches books and their authors' names and nationalities, all at once!
Passing Arguments to Fields
You can pass arguments to fields to filter, paginate, or specify a particular item. Arguments are defined in the schema and act like function parameters.
For example, to fetch a specific book by its ID:
query {
bookById(id: "1") {
title
author {
name
}
}
}Here, id: "1" is an argument passed to the bookById field.
Aliases for Field Renaming
What if you need to query the same field multiple times with different arguments? Aliases let you rename the result of a field.
This prevents conflicts in the JSON response when the same field name would appear multiple times:
query {
firstBook: bookById(id: "1") {
title
}
secondBook: bookById(id: "2") {
title
}
}The results will be firstBook and secondBook.
Reusing Selections with Fragments
Fragments are reusable units of selection logic. They let you define a set of fields once and then reuse them across multiple queries or within the same query.
This is useful for complex queries or when different parts of your UI need the same data subset:
fragment BookDetails on Book {
title
author {
name
}
}
query {
bookById(id: "1") {
...BookDetails
}
}The ...BookDetails syntax spreads the fields from the fragment into the query.
Operation Names & Variables
Giving your queries an operation name (e.g., GetBookDetails) is good for debugging and logging on the server side.
You can also define variables to pass dynamic data to your queries, making them more flexible:
query GetBookDetails($bookId: ID!) {
bookById(id: $bookId) {
title
author {
name
}
}
}The variable $bookId would be provided separately, usually as a JSON object (e.g., {"bookId": "3"}) in your query tool.
Querying Best Practices
Keep these tips in mind when constructing your queries:
- Be specific: Only ask for the data you truly need.
- Use fragments: For reusable selection sets, especially across different parts of your application.
- Name operations: It helps with debugging and monitoring.
- Use variables: For dynamic arguments, rather than hardcoding values directly into the query string.
- Explore the schema: Use GraphiQL/Playground's documentation explorer to understand available fields and arguments.
Quick Check
Which of the following GraphQL query snippets correctly uses an alias to fetch the title of two different books?
Querying Essentials Recap
Great job! You've covered the basics of executing GraphQL queries:
- How queries specify exactly what data to fetch.
- Using tools like GraphiQL and GraphQL Playground.
- The basic structure, including selecting fields and nested data.
- Passing arguments to fields for filtering.
- Using aliases to rename field results.
- Leveraging fragments for reusable selection sets.
- The importance of operation names and variables.
Now you're ready to start fetching data efficiently!
常见问题解答
「执行简单的 GraphQL 查询」课时是免费的吗?
是的 — 「执行简单的 GraphQL 查询」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 GraphQL APIs with Spring Boot 课程的其余内容,请升级到 CoddyKit PRO。 GraphQL APIs with Spring Boot 课程共包含 4 节课。
「执行简单的 GraphQL 查询」这节课中我会学到什么?
练习构建并执行基本查询,使用 GraphiQL 或 GraphQL Playground 等工具获取数据。 你通过在浏览器中直接运行的动手代码来练习 GraphQL APIs with Spring Boot,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 GraphQL APIs with Spring Boot 需要有经验吗?
无需任何先前经验。CoddyKit 上的 GraphQL APIs with Spring Boot 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「执行简单的 GraphQL 查询」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 GraphQL APIs with Spring Boot 课中编写并运行代码吗?
能。每节 GraphQL APIs with Spring Boot 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 定义自定义数据类型
- 实现数据解析器
- 执行简单的 GraphQL 查询
- GraphQL 变更:修改数据