Yassine Bridi
Yassine Bridi

Reputation: 923

How can i use logical operators with strapi's graphql filtering

I'm passing graphql variables like this:

variables: {
  where: {
    league: {
      name_contains: "some league name"
    },
    teams: {
      name_contains: "some teams name"
    }
  }
}

I want either the league's name or teams name or both to exist. but my current configuration is set to have both league's name and team's name to exist

the query looks like this:

const SEARCH_QUERY = gql`
  query SEARCH_QUERY($where: JSON) {
    games(where: $where) {
      id
      teams {
        id
        name
      }
      league {
        id
        name
      }
    }
  }

Upvotes: 2

Views: 1595

Answers (1)

Daniel Karski
Daniel Karski

Reputation: 346

I guess that you are looking some thing like in the howtographql documentation, I can't see on the moment some similar solution in the strapi documentation but I have some another workaround solution...

teams.name && league.name

You need just these arguments

where:{teams: {name_contains: "some league name"}, league: {name_contains: "some league name"}}

teams.name || league.name

In this case you need merge separate queries for each name parent like this:

where:{teams: {name_contains: "some league name"}}

where:{league: {name_contains: "some league name"}}

Body transform function:

const gameMap = [...teamsGames, ...leagueGames].reduce((games, game) => {
  games[game.id] = games[game.id] ? games[game.id] : { id: game.id, teams: [], league: [] };
  games[game.id].teams = [...games[game.id].teams, ...game.teams];
  games[game.id].league = [...games[game.id].league, ...game.league];

  return games;
}, {});

//flat to array
Object.keys(gameMap).map(key => gameMap[key]);

Upvotes: 1

Related Questions