Some Queries are Erroring Against the XM Edge Endpoint
Are you having a Sitecore GraphQL search query that is executing well against a Content Management (CM) GraphQL endpoint but fails to execute against the XM Edge endpoint? Is the response containing the following error?
{
"errors": [
{
"message": "Argument \"where\" has invalid value",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"search"
]
}
],
"data": {
"search": null
}
}
Cause of the GraphQL Error
This happens when there are where
clause groups with at least one clause that use a variable, and at least one clause that do not use variables. Here is an example:
query AllItemsByLanguage($language: String!) {
search(
where: {
AND: [
{ name: "_language", value: $language, operator: EQ }
{ name: "_path", value: "{11111111-1111-1111-1111-111111111111}", operator: EQ }
]
}
) {
results {
id
}
}
}
In the AND
clause group, the first clause uses a variable, and the second clause does not.
The Undocumented Rule
There seems to be an undocumented rule when variables are used in a Sitecore GraphQL search queries. If variables are used in a where
clause group, the last clause of the group should be one that uses a variable.
The solution is very simple. For each where
clause group that use variables, simply move one where
clause that uses a variable at the end of the group. Here is the fixed previous query:
query AllItemsByLanguage($language: String!) {
search(
where: {
AND: [
{ name: "_path", value: "{11111111-1111-1111-1111-111111111111}", operator: EQ }
# Move a clause that uses a variable at the last position of its clause group
{ name: "_language", value: $language, operator: EQ }
]
}
) {
results {
id
}
}
}
This query now executes both on the CM and XM Edge endpoints.
Avoiding Sitecore GraphQL Query Pitfalls
Sitecore GraphQL queries are very powerful and come with a learning curve. I hope this article saved you the lengthy trial and error process we had to go through to find that rule. Come back reading our blog for more tips and solutions.
Happy Sitecoring!