# nx based affected targets

[<mark style="color:blue;">Nx</mark>](https://nx.dev/) is a popular build framework for monorepos that focuses in simplicity. Like many other distributed build frameworks, the core concept is around identifying the targets that are affected in a change, and using a build cache. These same concepts can also be used for optimizing MergeQueue builds.

### GitHub Action

GitHub action might be the easiest way to fetch the affected targets and push that to Aviator API. Here’s an example of how you can setup such a GitHub Action. You can also find this code in action in [<mark style="color:blue;">nx-examples repository fork</mark>](https://github.com/aviator-co/nx-examples/blob/master/.github/workflows/aviator-targets.yml).

```yaml
name: Fetch Aviator targets

on:
  pull_request:

jobs:
  nextjs-build:
    runs-on: ubuntu-latest
    defaults:
      run:
         working-directory: .
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v3
        with:
          node-version: '18'
          cache: 'yarn'
      - run: yarn install
      - run: |
          set -euo pipefail
          git fetch origin $GITHUB_BASE_REF
          PR_NUMBER=${{ github.event.number }}
          TARGETS_JSON=$(npx nx show projects --affected --base FETCH_HEAD --head HEAD --json)
          curl -X POST -H "Authorization: Bearer ${{ secrets.AVIATOR_API_TOKEN }}" \
          -H "Content-Type: application/json" \
          -d '{
            "action": "update",
            "pull_request": {
                "number": '"$PR_NUMBER"',
                "repository": {"name": "nx-examples", "org": "aviator-co"},
                "affected_targets": '"$TARGETS_JSON"'
            }
          }' https://api.aviator.co/api/v1/pull_request/
```

### A few things to note

* It is recommended to store the API access token as `AVIATOR_API_TOKEN` in [GitHub secrets.](https://docs.github.com/en/actions/security-guides/encrypted-secrets) or replace the secrets above
* There are two separate `action` for the `pull_request` API: `update` and `queue`. If you use `update` API, these affected targets information is sent to Aviator. A developer can then queue the PR asynchronously. If using the `update` API, you should call this GitHub action every time a new commit is added to the PR.
* Alternatively, you can submit this as a `queue` action when the PR is ready to be queued. In that case, the information is submitted to the Aviator MergeQueue while queueing the PR in the same step.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aviator.co/mergequeue/concepts/affected-targets/nx-based-affected-targets.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
