Skip to content

Commit

Permalink
optionally run in docker
Browse files Browse the repository at this point in the history
  • Loading branch information
olirice committed Oct 18, 2022
1 parent 052f93c commit c1468f8
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 7 deletions.
10 changes: 10 additions & 0 deletions .ci/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'
services:

test:
container_name: pg_graphql_test
build:
context: ..
dockerfile: ./dockerfiles/db/Dockerfile
command:
- ./bin/installcheck
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
dockerfiles/
docs/
pg_graphql.egg-info/
target/
nix/
node_modules/
results/
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Test
on:
pull_request:
push: { branches: master }

jobs:
test:
name: Run tests
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Build docker images
run: docker-compose -f .ci/docker-compose.yml build

- name: Run tests
run: docker-compose -f .ci/docker-compose.yml run test
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pg_graphql"
version = "0.5.2"
version = "0.5.0"
edition = "2021"

[lib]
Expand Down
50 changes: 50 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
version: '3'
services:

db:
container_name: pg_db
build:
context: .
dockerfile: ./dockerfiles/db/Dockerfile
volumes:
- ./dockerfiles/db/setup.sql:/docker-entrypoint-initdb.d/setup.sql
ports:
- 5406:5432
command:
- postgres
- -c
- wal_level=logical
- -c
- shared_preload_libraries=pg_stat_statements
healthcheck:
test: ["CMD-SHELL", "PGUSER=postgres", "pg_isready"]
interval: 1s
timeout: 10s
retries: 5
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: graphqldb

rest:
container_name: pg_postgrest
image: postgrest/postgrest:v10.0.0
restart: unless-stopped
ports:
- 3001:3000
environment:
PGRST_DB_URI: postgres://postgres:password@db:5432/graphqldb
PGRST_DB_SCHEMA: public
PGRST_DB_ANON_ROLE: anon
depends_on:
- db

graphiql:
container_name: pg_graphiql
image: nginx
volumes:
- ./dockerfiles/graphiql:/usr/share/nginx/html
ports:
- 4000:80
depends_on:
- rest
39 changes: 39 additions & 0 deletions dockerfiles/db/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
FROM postgres:14
RUN apt-get update

ENV build_deps ca-certificates \
git \
build-essential \
libpq-dev \
postgresql-server-dev-14 \
curl \
libreadline6-dev \
zlib1g-dev


RUN apt-get install -y --no-install-recommends $build_deps pkg-config cmake

WORKDIR /home/pg_graphql

ENV HOME=/home/pg_graphql \
PATH=/home/pg_graphql/.cargo/bin:$PATH
RUN chown postgres:postgres /home/pg_graphql
USER postgres

RUN \
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path --profile minimal --default-toolchain nightly && \
rustup --version && \
rustc --version && \
cargo --version

# PGX
RUN cargo install cargo-pgx

RUN cargo pgx init --pg14 $(which pg_config)

USER root

COPY . .
RUN cargo pgx install

USER postgres
83 changes: 83 additions & 0 deletions dockerfiles/db/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
create extension pg_graphql;

create role anon;

grant usage on schema public to anon;
alter default privileges in schema public grant all on tables to anon;
alter default privileges in schema public grant all on functions to anon;
alter default privileges in schema public grant all on sequences to anon;

grant usage on schema graphql to anon;
grant all on function graphql.resolve to anon;

alter default privileges in schema graphql grant all on tables to anon;
alter default privileges in schema graphql grant all on functions to anon;
alter default privileges in schema graphql grant all on sequences to anon;


-- GraphQL Entrypoint
create function graphql(
"operationName" text default null,
query text default null,
variables jsonb default null,
extensions jsonb default null
)
returns jsonb
language sql
as $$
select graphql.resolve(
query := query,
variables := coalesce(variables, '{}'),
"operationName" := "operationName",
extensions := extensions
);
$$;


create table account(
id serial primary key,
email varchar(255) not null,
created_at timestamp not null
);


create table blog(
id serial primary key,
owner_id integer not null references account(id) on delete cascade,
name varchar(255) not null,
description varchar(255),
created_at timestamp not null
);


create type blog_post_status as enum ('PENDING', 'RELEASED');


create table blog_post(
id uuid not null default gen_random_uuid() primary key,
blog_id integer not null references blog(id) on delete cascade,
title varchar(255) not null,
body varchar(10000),
status blog_post_status not null,
created_at timestamp not null
);


-- 5 Accounts
insert into public.account(email, created_at)
values
('[email protected]', now()),
('[email protected]', now()),
('[email protected]', now()),
('[email protected]', now()),
('[email protected]', now());

insert into blog(owner_id, name, description, created_at)
values
((select id from account where email ilike 'a%'), 'A: Blog 1', 'a desc1', now()),
((select id from account where email ilike 'a%'), 'A: Blog 2', 'a desc2', now()),
((select id from account where email ilike 'a%'), 'A: Blog 3', 'a desc3', now()),
((select id from account where email ilike 'b%'), 'B: Blog 3', 'b desc1', now());


comment on schema public is '@graphql({"inflect_names": true})';
31 changes: 31 additions & 0 deletions dockerfiles/graphiql/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<html>
<head>
<title>GraphiQL - pg_graphql</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/2.0.4/graphiql.css" rel="stylesheet" />
</head>
<body style="margin: 0;">
<div id="graphiql" style="height: 100vh;"></div>

<script
crossorigin
src="https://unpkg.com/react/umd/react.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"
></script>
<script
crossorigin
src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/2.0.4/graphiql.js"
></script>
<script>
const fetcher = GraphiQL.createFetcher({
url: 'http://localhost:3001/rpc/graphql',
});
ReactDOM.render(
React.createElement(GraphiQL, { fetcher: fetcher }),
document.getElementById('graphiql'),
);
</script>
</body>
</html>
12 changes: 7 additions & 5 deletions docs/contributing.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
pg_graphql is OSS. PR and issues are welcome.


## Development

[Nix](https://nixos.org/download.html) is required to set up the environment.
Requirements:

- rust
- cargo
- [pgx](https://github.com/tcdi/pgx)

### Testing

Expand All @@ -12,8 +15,7 @@ Tests are located in `./test/sql` with expected output in `./test/expected`
To run tests locally, execute:

```bash
# might take a few minutes downloading dependencies on the first run
$ nix-shell --run "pg_14_graphql make installcheck"
$ cargo pgx install; ./bin/installcheck
```


Expand All @@ -22,7 +24,7 @@ $ nix-shell --run "pg_14_graphql make installcheck"
To reduce the iteration cycle, you may want to launch a psql prompt with `pg_graphql` installed to experiment

```bash
nix-shell --run "pg_14_graphql psql"
cargo pgx run pg14
```

Try out the commands below to spin up a database with the extension installed & query a table using GraphQL. Experiment with aliasing field/table names and filtering on different columns.
Expand Down
2 changes: 1 addition & 1 deletion pg_graphql.control
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
comment = 'pg_graphql: GraphQL support'
default_version = '@CARGO_VERSION@'
module_pathname = '$libdir/rs_graphql'
module_pathname = '$libdir/pg_graphql'
relocatable = false
superuser = false
schema = 'graphql'

0 comments on commit c1468f8

Please sign in to comment.