I have a graphql query that returns a list of pullup bar
objects.
Query:
query PullUpBarsByDate($userID: String) {
PullUpBarsByDate(
type: "PullUpBar"
sortDirection: DESC
) {
items {
id
lat
lng
userID
}
}
}
Returns something like this:
[
{
id: "rf6zrd1prm",
lat: 63.50235889533083,
lng: 28.426290788320724,
},
{
id: "y80ymnlgzi7",
lat: 62.5654380797649,
lng: 17.338090077030582,
},
{
id: "ufpgut6wwh",
lat: 56.85363369278235,
lng: 30.675813028973685,
},
{
id: "p80r9j2li8a",
lat: 55.274815489484716,
lng: 24.094777714658456,
},
{
id: "ydnlsd1vn8c",
lat: 50.61633534684824,
lng: 26.500618762197323,
}
]
I’m wondering if it’s possible to order the returned list of items based on how far this would be from my location, given that my location is known on the application side. Something like this formula would be perfect: d=√((x2 – x1)² + (y2 – y1)²)
1 Answer
GraphQL does not include native filtering/sorting capabilities.
This should be done in the resolver for the PullUpBarsByDate
query, i.e. in the back end. If your data is coming from a database with geo capabilities (ex postgres/PostGIS) then you could do the sorting directly in the database query. Otherwise you could sort in the resolver code itself.
Note that given the range of lat-long data you have a flat earth model will not give you very accurate distances.
3
-
Thanks, well I'm using AWS Amplify API so not sure if it supports postgress/postgis, but will try to find out. And yeah I didn't think about long-lat distances, but does it matter for ordering? The distance will be inaccurate but I only care about relative distance for ordering purposes
– Sebastian Meckovski12 hours ago
-
You will have cases where the order of distances will be off as well as the absolute value since the errors will be heading dependent.
– Michel Floyd2 hours ago
-
Amplify can do geo queries. This post might help: gerard-sans.medium.com/…
– Michel Floyd38 mins ago