graphql-engine/server/tests-py/queries/graphql_query/computed_fields/setup.yaml

131 lines
3.1 KiB
YAML
Raw Normal View History

type: bulk
args:
- type: run_sql
args:
sql: |
CREATE TABLE author(
id SERIAL PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT
);
INSERT INTO author (first_name, last_name)
VALUES ('Roger', 'Chris'), ('Daniel', NULL);
CREATE TABLE article(
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
content TEXT,
author_id INTEGER NOT NULL REFERENCES author(id)
);
INSERT INTO article (title, content, author_id)
VALUES
('Article 1', 'Content for Article 1', 1),
('Article 2', 'Content for Article 2', 2),
('Article 3', 'Content for Article 3', 2);
CREATE FUNCTION fetch_articles(search text, author_row author)
RETURNS SETOF article AS $$
SELECT *
FROM article
WHERE
( title ilike ('%' || search || '%')
OR content ilike ('%' || search || '%')
) AND author_id = author_row.id
$$ LANGUAGE sql STABLE;
CREATE FUNCTION full_name(author)
RETURNS TEXT AS $$
DECLARE
first_name text;
last_name text;
full_name text;
BEGIN
first_name := $1.first_name;
last_name := $1.last_name;
IF last_name IS NULL THEN
full_name := first_name;
ELSE full_name := first_name || ' ' || last_name;
END IF;
RETURN full_name;
END;
$$ LANGUAGE plpgsql STABLE;
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE EXTENSION IF NOT EXISTS postgis_topology;
CREATE TABLE locations
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
location geometry(Point,4326)
);
INSERT INTO locations (location)
VALUES (ST_GeomFromText('POINT(0.0 0.0)', 4326)),
(ST_GeomFromText('POINT(1.0 0.0)', 4326))
;
CREATE FUNCTION locations_distance("from" json, locations_row locations)
RETURNS INTEGER AS $$
SELECT ST_DISTANCE( locations_row.location::geography
, ST_GeomFromGeoJSON("from"::text)::geography
)::integer
$$ LANGUAGE sql STABLE;
- type: track_table
args:
name: author
schema: public
- type: track_table
args:
name: article
schema: public
- type: add_computed_field
args:
table: author
name: get_articles
definition:
function: fetch_articles
table_argument: author_row
- type: add_computed_field
args:
table: author
name: full_name
definition:
function: full_name
- type: create_select_permission
args:
table: article
role: user
permission:
columns: '*'
filter: {}
- type: create_select_permission
args:
table: author
role: user
permission:
columns: []
filter:
id: X-Hasura-User-Id
computed_fields:
- full_name
- type: track_table
args:
name: locations
schema: public
- type: add_computed_field
args:
table: locations
name: distance
definition:
function: locations_distance
table_argument: locations_row