GraphQL API
GraphQL API 允许执行查询和突变,以通过 Strapi 的 GraphQL 插件 与 content-types 进行交互。结果可以是 filtered、sorted 和 paginated。
¥The GraphQL API allows performing queries and mutations to interact with the content-types through Strapi's GraphQL plugin. Results can be filtered, sorted and paginated.
要使用 GraphQL API,请安装 GraphQL 插件:
¥To use the GraphQL API, install the GraphQL plugin:
- Yarn
- NPM
yarn add @strapi/plugin-graphql
npm install @strapi/plugin-graphql
安装后,可通过 /graphql URL 访问 GraphQL 在线运行,并可用于以交互方式构建查询和修改,以及阅读针对你的内容类型量身定制的文档:
¥Once installed, the GraphQL playground is accessible at the /graphql URL and can be used to interactively build your queries and mutations and read documentation tailored to your content-types:

GraphQL 插件仅公开一个处理所有查询和变更的端点。默认端点为 /graphql,并在 插件配置文件 中定义:
¥The GraphQL plugin exposes only one endpoint that handles all queries and mutations. The default endpoint is /graphql and is defined in the plugins configuration file:
export default {
shadowCRUD: true,
endpoint: '/graphql', // <— single GraphQL endpoint
subscriptions: false,
maxLimit: -1,
apolloServer: {},
v4CompatibilityMode: process.env.STRAPI_GRAPHQL_V4_COMPATIBILITY_MODE ?? false,
};
GraphQL API 不支持媒体上传。对所有文件上传使用 REST API POST /upload 端点,并使用返回的信息在内容类型中链接到它。你仍然可以使用媒体文件 id(参见 媒体文件的修改)更新或删除具有 updateUploadFile 和 deleteUploadFile 突变的上传文件。
¥The GraphQL API does not support media upload. Use the REST API POST /upload endpoint for all file uploads and use the returned info to link to it in content types. You can still update or delete uploaded files with the updateUploadFile and deleteUploadFile mutations using media files id (see mutations on media files).
documentId onlyGraphQL API 仅使用 documentId 字段公开文档。之前的数字 id 在此处不再可用,但为了向后兼容,REST API 仍然返回它(详情请参阅 重大变更)。
¥The GraphQL API exposes documents using only the documentId field. The previous numeric id is no longer available here, although it is still returned by the REST API for backward compatibility (see breaking change for details).
查询
¥Queries
GraphQL 中的查询用于获取数据而不修改数据。
¥Queries in GraphQL are used to fetch data without modifying it.
当将内容类型添加到你的项目时,2 个自动生成的 GraphQL 查询将添加到你的架构中,以内容类型的单数和复数 API ID 命名,如下例所示:
¥When a content-type is added to your project, 2 automatically generated GraphQL queries are added to your schema, named after the content-type's singular and plural API IDs, as in the following example:
| 内容类型显示名称 | Singular API ID | 复数 API ID |
|---|---|---|
| 餐厅 | restaurant | restaurants |
Singular API ID vs. Plural API ID:
在 Content-Type Builder 中创建内容类型时定义单数 API ID 和复数 API ID 值,并且可以在管理面板中编辑内容类型时找到(参见 用户指南)。你可以在创建内容类型时定义自定义 API ID,但之后无法修改这些 ID。
¥Singular API ID and Plural API ID values are defined when creating a content-type in the Content-Type Builder, and can be found while editing a content-type in the admin panel (see User Guide). You can define custom API IDs while creating the content-type, but these can not modified afterwards.


获取单个文档
¥Fetch a single document
文档 可以通过其 documentId 获取。
¥Documents can be fetched by their documentId.
{
restaurant(documentId: "a1b2c3d4e5d6f7g8h9i0jkl") {
name
description
}
}
获取多个文档
¥Fetch multiple documents
要获取多个文档 ,你可以使用简单、扁平的查询或 中继样式 查询:
¥To fetch multiple documents you can use simple, flat queries or Relay-style queries:
扁平查询仅返回每个文档所请求的字段。中继式查询以 _connection 结尾,并返回一个 nodes 数组和一个 pageInfo 对象。 当需要分页元数据时,请使用 Relay 样式的查询。
¥Flat queries return only the requested fields for each document. Relay-style queries end with _connection and return a nodes array together with a pageInfo object. Use Relay-style queries when you need pagination metadata.
- Flat queries
- Relay-style queries
要获取多个文档,你可以使用如下所示的扁平查询:
¥To fetch multiple documents you can use flat queries like the following:
restaurants {
documentId
title
}
关系也可以通过文档服务 API 连接、断开和设置,就像 REST API 一样(有关示例,请参阅 )。
¥Relay-style queries can be used to fetch multiple documents and return meta information:
{
restaurants_connection {
nodes {
documentId
name
}
pageInfo {
pageSize
page
pageCount
total
}
}
}
获取关系
¥Fetch relations
你可以要求在扁平查询或 中继样式 查询中包含关系数据:
¥You can ask to include relation data in your flat queries or in your Relay-style queries:
- Flat queries
- Relay-style queries
以下示例从 "餐厅" 内容类型中获取所有文档,并且对于每个文档,还返回与 "类别" 内容类型的多对多关系的一些字段:
¥The following example fetches all documents from the "Restaurant" content-type, and for each of them, also returns some fields for the many-to-many relation with the "Category" content-type:
{
restaurants {
documentId
name
description
# categories is a many-to-many relation
categories {
documentId
name
}
}
}
以下示例使用 Relay 样式查询从 "餐厅" 内容类型中获取所有文档,并且对于每个餐厅,还返回与 "类别" 内容类型的多对多关系的一些字段:
¥The following example fetches all documents from the "Restaurant" content-type using a Relay-style query, and for each restaurant, also returns some fields for the many-to-many relation with the "Category" content-type:
{
restaurants_connection {
nodes {
documentId
name
description
# categories is a many-to-many relation
categories_connection {
nodes {
documentId
name
}
}
}
pageInfo {
page
pageCount
pageSize
total
}
}
}
目前,pageInfo 仅适用于第一级文档。Strapi 的未来实现可能会为关系实现 pageInfo。
¥For now, pageInfo only works for documents at the first level. Future implementations of Strapi might implement pageInfo for relations.
Possible use cases for pageInfo:
这有效:
¥This works:
{
restaurants_connection {
nodes {
documentId
name
description
# many-to-many relation
categories_connection {
nodes {
documentId
name
}
}
}
pageInfo {
page
pageCount
pageSize
total
}
}
}