Unified GraphQL API for Serlo.
You need:
- Node.js from .tool-versions installed on your system. Enable
yarn
via runningcorepack enable
.- You may use asdf for the installation.
- Docker
Now follow the upcoming instructions.
# Clone the project:
$ git clone https://github.com/serlo/api.serlo.org.git
$ cd api.serlo.org
Run yarn
to install the dependencies of all packages.
Make sure Docker is running and then run yarn start
to start Redis.
If in the /etc/hosts
file of your host you have the ::1
(IPv6) mapped to localhost
, you will additionally need
to set: --dns-result-order=ipv4first
in the NODE_OPTIONS
environment variable:
export NODE_OPTIONS=--dns-result-order=ipv4first
By default, while developing, the caching won't work. If you want to have caching, change the value CACHE_TYPE
in .env
to any other
value besides 'empty'.
To check the cache locally, run yarn cache:cli
.
Some other useful commands:
redis:empty
removes the whole cache
redis:list
lists all cached keys
yarn test
runs the unit tests (requiresyarn start:containers
beforehand)yarn pacts
runs the contract tests (requiresyarn start:containers
beforehand)yarn check:all
runs all checks (like the linter and tests) to check whether your codebase is ready to be merged into main
You can pass the name of your test as an argument. For example to only run the tests of the metadata.
yarn test -- metadata
After yarn start
, you can open the GraphQL playground via http://localhost:3000/___graphql.
Note that most queries will need a running serlo/serlo.org-database-layer dev environment.
If you need to run requests authenticated/authorized, use yarn auth
to be authenticated as user with id 1 or yarn auth <id>
to choose a specific user.
Happy coding!
Interrupt the yarn start
command to stop the dev server and run yarn down
to remove all containers.
You can use git hooks to automatically check the whole codebase before you push to the server. To configure it, run the following commands in the root directory:
echo "yarn check:all --no-uncommitted-changes" > .git/hooks/pre-push
chmod +x .git/hooks/pre-push
With git push --no-verify
you can bypass the automatic checks.
yarn build:server
builds the server (only needed for deployment)yarn format
formats all source codeyarn lint
lints all source codeyarn license
updates license headers in source filesyarn pacts
runs the contract testsyarn test
runs the unit testsyarn codegen
generates TypeScript types from GraphQL schemayarn start
spins up the development environment
Via filtering PRs by base:production
you can access the changelog of production.
We use Ory Kratos for our user management. Usually you won't need it, but if you do, run first:
yarn start:kratos
In the folder ./kratos
you find all important configuration files.
Emails sent by Kratos are going to be found at http://localhost:4436
.
For more info about it see its documentation.
First of all add nbp
and vidis
as host
sudo bash -c "echo '127.0.0.1 nbp'" >> /etc/hosts
sudo bash -c "echo '127.0.0.1 vidis'" >> /etc/hosts
why do I need it? Kratos makes a request to the url of the oauth2 provider, but since it is running inside a container, it cannot easily use the host port. These DNSs are discoverable for the kratos container, so the host can also use it.
Run yarn start:sso
.
Make sure you already run yarn start:kratos
before.
Keycloak UI is available on nbp:11111
and vidis:11112
.
Username: admin, pw: admin.
There you have to configure Serlo as a client.
Client -> Create Client
id: serlo home and root url: http://localhost:3000 redirect uri: http://localhost:4433/self-service/methods/oidc/callback/nbp // OR redirect uri: http://localhost:4433/self-service/methods/oidc/callback/vidis
Get the credentials and go to kratos/config.yml
:
selfservice:
methods:
oidc:
enabled: true
config:
providers:
- id: nbp # or vidis
provider: generic
client_id: serlo
client_secret: <put secret here>
Run the local frontend (not forgetting to change environment in its .env
to local) to test.
Hint: you may want to create some users in Keycloak in order to test.
Kratos has to be rebuilt every time you change an email template. Use the following workflow:
- Edit templates. See at
kratos/config.yml
where they are. - Run
yarn kratos:rebuild
- Test the verification or the recovery email at
localhost:4436
. Repeat the process.