在 GraphQL API 中使用 locale {#graphql}
页面摘要: 使用 GraphQL API 的
locale参数,针对特定区域(locale)查询、创建、更新与删除文档。
国际化(i18n)功能为 GraphQL API 新增了以下特性:
- GraphQL schema 中新增了
locale字段。 - GraphQL 可用于:
获取特定区域中的所有文档 {#graphql-fetch-all}
要获取特定区域中的所有文档 ,可向查询传入 locale 参数:
请求
query {
restaurants(locale: "fr") {
documentId
name
locale
}
}
响应
{
"data": {
"restaurants": [
{
"documentId": "a1b2c3d4e5d6f7g8h9i0jkl",
"name": "Restaurant Biscotte",
"locale": "fr"
},
{
"documentId": "m9n8o7p6q5r4s3t2u1v0wxyz",
"name": "Pizzeria Arrivederci",
"locale": "fr"
},
]
}
}
获取特定区域中的某个文档 {#graphql-fetch}
要获取特定区域中的某个文档 ,可向查询传入 documentId 与 locale 参数:
请求:示例查询
query Restaurant($documentId: ID!, $locale: I18NLocaleCode) {
restaurant(documentId: "a1b2c3d4e5d6f7g8h9i0jkl", locale: "fr") {
documentId
name
description
locale
}
}
响应
{
"data": {
"restaurant": {
"documentId": "lviw819d5htwvga8s3kovdij",
"name": "Restaurant Biscotte",
"description": "Bienvenue au restaurant Biscotte!",
"locale": "fr"
}
}
}
创建新的本地化文档 {#graphql-create}
可传入 locale 字段,以针对特定区域创建本地化文档 (有关 GraphQL 变更的更多信息,请参阅 GraphQL API 文档)。
mutation CreateRestaurant($data: RestaurantInput!, $locale: I18NLocaleCode) {
createRestaurant(
data: {
name: "Brasserie Bonjour",
description: "Description in French goes here"
},
locale: "fr"
) {
documentId
name
description
locale
}
更新特定区域中的文档 {#graphql-update}
可在变更中传入 locale 参数,以更新某给定区域的文档 (有关 GraphQL 变更的更多信息,请参阅 GraphQL API 文档)。
mutation UpdateRestaurant($documentId: ID!, $data: RestaurantInput!, $locale: I18NLocaleCode) {
updateRestaurant(
documentId: "a1b2c3d4e5d6f7g8h9i0jkl"
data: {
description: "New description in French"
},
locale: "fr"
) {
documentId
name
description
locale
}
删除文档的某个区域 {#graphql-delete}
在变更中传入 locale 参数,以删除文档的某个特定本地化(localization):
mutation DeleteRestaurant($documentId: ID!, $locale: I18NLocaleCode) {
deleteRestaurant(documentId: "xzmzdo4k0z73t9i68a7yx2kk", locale: "fr") {
documentId
}
}