Skip to content

Commit

Permalink
feat: Add validate executor (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
domjtalbot authored Jul 17, 2022
1 parent c7c4a60 commit 4cdb0bd
Show file tree
Hide file tree
Showing 22 changed files with 245 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-insects-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nx-plugin-graphql-mesh': major
---

Add mesh `validate` executor
5 changes: 3 additions & 2 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ runs:
path: |
~/.cache/Cypress
**/node_modules
!**/node_modules/.cache/nx
${{ steps.pnpm-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-pnpm-dep-cache-v1-${{ hashFiles('**/pnpm-lock.yaml') }}
key: ${{ runner.os }}-pnpm-dep-cache-v3-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-dep-cache-v1-
${{ runner.os }}-pnpm-dep-cache-v3-
- name: Install Node Dependencies
if: steps.pnpm-cache.outputs.cache-hit != 'true'
Expand Down
31 changes: 22 additions & 9 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ on:
NX__WEATHERBIT__API_KEY:
description: 'The Weatherbit API Key'
required: true
NX_CLOUD_TOKEN:
NX_CLOUD_ACCESS_TOKEN:
description: 'The NX Cloud API token'
required: true

Expand All @@ -34,21 +34,21 @@ concurrency:
env:
NX__TRIPPIN__API_KEY: ${{ secrets.NX__TRIPPIN__API_KEY }}
NX__WEATHERBIT__API_KEY: ${{ secrets.NX__WEATHERBIT__API_KEY }}
NX_CLOUD_TOKEN: ${{ secrets.NX_CLOUD_TOKEN }}
NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}
NX_AFFECTED: ${{ inputs.affected == 'true' || true }}
NX_PARALLEL: ${{ inputs.parallel || 3 }}
NX_AFFECTED_COMMANDS: |
pnpm nx-cloud record -- npx nx workspace-lint
pnpm nx-cloud record -- npx nx format:check
pnpm nx affected --target=lint --parallel=${{ inputs.parallel || 3 }}
pnpm nx affected --target=build --parallel=${{ inputs.parallel || 3 }}
pnpm nx affected --target=test --parallel=${{ inputs.parallel || 3 }} --ci --code-coverage
pnpm nx affected --target=lint --parallel=${{ inputs.parallel || 2 }}
pnpm nx affected --target=build --parallel=${{ inputs.parallel || 2 }}
pnpm nx affected --target=test --parallel=${{ inputs.parallel || 2 }} --ci --code-coverage
NX_COMMANDS: |
pnpm nx-cloud record -- npx nx workspace-lint
pnpm nx-cloud record -- npx nx format:check
pnpm nx run-many --target=lint --all --parallel=${{ inputs.parallel || 3 }}
pnpm nx run-many --target=build --all --parallel=${{ inputs.parallel || 3 }}
pnpm nx run-many --target=test --all --parallel=${{ inputs.parallel || 3 }} --ci --code-coverage
pnpm nx run-many --target=lint --all --parallel=${{ inputs.parallel || 2 }}
pnpm nx run-many --target=build --all --parallel=${{ inputs.parallel || 2 }}
pnpm nx run-many --target=test --all --parallel=${{ inputs.parallel || 2 }} --ci --code-coverage
jobs:
build-test:
Expand Down Expand Up @@ -127,9 +127,22 @@ jobs:

# https://github.com/nrwl/ci/blob/main/.github/workflows/nx-cloud-main.yml#L246
- name: Nx Targets
run: ${{ github.workspace }}/.github/workflows/run-commands-in-parallel.sh '${{ steps.parallel_commands_config.outputs.result }}'
run: |
${{ github.workspace }}/.github/workflows/run-commands-in-parallel.sh '${{ steps.parallel_commands_config.outputs.result }}'
shell: bash

- name: Affected Validate
if: ${{ env.NX_AFFECTED == 'true' }}
shell: bash
run: |
pnpm nx affected --target=validate --parallel=${{ inputs.parallel || 3 }}
- name: Validate
if: ${{ env.NX_AFFECTED == 'false' }}
shell: bash
run: |
pnpm nx run-many --target=validate --all --parallel=${{ inputs.parallel || 3 }}
- name: Affected E2E
timeout-minutes: 5
if: ${{ env.NX_AFFECTED == 'true' }}
Expand Down
4 changes: 1 addition & 3 deletions .lintstagedrc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ export default {
'{nx,workspace}.json': () => 'nx workspace-lint --fix',
'**/*.{js,jsx,ts,tsx}': () => 'nx affected --target=lint --fix',
'{apps,libs}/**/*.*': () => [
'nx format:write',
'nx affected --target=build',
'nx affected --target=validate',
'nx affected --target=test',
'nx affected --target=e2e',
],
'apps/api-gateway/**/*.*': () => [
'nx affected --target=e2e --configuration start',
],
};
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ This is the equivalent of using `graphql-mesh dev`, but with extra steps for pac
| **`tsConfig`** | `string` | `true` | - | The path to the Typescript configuration file. |
| **`updateBuildableProjectDepsInPackageJson`** | `boolean` | `false` | `true` | Whether to update the buildable project dependencies in `package.json`. |

<br/>

### `build-gateway`

Builds artifacts for a GraphQL Mesh API Gateway app.
Expand Down Expand Up @@ -90,6 +92,8 @@ This is the equivalent of using `graphql-mesh build`, but with extra steps for c
| **`fileType`** | `json`, `ts` or `js` | `false` | `ts` | The filetype. |
| **`require`** | `string[]` | `false` | `[]` | Loads specific require.extensions before running the codegen and reading the configuration. |

<br/>

### `build-swc`

Builds artifacts for a GraphQL Mesh library.
Expand Down Expand Up @@ -128,6 +132,8 @@ This is the equivalent of using `graphql-mesh build`, but with extra steps for p
| **`tsConfig`** | `string` | `true` | - | The path to the Typescript configuration file. |
| **`updateBuildableProjectDepsInPackageJson`** | `boolean` | `false` | `true` | Whether to update the buildable project dependencies in `package.json`. |

<br/>

### `dev`

Serves a GraphQL server with GraphQL interface by building artifacts on the fly.
Expand All @@ -154,6 +160,8 @@ This is the equifilent of using `graphql-mesh dev`.
| **`port`** | `number` | `false` | `4000` | The port number to run on. |
| **`require`** | `string[]` | `false` | `[]` | Loads specific require.extensions before running the codegen and reading the configuration. |

<br/>

### `start`

Serves a GraphQL server with GraphQL interface based on your generated artifacts.
Expand Down Expand Up @@ -182,6 +190,33 @@ This is the equifilent of using `graphql-mesh start`.

<br/>

### `validate`

Validates artifacts.

This is the equifilent of using `graphql-mesh validate`.

```json
"targets": {
"validate": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:validate",
"options": {
"dir": "path/to/app/or/lib",
},
},
}
```

#### Options

| Name | Type | Required | Default | Description |
| ------------- | ---------- | -------- | ------- | ------------------------------------------------------------------------------------------- |
| **`debug`** | `boolean` | `false` | `false` | Display debugging info by applying the `DEBUG` env variable. |
| **`dir`** | `string` | `true` | - | The path of the directory containing the GraphQL Mesh config. |
| **`require`** | `string[]` | `false` | `[]` | Loads specific require.extensions before running the codegen and reading the configuration. |

<br/>

## Examples

### API Gateway
Expand Down
12 changes: 11 additions & 1 deletion apps/api-gateway/stackexchange/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"targets": {
"build": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:build-gateway",
"outputs": ["{options.dir}/.mesh", "{options.outputPath}"],
"outputs": [
"apps/api-gateway/stackexchange/.mesh",
"{options.outputPath}"
],
"options": {
"dir": "apps/api-gateway/stackexchange",
"outputPath": "dist/apps/api-gateway/stackexchange"
Expand Down Expand Up @@ -62,6 +65,13 @@
"jestConfig": "apps/api-gateway/stackexchange/jest.config.ts",
"passWithNoTests": true
}
},

"validate": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:validate",
"options": {
"dir": "apps/api-gateway/stackexchange"
}
}
},
"tags": ["type:app"]
Expand Down
8 changes: 7 additions & 1 deletion apps/api-gateway/trippin/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"targets": {
"build": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:build-gateway",
"outputs": ["{options.dir}/.mesh", "{options.outputPath}"],
"outputs": ["apps/api-gateway/trippin/.mesh", "{options.outputPath}"],
"options": {
"dir": "apps/api-gateway/trippin",
"outputPath": "dist/apps/api-gateway/trippin"
Expand Down Expand Up @@ -62,6 +62,12 @@
"jestConfig": "apps/api-gateway/trippin/jest.config.ts",
"passWithNoTests": true
}
},
"validate": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:validate",
"options": {
"dir": "apps/api-gateway/trippin"
}
}
},
"tags": ["type:app"]
Expand Down
8 changes: 7 additions & 1 deletion apps/api-gateway/weatherbit/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"targets": {
"build": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:build-gateway",
"outputs": ["{options.dir}/.mesh", "{options.outputPath}"],
"outputs": ["apps/api-gateway/weatherbit/.mesh", "{options.outputPath}"],
"options": {
"dir": "apps/api-gateway/weatherbit",
"outputPath": "dist/apps/api-gateway/weatherbit"
Expand Down Expand Up @@ -62,6 +62,12 @@
"jestConfig": "apps/api-gateway/weatherbit/jest.config.ts",
"passWithNoTests": true
}
},
"validate": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:validate",
"options": {
"dir": "apps/api-gateway/weatherbit"
}
}
},
"tags": ["type:app"]
Expand Down
12 changes: 0 additions & 12 deletions apps/nextjs/stackexchange/.meshrc.yml

