i have a filter component that is able to filter b...
# prisma-whats-new
b
i have a filter component that is able to filter by
Red
,
Green
,
Blue
. Sometimes they just want
Red
, sometimes they might want anything that is
Red
Or
Blue
, or sometimes they might just want it all. How can I write this query so it is dynamic based on what I pass from the client? Because writing a query like this wouldn’t be too dynamic.
Copy code
{
  allPokemons(filter:{
    OR: [
      {
        colorses_some: {
          color:"Green"
        }
      },
      {
        colorses_some: {
          color:"Red"
        }
      }
    ]
  }){
    name
  }
}
a
Try something like this:
Copy code
query($colors: [String!]!){
  allPokemons(filter: { colorses_some: { color_in: $colors}})
  {
    id
  }
}
with variables:
Copy code
{
  "colors": [
    "RED",
    "BLUE"
  ]
}
b
okay let me try! looks promising
a
I updated it from
colorses_some
to
colorses_every
, otherwise I think you will always get all Pokemons
b
so this will return any that is
red
OR
blue
correct?
colorses_every
returns result that is
red
AND
blue
a
Did you test that?
b
yeah
a
Ah yes
b
when I do
colorses_every
i get 1 result that has both colors
a
You're right
colorses_some
works?
b
yup!
this is exactly what i needed, thank you so much
😎 1
was a little overwhelming trying to wrap my head around the filters for a second
a
Well, try to translate it: This says: "give me all Pokemons, that have some color (child), with the color from this list"
Glad it's working
b
@jt9001
@agartha hey agartha, I think we are experiencing what you mean with
colors_every
. We created a
Colors
schema to categorize cards by color and with
colors_some
it seems to be working fine by returning blue/black/blueblack.
Copy code
query{
  allCards(filter:{
		colors_some: {
      id_in: ["cj6yfq72z7uos0167kty5jab7", "cj6yfq1mdu66g01241w4nib9n"]
    }
  })
  {
    colors {
      color
    }
    name
  }
}
however, when we change it to
colors_every
we do get blues/blacks/blueblacks but also ones that should not return like below:
Copy code
{
        "colors": [
          {
            "color": "Black"
          }
        ],
        "name": "Yahenni, Undying Partisan"
      },
      {
        "colors": [
          {
            "color": "Black"
          }
        ],
        "name": "Renegade's Getaway"
      },
      {
        "colors": [
          {
            "color": "Blue"
          },
          {
            "color": "Black"
          }
        ],
        "name": "Tezzeret the Schemer"
      },
      {
        "colors": [
          {
            "color": "Blue"
          },
          {
            "color": "Black"
          }
        ],
        "name": "Tezzeret's Touch"
      },
      {
        "colors": [],
        "name": "Aegis Automaton"
      },
      {
        "colors": [],
        "name": "Crackdown Construct"
      },
      {
        "colors": [],
        "name": "Filigree Crawler"
      },
      {
        "colors": [],
        "name": "Implement of Combustion"
      },
      {
        "colors": [],
        "name": "Implement of Examination"
      },
      {
        "colors": [],
        "name": "Implement of Ferocity"
      },
      {
        "colors": [],
        "name": "Implement of Improvement"
      },
      {
        "colors": [],
        "name": "Implement of Malice"
      },
      {
        "colors": [],
        "name": "Metallic Mimic"
      },
      {
        "colors": [],
        "name": "Merchant's Dockhand"
      },
      {
        "colors": [],
        "name": "Inspiring Statuary"
      },
      {
        "colors": [],
        "name": "Lifecrafter's Bestiary"
      },
      {
        "colors": [],
        "name": "Irontread Crusher"
      },
      {
        "colors": [],
        "name": "Paradox Engine"
      },
      {
        "colors": [],
        "name": "Mobile Garrison"
      },
      {
        "colors": [],
        "name": "Peacewalker Colossus"
      },
      {
        "colors": [],
        "name": "Ornithopter"
      },
      {
        "colors": [],
        "name": "Pacification Array"
      },
      {
        "colors": [],
        "name": "Night Market Guard"
      },
      {
        "colors": [],
        "name": "Renegade Map"
      },
      {
        "colors": [],
        "name": "Planar Bridge"
      },
      {
        "colors": [],
        "name": "Prizefighter Construct"
      },
      {
        "colors": [],
        "name": "Scrap Trawler"
      },
      {
        "colors": [],
        "name": "Reservoir Walker"
      },
      {
        "colors": [],
        "name": "Universal Solvent"
      },
      {
        "colors": [],
        "name": "Watchful Automaton"
      },
      {
        "colors": [],
        "name": "Treasure Keeper"
      },
      {
        "colors": [],
        "name": "Untethered Express"
      },
      {
        "colors": [],
        "name": "Verdant Automaton"
      },
      {
        "colors": [],
        "name": "Spire of Industry"
      },
      {
        "colors": [],
        "name": "Walking Ballista"
      },
      {
        "colors": [],
        "name": "Welder Automaton"
      },
      {
        "colors": [],
        "name": "Tranquil Expanse"
      },
      {
        "colors": [
          {
            "color": "Blue"
          },
          {
            "color": "Black"
          }
        ],
        "name": "Tezzeret, Master of Metal"
      },
      {
        "colors": [
          {
            "color": "Blue"
          },
          {
            "color": "Black"
          }
        ],
        "name": "Tezzeret's Betrayal"
      },
      {
        "colors": [],
        "name": "Tezzeret's Simulacrum"
      },
      {
        "colors": [],
        "name": "Pendulum of Patterns"
      },
      {
        "colors": [],
        "name": "Submerged Boneyard"
      }
    ]
