Skip to content

Commit 9729899

Browse files
committed
Queries with JOIN tables are a-working!
For example: SELECT * FROM graphql.to_sql('user', '"f3411edc-e1d0-452a-bc19-b42c0d5a0e36"', '{ friendship }'); to_sql --------------------------------------------------------------------------- SELECT json_agg("sub/1".friendship) + FROM """user""", + LATERAL ( + SELECT json_agg("""user""") + FROM """user""" + JOIN friendship ON (("""user""".id) = (friendship.second)) + JOIN """user""" ON ((friendship.first) = ("""user""".id)) + ) AS "sub/1" + WHERE (("""user""".id) = ('f3411edc-e1d0-452a-bc19-b42c0d5a0e36'::uuid))
1 parent f8482a3 commit 9729899

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

graphql.sql

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ DECLARE
148148
subselects text[] = ARRAY[]::text[];
149149
predicates text[] = ARRAY[]::text[];
150150
BEGIN
151-
body := substr(body, 2, length(body)-2);
151+
body := btrim(body, '{}');
152152
IF predicate IS NOT NULL THEN
153153
predicates := predicates
154154
|| graphql.format_comparison(tab,
@@ -185,8 +185,6 @@ BEGIN
185185
sub.predicate,
186186
sub.body,
187187
tab);
188-
--- TODO: Handle nested lookup into JSON, HStore, RECORD
189-
--- TODO: If col REFERENCES something, push lookup down to it
190188
cols := cols
191189
|| name(format('%I.%I', 'sub/'||cardinality(subselects), col));
192190
WHEN NOT FOUND THEN -- It might be a reference to another table
@@ -195,8 +193,9 @@ BEGIN
195193
sub.body,
196194
tab);
197195
cols := cols
198-
|| name(format('%I.%I', 'sub/'||cardinality(subselects),
199-
sub.selector));
196+
|| name(format('%I.%I',
197+
'sub/'||cardinality(subselects),
198+
sub.selector));
200199
ELSE
201200
RAISE EXCEPTION 'Not able to interpret this selector: %', sub.selector;
202201
END CASE;
@@ -214,7 +213,6 @@ BEGIN
214213
ELSE
215214
q := 'SELECT json_agg(' || column_expression || ')' || q;
216215
END IF;
217-
RAISE INFO 'Here: %', q;
218216
END;
219217
q := q || format(E'\n FROM %I', tab);
220218
FOR i IN 1..cardinality(subselects) LOOP
@@ -249,7 +247,8 @@ DECLARE
249247
lookups text[] = ARRAY[]::text[];
250248
labels text[] = ARRAY[]::text[];
251249
BEGIN
252-
SELECT col, typ FROM cols(tab) WHERE cols.col = selector INTO col, typ;
250+
SELECT cols.col, cols.typ INTO col, typ
251+
FROM graphql.cols(tab) WHERE cols.col = selector;
253252
IF NOT FOUND THEN
254253
RAISE EXCEPTION 'Did not find column % on table %', col, tab;
255254
END IF;
@@ -306,9 +305,9 @@ BEGIN
306305
WHEN NO_DATA_FOUND THEN
307306
RAISE EXCEPTION 'No REFERENCE to table % from table %', tab, selector;
308307
END;
309-
SELECT * FROM graphql.cols(selector)
310-
WHERE cols.col != ANY (SELECT array_agg(cols) FROM graphql.fk(selector))
311-
AND cols.typ NOT IN (regtype('timestamp'), regtype('timestamptz'));
308+
PERFORM * FROM graphql.cols(selector)
309+
WHERE cols.col NOT IN (SELECT unnest(cols) FROM graphql.fk(selector))
310+
AND cols.typ NOT IN (regtype('timestamp'), regtype('timestamptz'));
312311
--- If:
313312
--- * Thare are two and only two foreign keys for the other table, and
314313
--- * All the columns of the table participate in one or the other

0 commit comments

Comments
 (0)