Thank you for linking the post. We’ve seen that post and are utilising the solution as you can see above in our post. As mentioned before, it works perfectly in the GraphQL Explorer, but not in the code when using Apollo.
The error “Expected Name, found ‘$’” in GraphQL typically occurs when you are using a GraphQL variable and you haven’t provided a name for it. In GraphQL, variables are denoted by a dollar sign $ followed by a variable name.
Here is an example of a correct GraphQL query with a variable:
query GetItemDetails($itemId: ID!) {
item(id: $itemId) {
id
name
description
}
}
Ensure that you are adhering to the correct syntax for variables in your GraphQL query. If the error persists, kindly share the complete GraphQL query where you are encountering the issue for further assistance.
It appears that the variable $and may be causing an issue as GraphQL doesn’t directly recognize it within the query string. Could you please try the following modification and see if it resolves the problem?
We’ve found a working solution to this problem. When using operators in Apollo, they need to be wrapped by a combination of {} and "" like below:
// Array of builder content ids
const ids = ["34f3fdf343sdsad34", "f3244343434fdsf34"];
/**
* We use JSON.stringify to convert an array to a comma separated string.
* Notice we don't wrap the string with quotes here.
* We use regex to escape quotes inside the string.
**/
gql`
query {
page(query: {
id: "{$in: ${JSON.stringify(ids.map((id) => id.builder_id)).replace(/"/g, '\\"')}}"
}) {
name
createdAt
}
}
`