Graphql-engine: React-Apollo passing `where` clause to query fails with message: "expecting a JSON object"

Created on 18 Sep 2019  路  2Comments  路  Source: hasura/graphql-engine

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.

question

All 2 comments

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

revskill10 picture revskill10  路  3Comments

macalinao picture macalinao  路  3Comments

sachaarbonel picture sachaarbonel  路  3Comments

cpursley picture cpursley  路  3Comments

egislook picture egislook  路  3Comments