Skip to main content

GraphQL API 已更新

¥The GraphQL API has been updated

在 Strapi 5 中,GraphQL API 已更新。它处理新的扁平响应格式(参见 相关重大更改更改),现在还可以接受 中继样式 查询。

¥In Strapi 5, the GraphQL API has been updated. It handles the new, flattened response format (see related breaking change), and can also now accept Relay-style queries.

扁平查询仍然返回一个简单的文档数组。你还可以使用 Relay 风格的 *_connection 查询,它返回 nodespageInfo 对象来处理分页。当需要有关页面或总数的元数据时,请使用这些查询。

¥Flat queries still return a simple array of documents. You can also use Relay-style *_connection queries, which return nodes and a pageInfo object to handle pagination. Use these when you need metadata about pages or total counts.

此页面是 重大变更数据库 的一部分,提供有关重大更改的信息以及从 Strapi v4 迁移到 Strapi 5 的其他说明。

¥This page is part of the breaking changes database and provides information about the breaking change and additional instructions to migrate from Strapi v4 to Strapi 5.

 Is this breaking change affecting plugins?Yes
 Is this breaking change automatically handled by a codemod?No

更改列表

¥List of changes

话题更改说明
文件上传支持
国际化支持删除 createXXLocalization 查询和突变
草稿和发布支持删除模块配置选项并将其拆分为 publicationStatestatus
架构更改

有关新 Strapi 5 GraphQL API 的详细描述,请参阅 GraphQL API 参考文档。

¥For an extensive description of the new Strapi 5 GraphQL API, please refer to the GraphQL API reference documentation.

迁移

¥Migration

要逐步转换为新的 GraphQL API 格式,请按照以下步骤操作:

¥To gradually convert to the new GraphQL API format, follow these steps:

  1. 启用 v4CompatibilityMode 向后兼容标头,以便在重构客户端时查询可以继续依赖 data.attributes.*。在 config/plugins.{js,ts} 中进行配置。启用此标志后,服务器将持续返回 Strapi v4 格式。

    ¥Enable the v4CompatibilityMode retro-compatibility header so queries can continue to rely on data.attributes.* while you refactor clients. Configure it in config/plugins.{js,ts}. With the flag on, the server keeps returning the Strapi v4 shape.

config/plugins.js
   module.exports = {
graphql: {
config: {
v4CompatibilityMode: true,
},
},
};
{
restaurants {
data {
id
attributes {
title
image {
data {
id
attributes {
url
}
}
}
images {
data {
id
attributes {
url
}
}
}
xToOneRelation {
data {
id
attributes {
field
}
}
}
xToManyRelation {
data {
id
attributes {
field
}
}
}
}
}
meta {
pagination {
page
pageSize
}
}
}
}
  1. 采用 documentId,它替换 GraphQL 中的数字 id。更新查询和变更以读取和发送 documentId,即使在启用兼容模式的情况下也是如此。

    ¥Adopt documentId which replaces numeric id in GraphQL. Update queries and mutations to read and send documentId, even while compatibility mode is on.

    {
    restaurants {
    data {
    documentId
    attributes {
    title
    image {
    data {
    documentId
    attributes {
    url
    }
    }
    }
    images {
    data {
    documentId
    attributes {
    url
    }
    }
    }
    xToOneRelation {
    data {
    documentId
    attributes {
    field
    }
    }
    }
    xToManyRelation {
    data {
    documentId
    attributes {
    field
    }
    }
    }
    }
    }
    }
    }
    mutation UpdateRestaurant {
    updateRestaurant(
    documentId: "some-doc-id",
    data: { title: "My great restaurant" }
    ) {
    data {
    documentId
    attributes {
    title
    image {
    data {
    documentId
    attributes {
    url
    }
    }
    }
    }
    }
    }
    }
  2. 使用其 _connection 变体重命名集合字段。这可以解锁 Relay 分页元数据,同时保持 v4 风格的 dataattributes 格式。

    ¥Rename collection fields with their _connection variants. This unlocks Relay pagination metadata while still keeping the v4-style data and attributes shape.

    {
    # collection fields can be renamed to _connection to get a v4 compat response
    restaurants_connection {
    data {
    id
    attributes {
    title
    image {
    data {
    id
    attributes {
    url
    }
    }
    }
    # collection fields can be renamed to _connection to get a v4 compat response
    images_connection {
    data {
    id
    attributes {
    url
    }
    }
    }
    xToOneRelation {
    data {
    id
    attributes {
    field
    }
    }
    }
    # collection fields can be renamed to _connection to get a v4 compat response
    xToManyRelation_connection {
    data {
    id
    attributes {
    field
    }
    }
    }
    }
    }
    meta {
    pagination {
    page
    pageSize
    }
    }
    }
    }
  3. 一旦集合查询和单重查询都使用 *_connection,就停止将用户字段封装在 attributes 中。这适用于查询和变更响应。

    ¥Once collection and single queries use *_connection, stop wrapping user fields in attributes. This applies to queries and mutation responses.

    {
    # collection fields can be renamed to _connection to get a v4 compat response
    restaurants_connection {
    data {
    id
    title
    image {
    data {
    id
    url
    }
    }
    # collection fields can be renamed to _connection to get a v4 compat response
    images_connection {
    data {
    id
    url
    }
    }
    xToOneRelation {
    data {
    id
    field
    }
    }
    # collection fields can be renamed to _connection to get a v4 compat response
    xToManyRelation_connection {
    data {
    id
    field
    }
    }
    }
    meta {
    pagination {
    page
    pageSize
    }
    }
    }
    }
  4. (可选)如果你需要符合 Relay 规范的分页,请将 data 重命名为 nodes,将 meta.pagination 重命名为 pageInfo。如果客户端不需要分页元数据,你也可以完全删除 _connection

    ¥(Optional) If you need Relay-compliant pagination, rename data to nodes and meta.pagination to pageInfo. When a client does not need pagination metadata, you can also drop _connection entirely.

    {
    # Rename data to nodes & meta.pagination to pageInfo
    restaurants_connection {
    nodes {
    id
    title
    image {
    id
    url
    }
    images_connection {
    nodes {
    id
    url
    }
    }
    xToOneRelation {
    id
    field
    }
    xToManyRelation_connection {
    nodes {
    id
    field
    }
    }
    }
    pageInfo {
    page
    pageSize
    }
    }
    }
    {
    # remove _connection & data if you don't need pagination att all
    restaurants {
    id
    title
    image {
    id
    url
    }
    images {
    id
    url
    }
    xToOneRelation {
    id
    field
    }
    xToManyRelation {
    id
    field
    }
    }
    }
  5. 禁用 v4CompatibilityMode 兼容性标头,以便服务器以原生方式发出 Strapi 5 格式。

    ¥Disable the v4CompatibilityMode compatibility header so the server emits the Strapi 5 format natively.