博客

🚀 Gato GraphQL 版本 0.8 正式发布!

Leonardo Losoviz
作者:Leonardo Losoviz ·

Gato GraphQL 版本 0.8 现已可供下载! 🎉

这是一次重大发布,聚焦于以下三个领域:

  1. 重构代码库以支持扩展功能
  2. 进一步满足 GraphQL 规范
  3. 完善 GraphQL schema

此外,本版本支持新的 WordPress 5.8,并包含大量错误修复与改进。

请注意,此版本包含破坏性变更

以下是发布说明。快速链接:


WordPress 5.8 支持

WordPress 5.8 弃用了多个过滤器钩子,包括本插件使用的 allowed_block_typesblock_categories

受影响的钩子已被替换:

  1. allowed_block_types => allowed_block_types_all
  2. block_categories => block_categories_all

改进的 PHP 8.0 支持

本版本修复了使用 PHP 8.0 时的若干问题。

全面使用容器服务简化代码库

GraphQL 服务器的代码库已完成重构,改为使用服务容器来注册 schema 的所有元素(type resolver、field resolver、interface resolver、custom scalar resolver 等)。

这是一个里程碑,为开发插件及其扩展引入了统一的方式,大幅简化了代码和文档。

关于如何为 Gato GraphQL 创建自定义扩展的文档终于可以开始编写了。相关工作即将启动,并将发布在指南部分。

缓存保存在 wp-content

插件会将结果缓存到磁盘以优化性能。

缓存文件以前存储在系统文件夹下,管理员用户不可见。从现在起,它们将存储在 wp-content/graphql-api/cache/ 下。

引入"固定 schema"GraphQL 端点以驱动 WordPress 编辑器

现在,wp-admin 中有 2 个端点:

  1. GRAPHQL_API_ADMIN_CONFIGURABLESCHEMA_ENDPOINT
  2. GRAPHQL_API_ADMIN_FIXEDSCHEMA_ENDPOINT

使用 GRAPHQL_API_ADMIN_CONFIGURABLESCHEMA_ENDPOINT 时,GraphQL schema 会根据用户偏好进行修改,例如是否命名空间化、是否启用某些类型/指令等。

使用 GRAPHQL_API_ADMIN_FIXEDSCHEMA_ENDPOINT 时,GraphQL schema 不受用户偏好影响,始终公开所有类型、字段和指令,包括"无限制"管理员字段。

固定端点使 Gutenberg 块能够查询所有字段,无论这些字段是否被用户启用,并具有无限制访问权限。

进一步支持 schema 中的字段类型

对列表作为字段类型的支持已扩展,现在支持以下特性:

  • 非空元素的列表:[String!]
  • 列表的列表:[[String]]
  • 任意组合:[[String!]!]

输入强制转换:在期望列表时接受单个值

现在可以在 GraphQL Query 中期望列表的位置输入单个值,正如 GraphQL 规范所定义的

例如,以下 Query 现在是等价的:

query InputSingleValue {
  posts(filter: { ids: 1 }) {
    title
  }
}
 
query InputListOfSingleItem {
  posts(filter: { ids: [1] }) {
    title
  }
}

进一步完善 WordPress schema

WordPress 数据模型中的其他实体已被添加到 GraphQL schema:

GraphQL schema

让我们看看新增了哪些元素。

分类

分类已通过新的 PostCategory 类型和以下新字段完成映射:

  • Root.postCategories: [PostCategory]
  • Root.postCategory: PostCategory
  • Post.categories: [PostCategory]

例如,以下 Query 可获取文章的分类:

{
  posts {
    id
    title
    categories {
      id
      name
      url
    }
  }
}

还新增了一个 mutation 字段,用于为文章分配分类:

  • MutationRoot.setCategoriesOnPost: Post

并且在文章的 mutation 字段中新增了 categories 输入:

  • MutationRoot.createPost
  • MutationRoot.updatePost
  • Post.update(启用嵌套 mutation 时)

元数据

自定义文章、用户、评论和分类法的元数据值现在可以通过以下新字段进行查询:

  • Post.metaValue: AnyScalar
  • Post.metaValues: [AnyScalar]
  • User.metaValue: AnyScalar
  • User.metaValues: [AnyScalar]
  • Comment.metaValue: AnyScalar
  • Comment.metaValues: [AnyScalar]
  • PostCategory.metaValue: AnyScalar
  • PostCategory.metaValues: [AnyScalar]
  • PostTag.metaValue: AnyScalar
  • PostTag.metaValues: [AnyScalar]

例如,以下 Query 可获取用户的元数据 last_name

{
  users {
    id
    lastName: metaValue(key: "last_name")
  }
}

由于元数据值可以是任意类型(string、integer、float 或 boolean),它们通过新引入的通用标量类型 AnyScalar 进行映射。

元数据值可以是公开或私有的。哪些元数据键可被查询,必须在设置页面中明确配置:

定义条目
定义条目

默认情况下,允许的元数据键列表为空。

菜单

菜单已通过新的 Menu 类型和新字段 Root.menu 完成映射。

{
  menu(by: { id: 176 }) {
    itemDataEntries
  }
}

设置

站点设置(存储在 wp_options 表中)现在可以通过新字段 Root.option: AnyScalar 进行查询。

