Graphql-engine: "Expected value of type geography" when inserting or updading point

Created on 5 May 2020  ·  3Comments  ·  Source: hasura/graphql-engine

Hi Team,

I've created a stores tables with location column as Geography(Point) type. I've followed the schema from this doc.

CREATE TABLE public.stores
(
    store_id uuid NOT NULL DEFAULT gen_random_uuid(),
    created_at timestamp with time zone NOT NULL DEFAULT now(),
    location geography(Point,4326),
    CONSTRAINT stores_pkey PRIMARY KEY (store_id)
)

when trying to insert via the console, I get an error "Expected value of type geography" for the location property

mutation MyMutation($objects: [stores_insert_input!]!) {
  insert_stores(objects: $objects) {
    returning{
           store_id
           location           
         }
  }
}
{ "objects": [
       {
         "location": { "type": "Point", "coordinates":[43.75049, 11.03207] }
      }]
}

Also this following error:

{
  "errors": [
    {
      "extensions": {
        "internal": {
          "statement": "WITH \"public_stores__mutation_result_alias\" AS (INSERT INTO \"public\".\"stores\" ( \"store_name\", \"store_id\", \"location\", \"store_code\", \"latitude\", \"store_info\", \"updated_at\", \"is_retail\", \"created_at\", \"store_brand\", \"longitude\", \"is_active\" ) VALUES (DEFAULT, DEFAULT, ST_GeomFromGeoJSON($1 ), $2, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT)  RETURNING * , CASE WHEN 'true' THEN NULL ELSE \"hdb_catalog\".\"check_violation\"('insert check constraint failed')  END ) SELECT  json_build_object('returning', (SELECT  coalesce(json_agg(\"root\" ), '[]' ) AS \"root\" FROM  (SELECT  row_to_json((SELECT  \"_1_e\"  FROM  (SELECT  \"_0_root.base\".\"store_id\" AS \"store_id\", (ST_AsGeoJSON(\"_0_root.base\".\"location\", 15, 4 ))::json AS \"location\"       ) AS \"_1_e\"      ) ) AS \"root\" FROM  (SELECT  *  FROM \"public_stores__mutation_result_alias\" WHERE ('true')     ) AS \"_0_root.base\"      ) AS \"_2_root\"      ) )        ",
          "prepared": true,
          "error": {
            "exec_status": "FatalError",
            "hint": null,
            "message": "You need JSON-C for ST_GeomFromGeoJSON",
            "status_code": "XX000",
            "description": null
          },
          "arguments": [
            "(Oid 25,Just (\"{\\\"coordinates\\\":[43.75049,11.03207],\\\"type\\\":\\\"Point\\\"}\",Binary))",
            "(Oid 25,Just (\"CD01\",Binary))"
          ]
        },
        "path": "$.selectionSet.insert_stores.args.objects",
        "code": "unexpected"
      },
      "message": "postgres query error"
    }
  ]
}

I've also executed the following SQL commands in the SQL tab on the console

CREATE EXTENSION IF NOT EXISTS postgis;
CREATE EXTENSION IF NOT EXISTS postgis_topology;
question

Most helpful comment

Looking at the POSTGIS docs the error You need JSON-C for ST_GeomFromGeoJSON means that you will need to install JSON-C support in the database machine.

If you do not have JSON-C enabled, support you will get an error notice instead of seeing an output. To enable JSON-C, run configure --with-jsondir=/path/to/json-c. See Section 2.5.1, “Configuration” for details.

https://postgis.net/docs/ST_GeomFromGeoJSON.html

All 3 comments

Hi wineapp,

I just followed the same steps to reproduce in Hasura v1.2.1 and it worked for me.

Checking the error I can see the structure of the table is not the same as the one to reproduce. Also the error message says "You need JSON-C for ST_GeomFromGeoJSON", for me this says that the location field type is Geometry and not Geography.

If your field is Geometry you need to pass the SRID and the type of coordinate in the JSON object something like:

{ "objects": [
       {
         "location": { type: "Point",
                  coordinates: [43.75049,11.03207],
                  crs: { type: "name", properties: { name: "EPSG:4326" } }
                }
      }]
}

Looking at the POSTGIS docs the error You need JSON-C for ST_GeomFromGeoJSON means that you will need to install JSON-C support in the database machine.

If you do not have JSON-C enabled, support you will get an error notice instead of seeing an output. To enable JSON-C, run configure --with-jsondir=/path/to/json-c. See Section 2.5.1, “Configuration” for details.

https://postgis.net/docs/ST_GeomFromGeoJSON.html

I'm closing this. If there any more questions or anything to add, feel free to re-open this issue.

Was this page helpful?
0 / 5 - 0 ratings