Use locale with the GraphQL API
Page summary:Use the
localeargument with the GraphQL API to query, create, update, and delete documents for a specific locale.
The i18n feature adds new features to the GraphQL API:
- The
localefield is added to the GraphQL schema. - GraphQL can be used:
Fetch all documents in a specific locale
To fetch all documents for a specific locale, pass the locale argument to the query:
Example request
query {
restaurants(locale: "fr") {
documentId
name
locale
}
}
Example response
{
"data": {
"restaurants": [
{
"documentId": "a1b2c3d4e5d6f7g8h9i0jkl",
"name": "Restaurant Biscotte",
"locale": "fr"
},
{
"documentId": "m9n8o7p6q5r4s3t2u1v0wxyz",
"name": "Pizzeria Arrivederci",
"locale": "fr"
},
]
}
}
Fetch a document in a specific locale
To fetch a documents for a specific locale, pass the documentId and the locale arguments to the query:
Example query
query Restaurant($documentId: ID!, $locale: I18NLocaleCode) {
restaurant(documentId: "a1b2c3d4e5d6f7g8h9i0jkl", locale: "fr") {
documentId
name
description
locale
}
}
Example response
{
"data": {
"restaurant": {
"documentId": "lviw819d5htwvga8s3kovdij",
"name": "Restaurant Biscotte",
"description": "Bienvenue au restaurant Biscotte!",
"locale": "fr"
}
}
}