例如,以下 Query 可获取站点名称:

{
  siteName: optionValue(name: "blogname")
}

哪些选项可以被访问,必须在设置页面中明确配置:

Defining the entries for the Settings

默认情况下,只有以下选项可以查询:

  • "home"
  • "blogname"
  • "blogdescription"

用户文章

已登录的用户现在可以通过以下新字段获取自己的文章,支持任意状态(publishpendingdrafttrash):

  • Root.myPosts: [Post]
  • Root.myPostCount: Int
  • Root.myPost: Post

例如,现在可以执行以下 Query:

# Log yourself in first
mutation LogIn {
  loginUser(usernameOrEmail: "test", password: "pass") {
    id
    name
  }
}
 
# Then retrieve your posts
query GetMyPosts {
  myPosts {
    id
    title
    url
    status
    author {
      name
    }
  }
}

向 GraphQL schema 添加"无限制"管理员字段

GraphQL schema 必须在公开字段和私有字段之间取得平衡,以避免在公共 API 中暴露私有信息。

新模块 Schema for the Admin 向 GraphQL schema 添加了可能暴露私有数据的"无限制"管理员字段:

Root:

  • unrestrictedPost
  • unrestrictedPosts
  • unrestrictedPostCount
  • unrestrictedCustomPost
  • unrestrictedCustomPosts
  • unrestrictedCustomPostCount
  • unrestrictedGenericCustomPost
  • unrestrictedGenericCustomPosts
  • unrestrictedGenericCustomPostCount
  • unrestrictedPage
  • unrestrictedPages
  • unrestrictedPageCount
  • unrestrictedUsers
  • roles
  • capabilities

User:

  • unrestrictedPosts
  • unrestrictedPostCount
  • unrestrictedCustomPosts
  • unrestrictedCustomPostCount
  • roles
  • capabilities

PostCategory:

  • unrestrictedPosts
  • unrestrictedPostCount

PostTag:

  • unrestrictedPosts
  • unrestrictedPostCount

例如,访问文章数据时,目前有字段 posts,它通过获取已发布的文章仅公开公共数据。

从现在起,也可以通过字段 unrestrictedPosts 访问文章数据,它通过获取任意状态("publish""draft""pending""trash")的文章来公开公共和私有数据。

{
  unrestrictedPosts(status: [draft, pending]) {
    id
    title
    status
    author {
      id
      name
    }
  }
}

引入标量类型 AnyScalar

标量类型 AnyScalar 表示任意内置标量(StringIntBooleanFloatID)。

它用于新引入的 optionmetaValue(s) 字段,因为我们无法预先知道其返回数据的类型,且标量类型的联合体尚未被 GraphQL 规范支持。

长格式设置

设置页面中的选项按选项卡划分。从 v0.8 起,也可以在一个长页面中统一查看所有选项。

要启用此功能,请在设置中取消勾选 "Have all options in this Settings page be organized under tabs, one tab per module.",然后点击 "Save Changes"

Checkbox to enable/disable tabs in Settings

之后,所有设置将以长格式统一显示:

Settings in long format


破坏性变更

版本 v0.8 与之前的版本存在破坏性变更。

配置破坏性变更

以下 CPT 的"Options"块已重建:

  • Schema Configurations
  • Custom Endpoints
  • Persisted Queries

在之前的 v0.7 中,这些实体的单个 Options 块包含了许多配置项。从 v0.8 起,该块已被拆分为多个独立的块,每个块包含各自的配置。

例如,在 v0.7 中,Custom Endpoint Options 块允许配置 GraphiQL 和 Interactive Schema 客户端(除了启用/禁用端点之外):

Options in Custom Endpoint

v0.8 起,此配置通过 GraphiQL 和 Interactive Schema 块添加:

Options in Custom Endpoint

所有 3 个 CPT 的 Options 块中存储的配置不会自动迁移到新格式。因此,在升级到 v0.8 之前,请记录您存储的配置,并在升级到新版本后重新配置。

对于此不便之处,深感抱歉。

此外,您需要在 WordPress 编辑器中对所有 3 个 CPT 的所有条目点击"Reset the template"按钮。

Reset the template in the WordPress editor

已移除非标准指令

非标准指令已从插件中移除:

  • @default
  • @removeIfNull
  • @export

已移除的模块

以下模块已从插件中移除:

  • Field Deprecation
  • Configuration Cache
  • Schema Cache
  • Multiple Query Execution
  • Proactive Feedback
  • Schema Editing Access
  • Embeddable fields

未来路线图

v0.8 发布后,我们可以开始规划未来的路线图。

当前计划如下:

2021 年 9 月发布 v0.9,包括:

  • Custom scalars
  • 在适当情况下使用 custom scalars 的更新 GraphQL schema(例如:Post.date 将返回 Date 类型而非 String
  • 进一步增强对扩展功能的支持

然后,在年底或 2022 年初发布 v1.0,包括:

  • 扩展插件的演示
  • 关于创建扩展的完整文档指南
  • wp.org 上发布 Gato GraphQL 插件

要接收最新状态的通知,您可以订阅新闻通讯


遇到问题了吗?

如果您在安装或运行 v0.8 时遇到任何问题,请在仓库中创建 issue


订阅我们的新闻通讯

及时了解 Gato GraphQL 的所有更新。