0

I am fairly new to GraphQL, using Hasura connected to a Postgres DB to expose some REST APIs. I'd like to know if there is a way to insert a queried value in a single GraphQL mutation as a default value.

mutation insertCode($code: String = "", $workcenter: String = "", $order:String = QUERY) {
  insert_code_tracing(objects: {code: $code, order:   $order, workcenter: $workcenter}) {
    returning {
      time
    }
  }
}

Where QUERY would be the following GraphQL query:

query getActiveOrderByWrkcntr($workcenter: String = ""){
  orders(order_by: {time: desc_nulls_last}, where: {workcenter: {_eq: $workcenter}}, limit: 1) {
    order
    workcenter
    time
  }
}

That fetches a single value given the work center.

Is it possible to specify a queried default value instead of a static one? I'd like to keep the DB consistent without risking making two sequential REST calls to get the value and then pass it to the mutation.

1 Answers1

-1

Default per user/session? It's hard to name it default. There is no support in graphQL itself.

You can try to save/remember it (queried value) in some cache (redis?) - an object with current workCenter and order props under userId key.

You can read it ('default' values) in mutation resolver using user id (from context, id from auth middleware).

If no arg passed (explicitely) and valid (not outdated) value exists in cache then ok, use it - otherwise throw some missing arg error.

xadm
  • 8,219
  • 3
  • 14
  • 25