Search code examples
reactjsgraphqlapollostrapi

GraphQL Where: greater than / less than


I am trying to figure out how to query "less than"/"greater than" using GraphQL towards strapi backend (using React/Apollo). Basically I would like the amount of bonus to be "not smaller" than $min_bonus, and "not greater" then $max_bonus...

What would be a good solution to this?

My Query:

import { gql } from "@apollo/client";


const BONUSES_QUERY = gql`

query bonuses($min_bonus, $max_bonus, $welcome_bonus: Boolean, $deposit_bonus: Boolean, $monthly_bonus: Boolean, $weekly_bonus: Boolean, $slots_bonus: Boolean, $nodeposit_bonus: Boolean ) {
    
  welcome_bonuses: bonuses(where: 
    { 
      welcome_bonus: $welcome_bonus
      
    })
  {
    ...BonusParts
  }

  deposit_bonuses: bonuses(where: { deposit_bonus: $deposit_bonus })
  {
    ...BonusParts
  }

  monthly_bonuses: bonuses(where: { monthly_bonus: $monthly_bonus })
  {
    ...BonusParts
  }

  weekly_bonuses: bonuses(where: { weekly_bonus: $weekly_bonus })
  {
    ...BonusParts
  }

  slots_bonuses: bonuses(where: { slots_bonus: $slots_bonus })
  {
    ...BonusParts
  }

  nodeposit_bonuses: bonuses(where: { nodeposit_bonus: $nodeposit_bonus })
  {
    ...BonusParts
  }

}
fragment BonusParts on Bonuses {
          name
          casino { 
            name
            aff_link
            logo { 
              url 
            }
          }
          slug
          title
          amount
          code
          min_deposit
          value
          wager
          cashable
          bonus_type {
            name
          }      
  }


`;

export default BONUSES_QUERY;

Solution

  • You say you want greater than and less than but your description indicates you want greater than or equal to and less than or equal to so my suggestion is for that. You can sub _gt for _gte and the like if you don't want to include the limit.

    Here is the relevent Strapi documentation.

    You can append _gte and _lte to fields in your where clauses like so.

    import { gql } from "@apollo/client";
    
    
    const BONUSES_QUERY = gql`
    
    query bonuses($min_bonus, $max_bonus, $welcome_bonus: Boolean, $deposit_bonus: Boolean, $monthly_bonus: Boolean, $weekly_bonus: Boolean, $slots_bonus: Boolean, $nodeposit_bonus: Boolean ) {
        
      welcome_bonuses: bonuses(where: 
        { 
          deposit_bonus_gt: $min_bonus, 
          deposit_bonus_lt: $max_bonus,
          welcome_bonus: $welcome_bonus
          
        })
      {
        ...BonusParts
      }
    
      deposit_bonuses: bonuses(where: 
        { 
          deposit_bonus_gt: $min_bonus, 
          deposit_bonus_lt: $max_bonus,  
          deposit_bonus: $deposit_bonus 
        })
      {
        ...BonusParts
      }
    
      ...