Skip to content

Commit 3261a7b

Browse files
committed
Testing and comments for run() (fix for #5).
1 parent 4791c19 commit 3261a7b

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

demo.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BEGIN
3333
RAISE INFO E'GraphQL to parse:\n%\n', graphql_q;
3434
SELECT * INTO STRICT sql_q FROM graphql.to_sql(graphql_q);
3535
RAISE INFO E'SQL that will be run:\n%\n', sql_q;
36-
EXECUTE sql_q INTO STRICT result;
36+
result := graphql.run(graphql_q);
3737
RAISE INFO E'Result:\n%\n', result;
3838
END
3939
$$;

graphql.sql

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,26 @@ RETURNS json AS $$
131131
DECLARE
132132
intermediate json;
133133
result json[] = ARRAY[]::json[];
134+
n integer = 0;
134135
q text;
135136
BEGIN
136137
FOR q IN SELECT graphql.to_sql(expr) LOOP
137-
EXECUTE q INTO intermediate;
138-
CONTINUE WHEN NOT FOUND;
138+
n := n + 1;
139+
BEGIN
140+
EXECUTE q INTO STRICT intermediate;
141+
EXCEPTION
142+
WHEN NO_DATA_FOUND THEN CONTINUE;
143+
END;
139144
result := result || intermediate;
140145
END LOOP;
141-
RETURN to_json(result);
146+
--- Maybe there is a better way to approach query cardinality? For example,
147+
--- by insisting that there be a root query (perhaps with no predicate?) or
148+
--- returning TABLE (result json).
149+
IF n = 1 THEN
150+
RETURN result[1];
151+
ELSE
152+
RETURN to_json(result);
153+
END IF;
142154
END
143155
$$ LANGUAGE plpgsql STABLE STRICT;
144156

0 commit comments

Comments
 (0)