hasura / graphql-engine

Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
https://hasura.io
Apache License 2.0
31.18k stars 2.77k forks source link

Support for Composite types #1041

Open sachaarbonel opened 6 years ago

sachaarbonel commented 6 years ago

Hi guys, I created a table with the Hasura sql console with a composite type like in the Postgres docs. I can do queries like this

query{on_hand{count item}}

And I get the response as expected:

{
  "data": {
    "on_hand": [
      {
        "count": 1000,
        "item": {
          "name": "fuzzy dice",
          "supplier_id": 42,
          "price": 1.99
        }
      },
      {
        "count": 500,
        "item": {
          "name": "fuzzy dice",
          "supplier_id": 41,
          "price": 9.99
        }
      }
    ]
  }
}

But when I try to do a mutation like this it doesn't work

mutation {
  insert_on_hand(objects: [{count: 10, item: {name: "fuzzy dice", supplier_id: 42, price: 1.99}}]) {
    returning {
      count
      item
    }
  }
}

I get this error:

{
  "errors": [
    {
      "path": "$.selectionSet.insert_on_hand.args.objects[0].item",
      "error": "unexpected object for a scalar",
      "code": "validation-failed"
    }
  ]
}

Subscriptions work by making the insert in the hasura sql console. So composite types or "user defined" types work out of the box but is it possible to add official support for it ? Thank's for the great work on the engine guys :+1:

EDIT: I tried that way too

mutation {
  insert_on_hand(objects: [{count: 10, item: {data: {name: "fuzzy dice", supplier_id: 42, price: 1.99}}}]) {
    returning {
      count
      item
    }
  }
}
0x777 commented 6 years ago

@Sach97 While we don't explicitly support row types, there is an escape hatch (for all types that are not known to graphql-engine) using Postgres literals. In your case, you can do this:

mutation {
  insert_on_hand(objects: [{count: 10, item: "('fuzzy dice',42,1.99)"}]) {
    returning {
      count
      item
    }
  }
}
sachaarbonel commented 6 years ago

Thank's a lot for your response ! It will do the job

jvice152 commented 5 years ago

@0x777 , couldnt you hypothetically scrape custom postgres types, and support them?

this would be a big boost in performance, as my schema is littered with one-to-one relationships and id love to consolidate them into row types to bring my tracked tables/relationships down