How change query value (where : { }) with Apollo GraphQL

1.3k views Asked by At

with graphQL we can fetch data with "where"

query{
  activities(where: {date: "2020-08-01"}){
    date
    collect
  }
}

How change the date value to params ?

I try this :

let date = "2020-01-08"
const { loading, error, data } = useQuery(GET_ACTIVITY, {
    variables: {
      input: {
        where: { date: date }
      }
    }
  });
1

There are 1 answers

3
Fraction On

First the schema definition from your back-end must be something like this:

type Activity {
  date: String
  collect: String
  ...
}

type Query {
  activities(date: String): [Activity]
  ...
}

then in the front-end create the query using the gql function from @apollo/client:

import { gql, useQuery } from '@apollo/client';

...

const GET_ACTIVITY = gql`
  query Activities($date: String!) {
    activities(date: $date){
      date
      collect
    }
  }
`;

And finally call it like this:

let date = "2020-01-08";
const { loading, error, data } = useQuery(GET_ACTIVITY, {
  variables: {
    date
  }
});

More about queries: https://www.apollographql.com/docs/react/data/queries