This file was deleted.

6 changes: 6 additions & 0 deletions libs/nx-plugin-graphql-mesh/executors.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
"schema": "./src/executors/start/schema.json",
"description": "Serves a GraphQL server with GraphQL interface based on your generated artifacts",
"hasher": "./src/executors/start/hasher"
},
"validate": {
"implementation": "./src/executors/validate/validate.impl",
"schema": "./src/executors/validate/schema.json",
"description": "Validates artifacts",
"hasher": "./src/executors/validate/hasher"
}
}
}
27 changes: 27 additions & 0 deletions libs/nx-plugin-graphql-mesh/src/executors/validate/hasher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Hasher, HasherContext } from '@nrwl/devkit';

import { validateHasher } from './hasher';

describe('validateHasher', () => {
it('should generate hash', async () => {
const mockHasher: Hasher = {
hashTaskWithDepsAndContext: jest
.fn()
.mockReturnValue({ value: 'hashed-task' }),
} as unknown as Hasher;
const hash = await validateHasher(
{
id: 'my-task-id',
target: {
project: 'proj',
target: 'target',
},
overrides: {},
},
{
hasher: mockHasher,
} as unknown as HasherContext
);
expect(hash).toEqual({ value: 'hashed-task' });
});
});
12 changes: 12 additions & 0 deletions libs/nx-plugin-graphql-mesh/src/executors/validate/hasher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { CustomHasher } from '@nrwl/devkit';