and those arent every card, just a few cards with empty colors
a
colors_every
apparently returns
true
is
colors == []
I think it's a bug...
colors_some
also includes results where one of the colors is NOT in your list (so,
colors_some: ["RED", "BLUE"]
also includes results that are RED and YELLOW
colors_every
makes sure that every color is in that list, so it should only return RED only, BLUE only, or RED and BLUE only, without other colors
b
thanks for the quick response @agartha
😎 1
that does make sense, colors_some is how we expect it to run so thats no problem. I think it was just the bug that we were experiencing with
colors_every
.
@jt9001
j
Thanks for the help @agartha . I want to see if we see any issues with _some like you mentioned earlier. I see we're using a follow up "key_in" term where you just have an array. Not sure if that's shorthand or actual. Have you ever had to work around this bug? We switched our schema around to get here.
a
The 'key_in' term is because I just quickly draw the example and missed that. Because it's a relation, you need it. If colors would be a string array, you could do it directly like I did. There's a limited number of use cases where you actually run into the bug. Most use cases are covered by using
_some
, except for the 'only one of these colors' query. Also, you only run into this problem when you have parents with an empty child collection. If every card has 1+ colors, the
_every
query works fine...
Workaround would be to create a color with name 'None', and add that to all cards without color. That way you don't run into the
_every
bug.
j
Awesome, we will give that a shot
a
Thinking about it (for fun), there's actually a use-case that you can't cover using this filtering: Using
colors_every: { color_in: ["blue", "black"] }
will NOT exclude cards with other colors. If you know all colors on the client, you could do:
AND: [ { colors_every: { color_in: ["blue", "black"] } }, { colors_every: { color_not_in: [ "all", "other", "colors" ] } } ]
to ONLY show cards with either blue OR black. Making it even more difficult, if you ONLY want cards with blue AND black, you need to split it like in the original filter above, with
AND: [ { colors_some: { color: "blue } }, { colors_some: { color: "black" } }, { colors_every: { color_not_in: [ "all", "other", "colors" ] } } ]
Good luck 🙂
b
@agartha i updated our Cards with empty colors to have
['None']
(None being an id that links to a Color with
color: 'None'
and the filtering works as expected. Thank you so much for your help!
😎 1