Hasura改变了游戏规则。当我开始使用它的时候还处于起步阶段,但是现在它具有多种可定制的功能,并且可以创建自定义的postgres函数和视图。检查以下我编写的函数,以使用Hasura GraphQL Engine进行多字段搜索查询
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;
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
}
}