2018-07-11 07:57:03 +03:00
# Contributing
2018-10-19 19:59:18 +03:00
This guide explains how to set up the graphql-engine server for development on your
own machine and how to contribute.
2018-07-11 13:34:29 +03:00
## Pre-requisites
2020-01-18 01:07:15 +03:00
- [GHC ](https://www.haskell.org/ghc/ ) 8.6.5 and [cabal-install ](https://cabal.readthedocs.io/en/latest/ )
- There are various ways these can be installed, but [ghcup ](https://www.haskell.org/ghcup/ ) is a good choice if you’ re not sure.
2019-07-12 06:59:29 +03:00
- [Node.js ](https://nodejs.org/en/ ) (>= v8.9)
- npm >= 5.7
2019-12-07 02:58:08 +03:00
- [gsutil ](https://cloud.google.com/storage/docs/gsutil )
2019-02-14 08:16:14 +03:00
- libpq-dev
2019-07-12 06:59:29 +03:00
- python >= 3.5 with pip3
2019-09-19 15:54:40 +03:00
2019-07-12 06:59:29 +03:00
The last two prerequisites can be installed on Debian with:
2019-03-19 08:12:50 +03:00
2019-07-12 06:59:29 +03:00
$ sudo apt install libpq-dev python3 python3-pip python3-venv
2019-03-19 08:12:50 +03:00
2020-03-24 04:46:09 +03:00
Additionally, you will need a way to run a Postgres database server. The `dev.sh` script (described below) can set up a Postgres instance for you via [Docker ](https://www.docker.com ), but if you want to run it yourself, you’ ll need:
- [PostgreSQL ](https://www.postgresql.org ) >= 9.5
- [postgis ](https://postgis.net )
2019-07-12 06:59:29 +03:00
### Upgrading npm
2019-03-19 08:12:50 +03:00
2019-07-12 06:59:29 +03:00
If your npm is too old (>= 5.7 required):
2019-03-19 08:12:50 +03:00
2019-07-12 06:59:29 +03:00
$ npm install -g npm@latest # sudo may be required
2019-03-19 08:12:50 +03:00
2019-07-12 06:59:29 +03:00
or update your nodejs.
2019-03-19 08:12:50 +03:00
2018-07-11 13:34:29 +03:00
## Development workflow
2019-07-12 06:59:29 +03:00
You should fork the repo on github and then `git clone https://github.com/<your-username>/graphql-engine` .
After making your changes
2018-07-11 13:34:29 +03:00
### Compile
2019-03-19 08:12:50 +03:00
2019-07-12 06:59:29 +03:00
...console assets:
2019-03-19 08:12:50 +03:00
2019-07-12 06:59:29 +03:00
$ cd console
$ npm ci
$ npm run server-build
$ cd ..
2019-03-19 08:12:50 +03:00
2019-07-12 06:59:29 +03:00
...and the server:
2019-03-19 08:12:50 +03:00
2019-07-12 06:59:29 +03:00
$ cd server
2020-01-18 01:07:15 +03:00
$ ln -s cabal.project.dev cabal.project.local
$ cabal new-update
$ cabal new-build
2019-03-19 08:12:50 +03:00
2020-04-21 22:03:35 +03:00
To set up the project configuration to coincide with the testing scripts below, thus avoiding recompilation when testing locally, rather use `cabal.project.dev-sh.local` instead of `cabal.project.dev` :
$ ln -s cabal.project.dev-sh.local cabal.project.local
2019-12-07 02:58:08 +03:00
### Run and test via `dev.sh`
The `dev.sh` script in the top-level `scripts/` directory is a turnkey solution to build, run, and
test `graphql-engine` using a Docker container to run a Postgres database. **Docker is necessary to
use `dev.sh` .**
2019-03-19 08:12:50 +03:00
2019-12-07 02:58:08 +03:00
To use `dev.sh` , first launch a new postgres container with:
2019-03-19 08:12:50 +03:00
2019-07-12 06:59:29 +03:00
$ scripts/dev.sh postgres
2019-03-19 08:12:50 +03:00
2019-07-12 06:59:29 +03:00
Then in a new terminal launch `graphql-engine` in dev mode with:
2018-07-11 13:34:29 +03:00
2019-07-12 06:59:29 +03:00
$ scripts/dev.sh graphql-engine
2019-01-20 12:49:56 +03:00
2019-07-12 06:59:29 +03:00
The `dev.sh` will print some helpful information and logs from both services
will be printed to screen.
2019-01-20 12:49:56 +03:00
2019-07-12 06:59:29 +03:00
You can run the test suite with:
2019-01-20 12:49:56 +03:00
2019-07-12 06:59:29 +03:00
$ scripts/dev.sh test
2019-01-20 12:49:56 +03:00
2020-04-21 12:29:16 +03:00
This should run in isolation. The output format is described in the [pytest documentation ](https://docs.pytest.org/en/latest/usage.html#detailed-summary-report ). Errors and failures are indicated by `F` s and `E` s.
2019-01-20 12:49:56 +03:00
2019-12-07 02:58:08 +03:00
### Run and test manually
If you want, you can also run the server and test suite manually against a Postgres instance of your choosing.
#### Run
2020-01-18 01:07:15 +03:00
The following command can be used to build and launch a local `graphql-engine` instance:
2019-12-07 02:58:08 +03:00
```
2020-01-18 01:07:15 +03:00
cabal new-run -- exe:graphql-engine \
2019-12-07 02:58:08 +03:00
--database-url='postgres://< user > :< password > @< host > :< port > /< dbname > ' \
serve --enable-console --console-assets-dir=../console/static/dist
```
This will launch a server on port 8080, and it will serve the console assets if they were built with `npm run server-build` as mentioned above.
#### Test
`graphql-engine` has two test suites:
1. A small set of unit tests and integration tests written in Haskell.
2. An extensive set of end-to-end tests written in Python.
Both sets of tests require a running Postgres database.
##### Running the Haskell test suite
```
2020-01-18 01:07:15 +03:00
cabal new-run -- test:graphql-engine-tests \
--database-url='postgres://< user > :< password > @< host > :< port > /< dbname > '
2019-12-07 02:58:08 +03:00
```
##### Running the Python test suite
1. To run the Python tests, you’ ll need to install the necessary Python dependencies first. It is
recommended that you do this in a self-contained Python venv, which is supported by Python 3.3+
out of the box. To create one, run:
```
python3 -m venv .python-venv
```
(The second argument names a directory where the venv sandbox will be created; it can be anything
you like, but `.python-venv` is `.gitignore` d.)
With the venv created, you can enter into it in your current shell session by running:
```
source .python-venv/bin/activate
```
(Source `.python-venv/bin/activate.fish` instead if you are using `fish` as your shell.)
2. Install the necessary Python dependencies into the sandbox:
```
pip3 install -r tests-py/requirements.txt
```
3. Start an instance of `graphql-engine` for the test suite to use:
```
env EVENT_WEBHOOK_HEADER=MyEnvValue \
WEBHOOK_FROM_ENV=http://localhost:5592/ \
2020-01-18 01:07:15 +03:00
cabal new-run -- exe:graphql-engine \
2019-12-07 02:58:08 +03:00
--database-url='postgres://< user > :< password > @< host > :< port > /< dbname > ' \
serve --stringify-numeric-types
```
The environment variables are needed for a couple tests, and the `--stringify-numeric-types` option is used to avoid the need to do floating-point comparisons.
4. With the server running, run the test suite:
```
cd tests-py
pytest --hge-urls http://localhost:8080 \
--pg-urls 'postgres://< user > :< password > @< host > :< port > /< dbname > '
```
This will run all the tests, which can take a couple minutes (especially since some of the tests are slow). You can configure `pytest` to run only a subset of the tests; see [the `pytest` documentation ](https://doc.pytest.org/en/latest/usage.html ) for more details.
Some other useful points of note:
- It is recommended to use a separate Postgres database for testing, since the tests will drop and recreate the `hdb_catalog` schema, and they may fail if certain tables already exist. (It’ s also useful to be able to just drop and recreate the entire test database if it somehow gets into a bad state.)
- You can pass the `-v` or `-vv` options to `pytest` to enable more verbose output while running the tests and in test failures. You can also pass the `-l` option to display the current values of Python local variables in test failures.
2018-07-11 13:34:29 +03:00
### Create Pull Request
2020-01-18 01:07:15 +03:00
2018-07-11 13:34:29 +03:00
- Make sure your commit messages meet the [guidelines ](../CONTRIBUTING.md ).
2020-01-18 01:07:15 +03:00
- If you changed the versions of any dependencies, run `cabal new-freeze` to update the freeze file.
2018-07-11 13:34:29 +03:00
- Create a pull request from your forked repo to the main repo.
- Every pull request will automatically build and run the tests.
## Code conventions
This helps enforce a uniform style for all committers.
- Compiler warnings are turned on, make sure your code has no warnings.
- Use [hlint ](https://github.com/ndmitchell/hlint ) to make sure your code has no warnings.
- Use [stylish-haskell ](https://github.com/jaspervdj/stylish-haskell ) to format your code.