graphql-engine/server/tests-py/queries/graphql_mutation/functions/schema_setup.yaml
hasura-bot fb902d4209 Support tracking SQL functions as mutations. Closes #1514 (#34)
* Support tracking SQL functions as mutations. Closes #1514

Co-authored-by: Brandon Simmons <brandon.m.simmons@gmail.com>
Co-authored-by: Brandon Simmons <brandon@hasura.io>
GITHUB_PR_NUMBER: 6160
GITHUB_PR_URL: https://github.com/hasura/graphql-engine/pull/6160

* Update docs/graphql/core/api-reference/schema-metadata-api/custom-functions.rst

Co-authored-by: Marion Schleifer <marion@hasura.io>

* Update docs/graphql/core/schema/custom-functions.rst

Co-authored-by: Marion Schleifer <marion@hasura.io>

* Update docs/graphql/core/schema/custom-functions.rst

Co-authored-by: Marion Schleifer <marion@hasura.io>

* Update docs/graphql/core/schema/custom-functions.rst

Co-authored-by: Brandon Simmons <brandon@hasura.io>
Co-authored-by: Brandon Simmons <brandon.m.simmons@gmail.com>
Co-authored-by: Marion Schleifer <marion@hasura.io>
GitOrigin-RevId: 8fd39258641ecace6e3e9930e497b1655ad35080
2020-11-18 18:05:59 +00:00

118 lines
3.2 KiB
YAML

# Test VOLATILE SQL functions exposed as top-level fields under the mutation root field
# (#1514)
type: bulk
args:
# test functions having multiple defaults
- type: run_sql
args:
sql: |
CREATE TABLE "user" (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
score INTEGER,
/* We just return the session vars as this column from our function
* to show they're passed through properly.
*
* NOTE: with the addition of function "tracking" we probably want to
* logically be defining permissions on composite types (which might
* or might not have been created implicitly in a CREATE TABLE).
*
* See: https://github.com/hasura/graphql-engine-internal/issues/502
*/
role_echo TEXT DEFAULT ''
);
# Adds a value (defaulting to 1) to users matching 'search', returning updated
# rows and echoing the hasura session vars.
- type: run_sql
args:
sql: |
CREATE FUNCTION add_to_score(hasura_session json, search text, increment integer default 1)
RETURNS SETOF "user" AS $$
UPDATE "user"
SET score = score + increment
WHERE name ilike ('%' || search || '%')
RETURNING id,
name,
score,
/* NOTE: other fields may be added to hasura_session
* depending on the flavor of test run on CI, e.g.
* x-hasura-auth-mode: webhook, so filter just x-hasura-role
*/
hasura_session->>'x-hasura-role' AS role_echo
$$ LANGUAGE sql VOLATILE;
- type: track_table
args:
name: user
schema: public
- type: track_function
version: 2
args:
function:
schema: public
name: add_to_score
configuration:
exposed_as: mutation
session_argument: hasura_session
# We'll use this to check that permissions on "user" table are applied to the
# return set of the tracked function:
- type: create_select_permission
args:
table: user
role: anonymous
permission:
filter: {}
columns:
- name
- score
# A few unimportant functions for smoke tests
- type: run_sql
args:
sql: |
CREATE FUNCTION volatile_func1()
RETURNS SETOF "user" AS $$
SELECT * FROM "user" ORDER BY id
$$ LANGUAGE sql VOLATILE;
- type: run_sql
args:
sql: |
CREATE FUNCTION stable_func1()
RETURNS SETOF "user" AS $$
SELECT * FROM "user" ORDER BY id
$$ LANGUAGE sql STABLE;
- type: run_sql
args:
sql: |
CREATE FUNCTION volatile_func2()
RETURNS SETOF "user" AS $$
SELECT * FROM "user" ORDER BY id
$$ LANGUAGE sql VOLATILE;
- type: run_sql
args:
sql: |
CREATE FUNCTION stable_func2()
RETURNS SETOF "user" AS $$
SELECT * FROM "user" ORDER BY id
$$ LANGUAGE sql STABLE;
# Infer that function should be a mutation from VOLATILE, if exposed_as
- version: 2
type: track_function
args:
function: volatile_func2
# Volatile function as query
- version: 2
type: track_function
args:
function: volatile_func1
configuration:
exposed_as: query