博客

⭐️ v4.2 发布:新增标签与分类的 mutation、改进媒体 mutation、增强 Polylang 集成(PRO)

Leonardo Losoviz
作者:Leonardo Losoviz ·

Gato GraphQL v4.2 已发布。请查看 GitHub 上的发布说明以获取完整的变更列表。

以下是最重要的新功能。

新增标签和分类的 mutation

通过新增的 mutation,现在可以创建、更新和删除文章标签及分类:

  • PostCategory.delete
  • PostCategory.update
  • PostTag.delete
  • PostTag.update
  • Root.createPostCategory
  • Root.createPostTag
  • Root.deletePostCategory
  • Root.deletePostTag
  • Root.updatePostCategory
  • Root.updatePostTag

自定义标签和分类也同样支持,通过新增的 mutation 实现:

  • GenericCategory.delete
  • GenericCategory.update
  • GenericTag.delete
  • GenericTag.update
  • Root.createCategory
  • Root.createTag
  • Root.deleteCategory
  • Root.deleteTag
  • Root.updateCategory
  • Root.updateTag

此 Query 用于创建、更新和删除文章标签 term:

mutation CreateUpdateDeletePostTags {
  createPostTag(input: {
    name: "Some name"
    slug: "Some slug"
    description: "Some description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...PostTagData
    }
  }
 
  updatePostTag(input: {
    id: 1
    name: "Some updated name"
    slug: "Some updated slug"
    description: "Some updated description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...PostTagData
    }
  }
 
  deletePostTag(input: {
    id: 1
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}
 
fragment PostTagData on PostTag {
  id
  name
  slug
  description
}

此 Query 用于创建、更新和删除文章分类 term:

mutation CreateUpdateDeletePostCategories {
  createPostCategory(input: {
    name: "Some name"
    slug: "Some slug"
    description: "Some description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...PostCategoryData
    }
  }
 
  updatePostCategory(input: {
    id: 1
    name: "Some updated name"
    slug: "Some updated slug"
    description: "Some updated description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...PostCategoryData
    }
  }
 
  deletePostCategory(input: {
    id: 1
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}
 
fragment PostCategoryData on PostCategory {
  id
  name
  slug
  description
  parent {
    id
  }
}

此 Query 用于创建、更新和删除自定义 some-tag-taxonomy 标签的标签 term:

mutation CreateUpdateDeleteTags {
  createTag(input: {
    taxonomy: "some-tag-taxonomy",
    name: "Some name"
    slug: "Some slug"
    description: "Some description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...TagData
    }
  }
 
  updateTag(input: {
    id: 1
    taxonomy: "some-tag-taxonomy"
    name: "Some updated name"
    slug: "Some updated slug"
    description: "Some updated description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...TagData
    }
  }
 
  deleteTag(input: {
    id: 1
    taxonomy: "some-tag-taxonomy"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}
 
fragment TagData on Tag {
  id
  name
  slug
  description
}

此 Query 用于创建、更新和删除自定义 some-cat-taxonomy 分类的分类 term:

mutation CreateUpdateDeleteCategories {
  createCategory(input: {
    taxonomy: "some-cat-taxonomy",
    name: "Some name"
    slug: "Some slug"
    description: "Some description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...CategoryData
    }
  }
 
  updateCategory(input: {
    id: 1
    taxonomy: "some-cat-taxonomy"
    name: "Some updated name"
    slug: "Some updated slug"
    description: "Some updated description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...CategoryData
    }
  }
 
  deleteCategory(input: {
    id: 1
    taxonomy: "some-cat-taxonomy"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}
 
fragment CategoryData on Category {
  id
  name
  slug
  description
  parent {
    id
  }
}

使用现有媒体项的附件创建媒体项

createMediaItem mutation 现在可以使用与现有媒体项相同的附件来创建新的媒体项(即无需在磁盘上复制文件):

mutation {
  createMediaItem(input: {
    from: {
      mediaItemBy: {
        id: 337
      }
    }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    mediaItem {
      id  # New media item created
      src # Same attachment as the provided media item
    }
  }
}

[PRO] 在标签和分类的 mutation 中定义 Polylang 语言

使用 Polylang 集成时,在创建标签或分类(见上文)时,可以传入 polylangLanguageBy 输入以预先定义其语言。

例如,此 Query 创建一个文章分类,并将其语言定义为西班牙语:

mutation {
  createPostCategory(input: {
    name: "Noticias"
    polylangLanguageBy: { code: "es" }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      polylangLanguage {
        locale
      }
      name
    }
  }
}

[PRO] 新增媒体项的 Polylang Mutation

PRO 模块 Polylang Mutations 提供了与 Polylang 插件集成的 mutation。

GraphQL schema 已扩展了以下 mutation:

  • 为媒体项设置语言,以及
  • 定义媒体项之间的关联(即指定一组媒体项互为翻译)。
Mutation说明
polylangSetMediaItemLanguage设置媒体项的语言。
polylangSaveMediaItemTranslationAssociation设置媒体项的翻译关联。

例如,以下 Query 为 3 个媒体项定义语言(分别为英语、西班牙语和法语),然后将这 3 个媒体项定义为互为翻译:

mutation {
  mediaItem1: polylangSetMediaItemLanguage(input: {id: 1007, languageBy: { code: "en" }}) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
  mediaItem2: polylangSetMediaItemLanguage(input: {id: 204, languageBy: { code: "es" }}) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
  mediaItem3: polylangSetMediaItemLanguage(input: {id: 377, languageBy: { code: "fr" }}) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
  polylangSaveMediaItemTranslationAssociation(input: {
    ids: [1007, 204, 377]
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}

[PRO] 按 Polylang 默认语言筛选实体

现在可以通过在 polylangLanguagesBy 过滤器中提供 DEFAULT 枚举值,按 Polylang 设置的默认语言筛选实体:

{
  posts(
    filter: {
      polylangLanguagesBy: {
        predefined: DEFAULT
      }
    }
  ) {
    title
    polylangLanguage {
      code
    }
  }
 
  pages(
    filter: {
      polylangLanguagesBy: {
        predefined: DEFAULT
      }
    }
  ) {
    title
    polylangLanguage {
      code
    }
  }
 
  customPosts(
    filter: {
      polylangLanguagesBy: {
        predefined: DEFAULT
      }
      customPostTypes: "dummy-cpt"
    }
  ) {
    title
    polylangLanguage {
      code
    }
  }
}

[PRO] 自动化:将 GraphQL 响应存储到信息日志

自动化执行(包括 WP-Cron 和 Automation Rules,无论执行成功与否)的完整 GraphQL 响应会记录到文件 wp-content/gatographql/logs/info.log 中。


订阅我们的新闻通讯

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