Skip to content

Commit bc2fa3f

Browse files
committed
Add README and convenience interface.
1 parent adc9044 commit bc2fa3f

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# GraphpostgresQL -- a graph interface to relational data
2+
3+
GraphpostgresQL is inspired by Facebook's [`graphql`][graphql]. By using table
4+
introspection, GraphpostgresQL is able to follow foreign keys and index into
5+
complex datatypes like `json`, `jsonb` and `hstore`.
6+
7+
[graphql]: https://www.npmjs.com/package/graphql
8+
9+
10+
# A Proof of Concept
11+
12+
GraphpostgresQL is alpha quality and has undergone neither extensive
13+
optimization nor comprehensive testing. To use it for production workloads
14+
would needlessly tempt fate.
15+
16+
17+
# Install GraphpostgresQL
18+
19+
Using `psql`, load the `graphql` schema file:
20+
21+
```sql
22+
\i graphql.sql
23+
```
24+
25+
All definitions are created under the `graphql` schema. GraphpostgresQL
26+
doesn't load any extensions or alter the `search_path`. If an older version of
27+
GraphpostgresQL is loaded, the new installation will overwrite it.
28+
29+
30+
# Using GraphpostgresQL
31+
32+
To generate a query, use `graphql.to_sql(text)`:
33+
34+
```sql
35+
SELECT graphql.to_sql($$
36+
user("f3411edc-e1d0-452a-bc19-b42c0d5a0e36") {
37+
full_name,
38+
friendship
39+
}
40+
$$);
41+
```
42+
43+
Which should result in something like:
44+
45+
```sql
46+
SELECT to_json("sub/2") AS "user"
47+
FROM "user",
48+
LATERAL (
49+
SELECT json_agg("user") AS friendship
50+
FROM "user"
51+
JOIN friendship ON (("user".id) = (friendship.second))
52+
WHERE (friendship.first)
53+
= ('f3411edc-e1d0-452a-bc19-b42c0d5a0e36'::uuid)
54+
) AS "sub/1",
55+
LATERAL (
56+
SELECT "user".full_name, "sub/1".friendship
57+
) AS "sub/2"
58+
WHERE (("user".id) = ('f3411edc-e1d0-452a-bc19-b42c0d5a0e36'::uuid))
59+
```
60+
61+
To run a query, use `graphql.run(text)` instead of `graphql.to_sql(text)`.
62+
63+
64+
# Removing GraphpostgresQL
65+
66+
It's easy to remove GraphpostgresQL:
67+
68+
```sql
69+
DROP SCHEMA IF EXISTS graphql CASCADE;
70+
```

graphql.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ $$ LANGUAGE sql STABLE STRICT;
126126

127127
/* * * * * * * * * * * * * * * Begin main program * * * * * * * * * * * * * */
128128

129+
CREATE FUNCTION run(expr text)
130+
RETURNS json AS $$
131+
DECLARE
132+
intermediate json;
133+
result json[] = ARRAY[]::json[];
134+
q text;
135+
BEGIN
136+
FOR q IN SELECT graphql.to_sql(expr) LOOP
137+
EXECUTE q INTO intermediate;
138+
CONTINUE WHEN NOT FOUND;
139+
result := result || intermediate;
140+
END LOOP;
141+
RETURN to_json(result);
142+
END
143+
$$ LANGUAGE plpgsql STABLE STRICT;
144+
129145
CREATE FUNCTION to_sql(expr text)
130146
RETURNS TABLE (query text) AS $$
131147
BEGIN

0 commit comments

Comments
 (0)