I have a react app using react-apollo and graphql-tags, with the following query:
const APOLLO_QUERY = gql`
query get_resources(
$limit: Int!
$offset: Int!
$where: user_bool_exp
) {
user(limit: $limit, offset: $offset, where: $where) {
id
name
}
user_aggregate(where: $where) {
aggregate {
count
}
}
}
`;
and the way I am calling this is the following:
const { loading, error, data } = useQuery(APOLLO_QUERY, {
variables: {
limit: countPerPage,
offset: (currentPage - 1) * countPerPage,
...(whereClause && { where: whereClause }),
},
});
and this is how I am composing whereClause:
const adminClause = isAdmin => `admin: {_eq: ${isAdmin}}`;
const subClauses = [adminClause(props.isAdmin)].filter(Boolean);
const whereClause = subClauses =>
subClauses.length > 0 && `{${subClauses.join(",")}}`;
after I make the query, this is the request payload:
{
"operationName":"get_users",
"variables":{
"limit":10,
"offset":0,
"where":"{admin: {_eq: true}}"
},
"query":"query get_users($limit: Int!, $offset: Int!, $where: user_bool_exp) {\n user(limit: $limit, offset: $offset, where: $where) {\n id\n name\n __typename\n }\n user_aggregate(where: $where) {\n aggregate {\n count\n __typename\n }\n __typename\n }\n}\n"
}
and this is the error I get in the response:
{"errors":[{"extensions":{"path":"$.variableValues.where","code":"validation-failed"},"message":"expecting a JSON object"}]}
I can notice that in the payload where is a string, unlike in the Graphiql console where it is without quotes.
any idea how to properly pass the where variable in this case? I realize this might not be a hasura problem but I thought you might have ideas.
thank you.
@amine-benselim Construct the where clause as a JSON object since everything in the variables key should be JSON:
"where":{ "admin" : { "_eq": true } }"
If you came here from Google, just check that the variables you are putting into your GraphQL query are not misspelled - match name of your DB cells.