graphql-engine/server/tests-py/queries/v1/metadata/setup.yaml
Auke Booij ee7c7b1672
server: allow computed fields to have access to Hasura's session variables (fix #3846) (#4486)
* Allow computed fields to have access to Hasura's session variables

* Inform about session args for computed fields in changelog and docs

* Add tests for session arguments for computed fields (and the respective errors)

Co-authored-by: Tirumarai Selvan <tiru@hasura.io>
Co-authored-by: Marion Schleifer <marion@hasura.io>
Co-authored-by: Rakesh Emmadi <12475069+rakeshkky@users.noreply.github.com>
2020-04-27 17:07:03 +02:00

153 lines
3.1 KiB
YAML

type: bulk
args:
#Author table
- type: run_sql
args:
sql: |
create table author(
id serial primary key,
name text unique
);
- type: track_table
args:
schema: public
name: author
#Article table
- type: run_sql
args:
sql: |
CREATE TABLE article (
id SERIAL PRIMARY KEY,
title TEXT,
content TEXT,
author_id INTEGER REFERENCES author(id),
is_published BOOLEAN,
published_on TIMESTAMP
)
- type: track_table
args:
schema: public
name: article
#Object relationship
- type: create_object_relationship
args:
table: article
name: author
using:
foreign_key_constraint_on: author_id
#Array relationship
- type: create_array_relationship
args:
table: author
name: articles
using:
foreign_key_constraint_on:
table: article
column: author_id
comment: List all articles of the author
#Insert Author table data
- type: insert
args:
table: author
objects:
- name: Author 1
- name: Author 2
- type: run_sql
args:
sql: |
insert into article (title,content,author_id,is_published)
values
(
'Article 1',
'Sample article content 1',
1,
false
),
(
'Article 2',
'Sample article content 2',
1,
true
),
(
'Article 3',
'Sample article content 3',
2,
true
)
#Create search article function
- type: run_sql
args:
sql: |
create function search_articles(search text)
returns setof article as $$
select *
from article
where
title ilike ('%' || search || '%') or
content ilike ('%' || search || '%')
$$ language sql stable;
- type: track_function
args:
name: search_articles
schema: public
#Create get_articles computed field
- type: run_sql
args:
sql: |
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;
- type: add_computed_field
args:
table: author
name: get_articles
definition:
function: fetch_articles
table_argument: author_row
#Computed field with session argument
- type: run_sql
args:
sql: |
CREATE FUNCTION test_session(author_row author, key text, session json)
RETURNS TEXT AS $$
SELECT $3->>$2
$$ LANGUAGE sql STABLE;
- type: add_computed_field
args:
table: author
name: test_session
definition:
function: test_session
session_argument: session
- type: run_sql
args:
sql: |
CREATE TABLE text_result(
result text
);
CREATE FUNCTION get_session_var(hasura_session json, session_var text)
RETURNS SETOF text_result AS $$
SELECT q.* FROM (VALUES (hasura_session ->> session_var)) q
$$ LANGUAGE sql STABLE;