- Print
- DarkLight
What else should I know about GraphQL?
filtering within the results, using aliases, changing the language of a field and use pagination and facets.
Filter information
Filter criterea on the top level define which objects are returned by the query. For example, the productsearch query returns only the products which match the filter criteria in the round brackets.
You can also define filters which take effect within each object that is returned. In this example, only properties —UniversalTypes (learn more about UniversalTypes here) — with the codes MT01-MPID and MT01-GTIN are returned.
Here is a question you might want to query an answer to:
What are the most-used UniversalTypes?
query q1 {
productsearch(query: "gkfi") {
content {
productDetails {
descriptions {
descriptionShort
}
}
features {
universalTypes(filter: ["MT01-MPID", "MT01-GTIN"]) {
name
code
value
}
}
}
}
}
Show values in another language
You can request UniversalType names and descriptions in a specific language. Add the code for the language in curly brackets. Currently supported "de" and "en".
query q1 {
productsearch(query: "gkfi") {
content {
productDetails {
descriptions {
descriptionShort
}
}
features {
universalTypes {
name(language: "en")
code
value
unit {
symbol
}
}
}
}
}
}
Use aliases
You can choose your own names for fields; just use aliases. Names for fields must be unique within one block. In this example, we request the name of UniversalType in two languages and use aliases to give the fields different names.
query q1 {
productsearch(query: "gkfi") {
content {
productDetails {
descriptions {
descriptionShort
}
}
features {
universalTypes {
name_en:name(language: "en")
name_de:name(language: "de")
code
value
unit {
symbol
}
}
}
}
}
}
In this example, we use aliases to name UniversalTypes in the response.
query q1 {
productsearch(query: "gkfi") {
content {
productDetails {
descriptions {
descriptionShort
}
}
features {
mpid: universalTypes(filter: "MT01-MPID") {
value
}
gtin: universalTypes(filter: "MT01-GTIN") {
value
}
}
}
}
}
Pagination
In the top part of the query we specify the page and the number of elements per page (size). You can request the total number of elements returned from the query and the boolean hasNextPage in the pageInfo section of a query.
query q1 {
productsearch(query: "gkfi" page: {page: 0 size: 1}) {
content {
productDetails {
descriptions {
descriptionShort
}
}
}
pageInfo {
hasNextPage
totalCount
}
}
}
Facets
We use facets to generate filters in the frontend. In this example, the results are grouped by UniversalType "ST02-MCOM" — name of the manufacturer — and counted.
query q1 {
productsearch(query: "gkfi") {
facets {
universalTypes(filter: "ST02-MCOM") {
manufacturer: value
count
}
}
}
}
Result:
{
"data": {
"productsearch": {
"facets": {
"universalTypes": [
{
"manufacturer": "Manufacturer A",
"count": 179
},
{
"manufacturer": "Manufacturer B",
"count": 22
}
]
}
}
}
}