# graphql-publishing

## Prerequisites

1. Make sure you have node version >= 20, [nvm](https://github.com/nvm-sh/nvm/blob/master/README.md) and [yarn](https://yarnpkg.com/) installed.
2. We are using private npm packages from our [monorepo](https://github.com/theorchard/orchard-suite).
   That means you need a valid access token for each of them.
   Follow this guide on how to setup NPM access.
   https://www.notion.so/Javascript-Package-Management-Setup-9553d5d491c94835aa787fdf0fc4838d
3. Familiarize yourself with [GraphQL at The Orchard](https://github.com/theorchard/docs/tree/master/graphql) before contributing!

### GraphQL Playground

[https://qa-graphql-publishing.theorchard.io/graphql](https://qa-graphql-publishing.theorchard.io/graphql)

## Setup

You will need a valid SPLIT_API_KEY or the service won't start

### Running locally

#### Running outside of Docker:

1. Make sure that you have `nodejs` and `yarn` installed on your system

2. Follow [Javascript Package Setup](https://www.notion.so/JavaScript-Package-Setup-9553d5d491c94835aa787fdf0fc4838d#8ee270b1ac2a4dd589c75ea8e32def0f)

3. Copy the `.env.shadow` file to `.env`:

```
$ cp .env.shadow .env
```

If any of the values in your `.env` file contain a `$` make sure you escape it. Examples include the neo4j password.

```
NEO4J_PASSWORD=abc12\$34
```

which will be properly handled by dotenv-expand and load as `abc12$34`

4. Set `GITHUB_NPM_TOKEN` environment variable in current shell to a working token.

5. Install project dependencies

```
$ yarn install
```

6. Run local server.

```
$ yarn start
```

7. Open your web browser [http://localhost:8080/graphql](http://localhost:8080/graphql) and make some queries.

#### Running in Docker:

1. Make sure you have docker installed.

2. Log in to Docker - `aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 086679231553.dkr.ecr.us-east-1.amazonaws.com`

3. Authenticate to ECR in the AWS account containing parent images (specified in Dockerfile)

4. Replace any unset environment variables in docker-compose.yaml for the service with real values

5. Set `GITHUB_NPM_TOKEN` environment variable in current shell to a working token.

6. Run one or more services using docker compose:

```
docker compose up --build service
```

Alternatively, separate the build and run steps:

```
docker-compose build --no-cache service
docker-compose up service
```

6. Open your web browser [http://localhost:8080/graphql](http://localhost:8080/graphql) and make some queries.

### Generating Types

#### Types from Schema

This app uses `@graphql-codegen` for generating type definitions from Graphql schema files.
When modifying schema files, make sure you re-run

```
yarn graphql-codegen
```

This will produce the `generated/index.ts` files with type definitions.

#### Types from Queries for Integration Tests

The integrations tests rely on Graphql queries located in `tests/queries`. In the spirit of making correctly typed tests,
you need to generate type definitions from the queries using apollo codegen. Use this command to generate the types to `tests/definitions`.

```
yarn generate:types
```

### Integration Tests

To run the entire suite of tests, there are some steps before you can do it:

- Make sure you have installed [Docker Desktop](https://desktop.docker.com/mac/main/arm64/Docker.dmg?utm_source=docker&utm_medium=webreferral&utm_campaign=docs-driven-download-mac-arm64) and it's running on your machine
- Follow this [Notion Guide](https://www.notion.so/AWS-Access-f841b9dd815d4443a80e96a86c92cd2f) to get permissions to download and use our ECR images (command `awsume prod`)

- Run tests:

```
yarn docker:test:integration
```

- Run a single test file, for example:

```
yarn docker:test:integration --testPath=tests/specs/create-changelog-record.spec.ts
```

- To keep the containers running to debug the local neo4j database, you can use the `--keepDbAlive` flag:

```
yarn docker:test:integration --testPath=tests/specs/create-changelog-record.spec.ts --keepDbAlive
```

- You can access to the neo4j browser at `http://localhost:7474/browser/` with the following credentials:
    - Username: `neo4j`
    - Password: `neo4jPass`
