2

Hasura 是游戏规则的改变者。当我开始使用它时,它还处于开发的初级阶段,但现在它具有多个可自定义的功能,并且允许创建自定义 postgres 函数和视图。检查我编写的以下函数以使用 Hasura GraphQL 引擎进行多字段搜索查询

  1. 我已经制作了一个自定义 postgres 配置文件搜索功能,如下所示,此查询还允许搜索元素数组。
CREATE 
OR 
replace FUNCTION search_profiles(
    _country text default null,
    _gender text default null,
    _marital_status text default null,
    _religion text[] default null,
    _age_start integer default null, 
    _age_end integer default null
) 
returns setof profile AS $$
SELECT * 
FROM   profile 
WHERE  ((extract( year FROM age(CURRENT_TIMESTAMP, dob) ) >= _age_start 
AND    extract( year FROM age(CURRENT_TIMESTAMP, dob) ) <= _age_end) OR _age_start IS NULL)
AND    (living_country LIKE _country OR _country IS NULL)
AND    (gender = _gender OR _gender IS NULL)
AND    (marital_status LIKE _marital_status OR _marital_status IS NULL)
AND    (religion IN  (select unnest(_religion)) OR _religion IS NULL)
$$ language sql stable;
  1. graphql 查询必须是这样的
query searchProfileQuery {
  search_profiles(args: {
    _country: "United Kingdom",
    _gender: "male",
    _marital_status: "Un Married",
    _religion: "{hindu, muslim}",
    _age_start: 20, 
    _age_end: 29
  }, order_by: {created_at: asc}) {
    id
    income_currency
    last_name
    living_city
    living_country
    living_state
    marital_status
    mother_tongue
    nationality
    occupation
    religion
  }
}
4

0 回答 0