https://kotlinlang.org logo
#graphql
Title
# graphql
d

dave08

02/18/2021, 10:26 AM
Hi! What's the recommended way to deal with filtering by field in a search (like when using a React datatable)? Is there something in graphql-java or graphql-kotlin to simplify/standardize this?
m

mbonnin

02/18/2021, 10:28 AM
I'm not super familiar with react but it looks like a client thing not related to the API ?
d

dave08

02/18/2021, 10:31 AM
I would have thought that there is some kind of expansion of the query language to handle passing over filtering... in regular systems, they have different ways of searching each type of field. For strings, there's startsWith, contains, containsOneOf..., for dates, there's before, after, between... etc... it sounds like something that could be handled by some framework instead of each person re-inventing the wheel.
👀 1
d

Dariusz Kuc

02/18/2021, 2:22 PM
Keep in mind that GraphQL does not imply a database so there is no built-in operations that you would see in SQL.
Libraries that expose databases using GraphQL like hasura or dgraph might provide some SQL like features but that is specific to those implementations.
👍 1
d

dave08

02/18/2021, 2:31 PM
Yeah, that's understandable. But maybe there's something in Graphql-kotlin that can generate a re-usable set of query arguments?
Something like the way
DataFetchingEnvironment
or the context can be passed to `Query`s?
I guess creating a class like UserFilters and UserSorters, and passing them to the queries would be the solution, but the filter types for its fields would need to be copied manually in all projects and clutter the schema itself.
d

Dariusz Kuc

02/18/2021, 4:21 PM
as you mentioned - you could reuse same input objects but you would have to explicitly specify it for each function
that being said schema should be explicit so IMHO having that extra hidden functionality (like those sort/filters) that is not defined in the schema is problematic
d

dave08

02/18/2021, 4:25 PM
True. I was wondering if it would fit the bill of a custom type, or directive?
I'm a bit new to all this, so I don't really know how people implement this. And it seems like each backend has it's own additions...
d

Dariusz Kuc

02/18/2021, 4:27 PM
i'd imagine that should be some custom type
e.g. consider something like
Date
-> how do you represent it in the graph? • is it a formatted string? (or should there be multiple formats?) • is it a long? • do you deconstruct it to separate fields?
creating new function/resolver is always cheap -> it is backwards compatible change and clients have to opt-in to use it
d

dave08

02/18/2021, 4:33 PM
You mean a new type, but a new scalar or directive would not be recommended?
d

Dariusz Kuc

02/18/2021, 4:35 PM
in general i'd stay away from custom scalars as clients need to know how to parse them (recently there was added functionality to provide some additional info to the scalar definition)
👍🏼 1
so even if your server defines custom
Date
scalar that serializes it as X, all your clients have to know how to deserialize X into proper type (in general it defaults to String)
or even simpler example Java
Long
-> you cannot represent it as Javascript
Number
so it is a custom scalar
*there are some libs that attempt to define some common custom scalars
d

dave08

02/18/2021, 4:49 PM
Thanks for the explanation 👌🏼!
9 Views