From c1468f8b58a27591b169d3933ef02bd874be2a82 Mon Sep 17 00:00:00 2001 From: Oliver Rice Date: Tue, 18 Oct 2022 12:24:35 -0500 Subject: [PATCH] optionally run in docker --- .ci/docker-compose.yml | 10 ++++ .dockerignore | 1 + .github/workflows/test.yml | 19 ++++++++ Cargo.toml | 2 +- docker-compose.yaml | 50 ++++++++++++++++++++ dockerfiles/db/Dockerfile | 39 ++++++++++++++++ dockerfiles/db/setup.sql | 83 +++++++++++++++++++++++++++++++++ dockerfiles/graphiql/index.html | 31 ++++++++++++ docs/contributing.md | 12 +++-- pg_graphql.control | 2 +- 10 files changed, 242 insertions(+), 7 deletions(-) create mode 100644 .ci/docker-compose.yml create mode 100644 .github/workflows/test.yml create mode 100644 docker-compose.yaml create mode 100644 dockerfiles/db/Dockerfile create mode 100644 dockerfiles/db/setup.sql create mode 100644 dockerfiles/graphiql/index.html diff --git a/.ci/docker-compose.yml b/.ci/docker-compose.yml new file mode 100644 index 00000000..3ea349e0 --- /dev/null +++ b/.ci/docker-compose.yml @@ -0,0 +1,10 @@ +version: '3' +services: + + test: + container_name: pg_graphql_test + build: + context: .. + dockerfile: ./dockerfiles/db/Dockerfile + command: + - ./bin/installcheck diff --git a/.dockerignore b/.dockerignore index 4729fc66..6c5b3487 100644 --- a/.dockerignore +++ b/.dockerignore @@ -5,6 +5,7 @@ dockerfiles/ docs/ pg_graphql.egg-info/ +target/ nix/ node_modules/ results/ diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..27744068 --- /dev/null +++ b/.github/workflows/test.yml @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 0473fd0b..5189355c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pg_graphql" -version = "0.5.2" +version = "0.5.0" edition = "2021" [lib] diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000..6a89dfc8 --- /dev/null +++ b/docker-compose.yaml @@ -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 diff --git a/dockerfiles/db/Dockerfile b/dockerfiles/db/Dockerfile new file mode 100644 index 00000000..cfe23dec --- /dev/null +++ b/dockerfiles/db/Dockerfile @@ -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 diff --git a/dockerfiles/db/setup.sql b/dockerfiles/db/setup.sql new file mode 100644 index 00000000..7c72429e --- /dev/null +++ b/dockerfiles/db/setup.sql @@ -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 + ('aardvark@x.com', now()), + ('bat@x.com', now()), + ('cat@x.com', now()), + ('dog@x.com', now()), + ('elephant@x.com', 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})'; diff --git a/dockerfiles/graphiql/index.html b/dockerfiles/graphiql/index.html new file mode 100644 index 00000000..16487b3a --- /dev/null +++ b/dockerfiles/graphiql/index.html @@ -0,0 +1,31 @@ + + + GraphiQL - pg_graphql + + + +
+ + + + + + + diff --git a/docs/contributing.md b/docs/contributing.md index 3a5e3aeb..8e516147 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -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 @@ -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 ``` @@ -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. diff --git a/pg_graphql.control b/pg_graphql.control index 12de9b52..6e0b651b 100644 --- a/pg_graphql.control +++ b/pg_graphql.control @@ -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'