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

162 lines
3.8 KiB
YAML

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;
DO $$
BEGIN
IF PostGIS_lib_version() ~ '^3.*' THEN
CREATE EXTENSION IF NOT EXISTS postgis_raster;
END IF;
END$$;
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;
CREATE TABLE float_test(
id serial primary key,
first_int integer not null,
second_int integer not null
);
INSERT INTO float_test (first_int, second_int)
VALUES (1, 2), (3, 2), (4, 6);
CREATE FUNCTION get_sum(table_row float_test) returns float8 AS $$
SELECT table_row.first_int::float8 + table_row.second_int::float8
$$ 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
- type: track_table
args:
name: float_test
schema: public
- type: add_computed_field
args:
table: float_test
name: sum_float
definition:
function: get_sum