Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL filtering by data

I have a following query:

query MyQuery($today: Date) {
        allMovies(
            filter: {
              info: { endDate: { gte: $today }, startDate: { lte: $today } }
            }
          ) {
        edges {
          node {
            info {
             startDate
             endDate
            }
          }
        }
}

I'm using ReactJS with GatsbyJS, can anyone explain me how to insert variable into graphql? I don't really understand how to pass and create $today to this query. Or maybe it is somehow possible to insert today date in proper format.

All dates are YYYY-MM-DD

In mysql I could insert variable directly/ there was a function CURDATE() :(

like image 663
r3vis3d Avatar asked May 19 '26 01:05

r3vis3d


1 Answers

You have to pass the variables as a separate object in your call to the API. A example from graphql.org:

var dice = 3;
var sides = 6;
var query = `
    query RollDice($dice: Int!, $sides: Int) {
       rollDice(numDice: $dice, numSides: $sides)
}`;

fetch('/graphql', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
  },
  body: JSON.stringify({
    query,
    variables: { dice, sides },
  })
}).then(r => r.json())
  .then(data => console.log('data returned:', data));
like image 174
blueheart Avatar answered May 21 '26 10:05

blueheart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!