# graphql-product

**Please familiarize yourself with [GraphQL at The Orchard](https://github.com/theorchard/docs/tree/master/graphql) before contributing!**

#### GraphQL Playground
[https://qa-graphql-product.theorchard.io/graphql](https://qa-graphql-product.theorchard.io/graphql)

### 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.


**Important**: You need to provide Orchard identity headers. Click "Headers" tab in the UI:

For InsightsProfiles paste this snippet:
```
{
  "apollographql-client-name": <frontend-repo-name>,
  "Orchard-Identity-Id": <identity-id>,
  "Orchard-Profile-Type": "InsightsProfile",
  "Orchard-Profile-Id": <profile-id>
}
```

For vendors paste this snippet:
```
{
  "Grass-Account-Type": "vendor",
  "Grass-Account-Id": 15554
}
```

For OA users paste this snippet:
```
{
  "Orchard-User-Id": "oa:105"
}
```

For ArtistProfiles paste this snippet:
```
{
  "apollographql-client-name": <frontend-repo-name>,
  "Orchard-Identity-Id": <identity-id>,
  "Orchard-Profile-Type": "ArtistProfile",
  "Orchard-Profile-Id": <profile-id>
}
```
For the above
- The ```apollographql-client-name``` valid values can be found in src/constants/applications.js in the APPLICATION_MAP object.
- The ```identity-id``` and ```profile-id``` can be found by logging in to insights and looking at the account page.


Example query:
```
{
  allProductsSearch(limit: 5, status: "in_content")
  {
    totalCount
    products {
      productId
      productName
      tracks {
        tuid
        trackName
        primaryArtists {
          artistName
        }
      }
    }
  }
}
```

### 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
```

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

### Request Logging

To disable request logging, set `OWS_REQUEST_TRACING=false` in your `.env` file.

### Schema and Docs in Local Playground

To enable the *Schema* and *Docs* tabs when running locally, set `APOLLO_INTROSPECTION=true` in your `env` file.

### Tests and Linting

Utilize the following `yarn` task to perform testing and linting:

```
yarn test                   # Run the unit tests and linter
yarn lint:js                # Run the linter
yarn test:unit              # Run the unit tests
yarn test:integration       # Run the integration tests (make sure the application is running)
```

#### Project transfer integration test fixtures

The `project-transfer-job` integration tests require a project that can be transferred between two vendors. Set these in your `.env`:

```
TEST_TRANSFER_PROJECT_ID=<project id to transfer>
TEST_TRANSFER_ORIGIN_VENDOR_ID=<vendor id the project currently belongs to>
TEST_TRANSFER_DESTINATION_VENDOR_ID=<vendor id to transfer the project to>
```

### Federation

New schema Types should avoid name clashes with existing schema Types in other federated services.

For example, if a ```Chart``` Type were added to the schema, it would conflict with ```Chart``` in graphql-knowledge

Check any new Types do not share names with those defined in:

    graphql-knowledge
    graphql-participant
    graphql-user

### Other

[Mocking Microservice REST Requests](docs/mocking-requests.md)


### Running new Opensearch queries locally
There are a few new graphql search queries to replace the existing Cloudsearch ones.
 See the [detail queries here](https://www.notion.so/New-Graphql-Product-search-WIP-2ed97177520f80c99cf4d1b53d5ca89a) . In order to try these from your local graphql-product you will need a user for opensearch instance

1. Create a user for `content-search` opensearch instance if you dont have one and map it to a role that has access to search the indexes. This is [the document](https://www.notion.so/Terraform-for-Opensearch-management-22997177520f80cc9042f45ea2580559?source=copy_link#22997177520f80db88f1e6ed5309fcb4) on how to do this from terraform.
2. Set these environment variables
    ```
    CONTENT_OPENSEARCH_URL="https://vpc-qa-content-search-fev6wyjtut6zt4evbtwwnjm56q.us-east-1.es.amazonaws.com/"
    CONTENT_OPENSEARCH_USERNAME=<from step 1>
    CONTENT_OPENSEARCH_PASSWORD=<from step 1>

    ```
3. Run graphql using `yarn start` and try one of the search requests.



### Running graphql-service with frontend service
1. This service uses jwt-service-handler private package. The package defaults to use QA auth0 creds for jwt token validation on local graphql setup. Make sure to use qa auth0 creds in your frontend service local setups so that they generate QA jwt and pass on in graphql calls.