/**
* This is a boilerplate custom hasher that matches
* the default Nx hasher. If you need to extend the behavior,
* you can consume workspace details from the context.
*/
export const validateHasher: CustomHasher = async (task, context) => {
return context.hasher.hashTaskWithDepsAndContext(task);
};

export default validateHasher;
27 changes: 27 additions & 0 deletions libs/nx-plugin-graphql-mesh/src/executors/validate/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"title": "Start executor",
"description": "Validates artifacts",
"type": "object",
"properties": {
"debug": {
"type": "boolean",
"description": "Display debugging info.",
"default": false
},
"dir": {
"description": "The directory containing built GraphQL Mesh assets (.mesh).",
"type": "string"
},
"require": {
"type": "array",
"description": "Loads specific require.extensions before running the codegen and reading the configuration.",
"items": {
"type": "string"
},
"default": []
}
},
"required": ["dir"]
}
6 changes: 6 additions & 0 deletions libs/nx-plugin-graphql-mesh/src/executors/validate/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Options } from '../../utils';

type MeshValidateSchema = Options<'validate'>;

export type ValidateExecutorSchema = MeshValidateSchema['args'] &
MeshValidateSchema['env'];
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { ExecutorContext } from '@nrwl/devkit';

import { logger } from '@nrwl/devkit';
import { resolve } from 'path';

import { runMeshCli } from '../../utils/mesh-cli';

import { ValidateExecutorSchema } from './schema';

export default async function* startExecutor(
options: ValidateExecutorSchema,
context: ExecutorContext
) {
if (options.dir === undefined) {
throw new Error("Please define the 'dir' value");
}

logger.info('Validating GraphQL Mesh artifacts...');

await runMeshCli(
'validate',
{
args: {
dir: resolve(context.root, options.dir),
require: options.require,
},
env: {
debug: options.debug,
},
},
context
);

yield {
success: true,
};
}
3 changes: 2 additions & 1 deletion libs/nx-plugin-graphql-mesh/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"**/*.d.ts",
"src/executors/build-gateway/schema.ts",
"src/executors/build/schema.ts",
"src/executors/build-swc/schema.ts"
"src/executors/build-swc/schema.ts",
"src/executors/validate/schema.ts"
]
}
Loading

0 comments on commit 4cdb0bd

Please sign in to comment.