Skip to content
This repository was archived by the owner on Aug 16, 2023. It is now read-only.

Example on how to use your own data with graphqlize #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ The actual implementation of these steps will vary based on which language (Java
- [Ring](https://www.graphqlize.org/docs/getting_started/clojure/ring)
- [Vanilla Clojure](https://www.graphqlize.org/docs/getting_started/clojure/vanilla)

### Getting started with Docker

Some examples include docker and docker-compose files to get you started faster.

They will allow you to play around with the API generated by graphqlize over a sample database or your own.

![graphqlize-docker-demo](graphqlize-boot-docker.gif)
41 changes: 40 additions & 1 deletion java/spring-boot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,43 @@ Important: Since the database takes some time to initialize the first time, you

# Once you are done stop docker compose and delete the database volumes
docker-compose down
```
```

## Build an API on top of your own data

The Docker setup uses the sakila sample databse, however you can use your own very easily. This way you can explore very fast how grapqhlize will look on top of your existing database structure.

Please use this feature and contribute with feedback. If you are a developer and you find the project useful but in need of improvement approach the community for code contributions.

The steps to use your date are as follows:

* Export your data from your existing databse or take a copy from backup.
* Copy your data to `docker-entrypoint-initdb.d`. We are using the import features provided by the docker image. See image docs for help.
* Update the databse configureation in `config/docker-application.properties` to match your database name, etc
* Start your containers `docker-compose up -d && docker-compose logs -f`
* Explore your Graphqlize API

The docker images for both PostgreSQL and MySQL/MariaDB offer ways to load data before the container is started.
We are going to use that feature to load another database instead of sakiladb.

**Note**: Database initialization happens only once during the initial docker start. You may want to remove your volumes with `docker-compose down -v` to force initialization.

### Using your own data with PostgreSQL

For PostgreSQL we are going to use the functionality described in the [Initialization scripts](https://hub.docker.com/_/postgres) section of the image documentation.

> If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary).
> After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files, run any executable *.sh scripts, and source any non-executable *.sh scripts found in that directory to do further initialization before starting the service.

If you are on a Linux system you can run the following commands:

```sh
# Export your data with pg_dump or just take it from your backups.
sudo -u postgres pg_dump my_db | gzip > my_db.sql.gz
# Copy your data to docker-entrypoint-initdb.d
# Update config/docker-application.properties with your database name and other credentials
# Start the container
docker-compose up -d && docker-compose logs -f
```

In my personal setup I took a database dump generated by `autopostgresqlbackup`. I had to customize postgresql image locale and also add an initialization script to create the roles that my database dump was expecting.
4 changes: 2 additions & 2 deletions java/spring-boot/config/docker-application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
logging.level.root=INFO
#spring.datasource.url=jdbc:mysql://sakiladb:3306/sakila
#spring.datasource.url=jdbc:mysql://db:3306/sakila
#spring.datasource.username=sakila
#spring.datasource.password=p_ssW0rd
spring.datasource.url=jdbc:postgresql://sakiladb:5432/sakila
spring.datasource.url=jdbc:postgresql://db:5432/sakila
spring.datasource.username=sakila
spring.datasource.password=p_ssW0rd
15 changes: 11 additions & 4 deletions java/spring-boot/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,31 @@ volumes:
services:
graphqlize:
build: .
command: bash -c 'while !</dev/tcp/sakiladb/5432; do sleep 1; done; /app/bin/graphqlize-boot '
command: bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; /app/bin/graphqlize-boot '
deploy:
restart_policy:
condition: on_failure
delay: 10s
max_attempts: 5
window: 120s
volumes:
# The configuration graphqlize will use for connecting to the database
- ./config/docker-application.properties:/app/config/application.properties
depends_on:
- sakiladb
- db
ports:
- "8080:8080"
networks:
- graphqlize
sakiladb:
image: sakiladb/postgres:12
db:
# image: sakiladb/postgres:11
# Use this image if you want to load your own data instead of sakila sample db
image: postgres-ro
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
volumes:
- db-data:/var/lib/postgresql/data
# Place your SQL data here to be loaded during first initialization
- ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
networks:
- graphqlize
Empty file.
2 changes: 1 addition & 1 deletion java/vanilla/config/docker-database.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ dataSource.user=sakila
dataSource.password=p_ssW0rd
dataSource.databaseName=sakila
dataSource.portNumber=5432
dataSource.serverName=sakiladb
dataSource.serverName=db
7 changes: 5 additions & 2 deletions java/vanilla/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ services:
max_attempts: 5
window: 120s
volumes:
# The configuration graphqlize will use for connecting to the database
- ./config/docker-database.properties:/app/config/database.properties
depends_on:
- sakiladb
- db
ports:
- "8080:80"
networks:
- graphqlize
sakiladb:
db:
image: sakiladb/postgres:12
# Use this image if you want to load your own data instead of sakila sample db
# image: postgres:12
volumes:
- db-data:/var/lib/postgresql/data
networks:
Expand Down