CREATE EXTENSION IF NOT EXISTS age;

LOAD 'age';
SET search_path = ag_catalog, "$user", public;

-- Apache AGE load_sql
CREATE OR REPLACE FUNCTION public.load_sql(query agtype) RETURNS SETOF agtype AS $$
BEGIN
    RETURN QUERY EXECUTE FORMAT($sql$
        WITH query AS (%s) 
        SELECT agtype_build_list(query) 
        FROM query
    $sql$, query::text);
END
$$ LANGUAGE plpgsql VOLATILE;


-- sample

SELECT * FROM cypher('playground', $$
    WITH public.load_sql('
        SELECT * FROM movies
    ') AS rows
    UNWIND rows AS row
    MERGE (v:MOVIE {id: row.title})
    SET v=row
    RETURN v
$$) AS (v agtype);
