Graphql-engine: docs: document support for st_within or st_dwithin for polygons

Created on 4 Oct 2019  路  3Comments  路  Source: hasura/graphql-engine

I need to get the following query in PostgreSQL

SELECT count(*) 
FROM geo_locations
WHERE ST_DWithin(coordinates, 'POLYGON((58.44773280389084 -19.335937500000004, 56.17002298293205 -14.765625000000002, 55.7765730186677 4.218750000000001, 54.36775852406841 9.492187500000002, 52.26815737376817 9.843750000000002, 45.089035564831036 3.5156250000000004, 42.293564192170095 8.085937500000002, 40.979898069620155 7.382812500000001, 41.244772343082076 3.5156250000000004, 52.482780222078226 -14.765625000000002, 57.89149735271034 -20.390625000000004, 60.75915950226991 -21.796875000000004, 58.44773280389084 -19.335937500000004))', 100)

or

SELECT count(*) 
FROM geo_locations
WHERE ST_DWithin(coordinates, ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[58.44773280389084,-19.335937500000004],[56.17002298293205,-14.765625000000002],[55.7765730186677,4.218750000000001],[54.36775852406841,9.492187500000002],[52.26815737376817,9.843750000000002],[45.089035564831036,3.5156250000000004],[42.293564192170095,8.085937500000002],[40.979898069620155,7.382812500000001],[41.244772343082076,3.5156250000000004],[52.482780222078226,-14.765625000000002],[57.89149735271034,-20.390625000000004],[60.75915950226991,-21.796875000000004],[58.44773280389084,-19.335937500000004]]]}'), 100)

I'm trying to do it with

query ($polygon: st_d_within_input) {
  geo_locations(where: {coordinates: {_cast: {geometry: {_st_d_within: $polygon}}}}) {
    search_key
  }
}

but it fails with

{
  "errors": [
    {
      "extensions": {
        "path": "$.variableValues.polygon",
        "code": "validation-failed"
      },
      "message": "field distance of type Float! is required, but not found"
    }
  ]
}
docs postgres

Most helpful comment

The solution is

query ($polygon: geometry!) {
  geo_locations(where: {coordinates: {_cast: {geometry: {_st_within: $polygon}}}}) {
    search_key
  }
}

and params

{
  "polygon": {
    "type": "Polygon",
    "coordinates": [[[58.44773280389084,-19.335937500000004],[56.17002298293205,-14.765625000000002],[55.7765730186677,4.218750000000001],[54.36775852406841,9.492187500000002],[52.26815737376817,9.843750000000002],[45.089035564831036,3.5156250000000004],[42.293564192170095,8.085937500000002],[40.979898069620155,7.382812500000001],[41.244772343082076,3.5156250000000004],[52.482780222078226,-14.765625000000002],[57.89149735271034,-20.390625000000004],[60.75915950226991,-21.796875000000004],[58.44773280389084,-19.335937500000004]]],
    "crs":{"type":"name","properties":{"name":"EPSG:4326"}}
  }
}

Pay attention to "crs":{"type":"name","properties":{"name":"EPSG:4326"}}

All 3 comments

The solution is

query ($polygon: geometry!) {
  geo_locations(where: {coordinates: {_cast: {geometry: {_st_within: $polygon}}}}) {
    search_key
  }
}

and params

{
  "polygon": {
    "type": "Polygon",
    "coordinates": [[[58.44773280389084,-19.335937500000004],[56.17002298293205,-14.765625000000002],[55.7765730186677,4.218750000000001],[54.36775852406841,9.492187500000002],[52.26815737376817,9.843750000000002],[45.089035564831036,3.5156250000000004],[42.293564192170095,8.085937500000002],[40.979898069620155,7.382812500000001],[41.244772343082076,3.5156250000000004],[52.482780222078226,-14.765625000000002],[57.89149735271034,-20.390625000000004],[60.75915950226991,-21.796875000000004],[58.44773280389084,-19.335937500000004]]],
    "crs":{"type":"name","properties":{"name":"EPSG:4326"}}
  }
}

Pay attention to "crs":{"type":"name","properties":{"name":"EPSG:4326"}}

@stereobooster thanx for your solution.
Where did you find that? Is it in the documentation?

Reopened to track documenting this use case

Was this page helpful?
0 / 5 - 0 ratings