-2

I have Table in postgresql with two columns id(int) and data(jsonb)

id  | data
---------
int | jsonb

data has undefined format for example

{ id: 0, name: "Sergey", age: 23} or {vehicle: "motorcycle", max_speed: 250, brand: "honda"}

I need to write qraphQl query where response contains data list where find key word in any jsonb fields for example:

query {
  declarations(where: {data: {_ilike: "Sergey"}}) {
    id
    data
  }
}

But at this case I have error: "message": "field '_ilike' not found in type: 'jsonb_comparison_exp'"

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Which framework do you use for graphql api implementation? Some frameworks provide `contains` operation: `where: {data: {contains: "Sergey"}}` – Eugene Jul 10 '23 at 08:59
  • @Eugene I use hasura. But execute query from C++ code – Евгений Дружинин Jul 10 '23 at 09:14
  • 1
    Then check `_contains` operation: `where: {data: {_contains: "Sergey"}}`. I got it from the docs: https://hasura.io/docs/latest/queries/postgres/query-filters/#jsonb-operators-_contains-_has_key-etc – Eugene Jul 10 '23 at 09:19
  • Unfortunately it does not work because $jsonFilter: jsonb must be jsonb, but I need to use string. And in your example you exactly know key, but in my case I can not know about key – Евгений Дружинин Jul 10 '23 at 10:06

1 Answers1

0

I need to use user's function in postgresql.

  1. Create function

    CREATE OR REPLACE FUNCTION public.new_function1() RETURNS SETOF declarations LANGUAGE sql STABLE AS $function$ select * from declarations where data @? '$.** ? (@ == "Sergey")'; $function$

  2. Track this function into data base

  3. Use this function in graphQl

    query { new_function1() { data } }