# graphql-neighbouring-rights

## Prerequisites

1. Make sure you have node version >= 24.12.0 [nvm](https://github.com/nvm-sh/nvm/blob/master/README.md) and [yarn](https://yarnpkg.com/) installed. Run

```
nvm use
```

2. We are using private npm packages from our [monorepo](https://github.com/theorchard/orchard-suite) and packagecloud.io registry.
   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!
4. This Graphql is Federated with "graphql-theorchard". New schema Types should avoid name clashes with existing schema Types in other federated services. E.g., if a GlobalSoundRecording Type were added to this schema, it would conflict with GlobalSoundRecording in graphql-knowledge. We should extend those types instead and use it.
   E.g:

```
type GlobalSoundRecording @key(fields: "isrc")  {
  isrc: String!
}

type MyNewThing {
    id: ID!
    artistNames: String
    globalSoundRecording: GlobalSoundRecording
}
```

5. Fields marked `@external` (e.g. in src/schema/Abacus.graphql) are resolved in another subgraph, so cannot be queried using the local graphql sandbox. For local development, you will need to set up a local [graphql-router](https://github.com/theorchard/graphql-router) which points to the local graphql-nr service.

## 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 Management Setup](https://github.com/theorchard/docs/blob/master/javascript/packages.md)

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

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

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. Authenticate to ECR in the AWS account containing parent images (specified in Dockerfile)

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

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

5. 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 from Schema

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

```
yarn generate:types && yarn generate:types:integration
```

This will produce/update the `generated/index.ts` and `tests/definitions/index.ts` files with type definitions and lint changes. You should commit that file along with your schema changes. You can use the `generated/index.ts` types in your connectors or resolvers.

## Integration tests

There are 2 ways of running integration tests.

### In Docker (using local DBs)

- When we run our integration tests in Docker, we spin up local DBs to run the tests against. This means we aren't modifying our QA data, which our QA apps rely on. This is how integration tests are run in the Jenkins pipeline.
    - We have three Docker services, one for each of the DBs the graphql-nr server connects to (`local-ownership-rds`, `local-performance-rds` and `local-neo4j`).
    - The credentials for these are hard-coded in `docker-compose.yaml`, so you don't need to add any new env vars.

- `local-ownership-rds` and `local-performance-rds` data is seeded via the sql queries in the `db` directory.
    - Note: if you modify the sql queries in the `db` directory, you will need to clear the `storage` directory with `yarn clear:storage`.

- Neo4j data is populated using the `tests/global/setup.ts` script before the tests run.
    - **Please read if updating test data**: if the integration tests are run outside of Docker, this cypher will be run against our QA DBs. Please make sure any queries are compatible with QA Neo4J (i.e. you aren't adding/removing anything that would break a QA frontend app, or break another repo's tests.)

**To run the tests with Docker, open Docker Desktop and run**

```
awsume prod
yarn dev:docker:test:integration
```

After the tests have run, containers should stop automatically. Otherwise you can run `docker compose down`.

**Querying the local DBs:**

- Once the local DBs are running, you can query these:
    - For Neo4J, use the url `http://localhost:7474/browser` with username `neo4j` and password `neo4jPass`.
    - For the MySQL servers, go to the container/Exec tab in the UI, and enter `mysql -u root -p` and then the password. Alternatively you can run `docker exec -it performancedeliveryrds mysql -uroot --password=performancePass`.

###

## Available Scripts

In the project directory, you can run:

### `yarn start`

Runs the app in the development mode.
Open http://localhost:8080/graphql to view the playground in the browser.
The server will reload if you make edits.

**NOTE:**
**Running the project locally requires an active VPN connection.**

### `yarn test:unit --watch`

Runs the jest unit tests in watch mode. In watch mode type `p` to enter a filter.

### `yarn test:integration`

Runs the jest integration tests against a local instance. **Note** that you need to run `yarn start` in a different process first.

### `yarn test`

Runs unit and integration tests. Run this before creating a PR.

### `yarn lint`

Runs the js and graphql schema linter. Run this before creating a PR.

### `yarn build`

Builds the app for production to the `build` folder.<br />

Note that this command is not required to run locally, it is run during the docker build steps.

### `yarn jest --updateSnapshot`

Updates snapshot for snapshot testing.

# Performance and Ownership Rights

Note that we have types, queries and mutations for both Ownership Rights and Performance Rights in this graphql service. Almost all performance-related queries and mutations are in the schema files starting with `NR...`. The ownership-related queries and mutations are in the schema files starting with `Ownership...`.

# Permissions checks

We are in the process of adding Permissions Platform checks to graphql-neighbouring-rights. Currently this is limited to performance contributions, and controlled by the feature flag `nr_content_pp_enabled`.

If your identity has the FF enabled, you will need to have permissions for the resource/action defined using the `@ppAllowedTenants` directive on each field. You will also need the `Authorization` header with a valid `Bearer ` + jwt.
