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.

