Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
feat: add serve executor (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
domjtalbot authored Jul 31, 2022
1 parent ca8506b commit 6b85c28
Show file tree
Hide file tree
Showing 23 changed files with 335 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-pumas-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nx-plugin-graphql-mesh': major
---

Add `serve` executor
92 changes: 78 additions & 14 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ jobs:
timeout-minutes: 10
continue-on-error: ${{ matrix.experimental }}
strategy:
max-parallel: 3
max-parallel: 2
fail-fast: false
matrix:
node: [lts]
command: [build, test, validate, e2e, e2e-start]
command:
[build, test, validate, e2e, e2e-start, e2e-serve, e2e-serve-dev]
experimental: [false]
include:
- node: latest
Expand All @@ -129,6 +130,12 @@ jobs:
- node: latest
command: e2e-start
experimental: true
- node: latest
command: e2e-serve
experimental: true
- node: latest
command: e2e-serve-dev
experimental: true

name: node-${{ matrix.node }} - ${{ matrix.command }}
env:
Expand Down Expand Up @@ -181,26 +188,83 @@ jobs:
run: |
pnpm nx run-many --target=validate --all --parallel=2
- name: E2E
timeout-minutes: 5
if: ${{ env.NX_AFFECTED == 'false' }}
uses: cypress-io/github-action@be2de7b1fe1cdafb4a3c8da0a3746476ef636b65 # tag=v4.1.0
with:
install: false
command: |
pnpm nx run-many --target=e2e --all --parallel=${{ env.NX_PARALLEL }}
- name: Affected E2E
if: inputs.affected == 'true' && matrix.command == 'e2e'
shell: bash
run: |
pnpm nx affected --target=e2e --parallel=3
uses: cypress-io/github-action@be2de7b1fe1cdafb4a3c8da0a3746476ef636b65
timeout-minutes: 5
with:
install: false
command: |
pnpm nx affected --target=e2e --parallel=3
- name: E2E
if: inputs.affected != 'true' && matrix.command == 'e2e'
shell: bash
run: |
pnpm nx run-many --target=e2e --all --parallel=3
uses: cypress-io/github-action@be2de7b1fe1cdafb4a3c8da0a3746476ef636b65
timeout-minutes: 5
with:
install: false
command: |
pnpm nx run-many --target=e2e --all --parallel=3
- name: Affected E2E-Start
if: inputs.affected == 'true' && matrix.command == 'e2e-start'
shell: bash
run: |
pnpm nx affected --target=e2e --configuration=start --parallel=2
uses: cypress-io/github-action@be2de7b1fe1cdafb4a3c8da0a3746476ef636b65
timeout-minutes: 5
with:
install: false
command: |
pnpm nx affected --target=e2e --configuration=start --parallel=3
- name: E2E-Start
if: inputs.affected != 'true' && matrix.command == 'e2e-start'
shell: bash
run: |
pnpm nx run-many --target=e2e --configuration=start --all --parallel=2
uses: cypress-io/github-action@be2de7b1fe1cdafb4a3c8da0a3746476ef636b65
timeout-minutes: 5
with:
install: false
command: |
pnpm nx run-many --target=e2e --configuration=start --all --parallel=3
- name: Affected E2E-Serve
if: inputs.affected == 'true' && matrix.command == 'e2e-serve'
uses: cypress-io/github-action@be2de7b1fe1cdafb4a3c8da0a3746476ef636b65
timeout-minutes: 5
with:
install: false
command: |
pnpm nx affected --target=e2e --configuration=serve --parallel=3
- name: E2E-Serve
if: inputs.affected != 'true' && matrix.command == 'e2e-serve'
uses: cypress-io/github-action@be2de7b1fe1cdafb4a3c8da0a3746476ef636b65
timeout-minutes: 5
with:
install: false
command: |
pnpm nx run-many --target=e2e --configuration=serve --all --parallel=3
- name: Affected E2E-Serve
if: inputs.affected == 'true' && matrix.command == 'e2e-serve-dev'
uses: cypress-io/github-action@be2de7b1fe1cdafb4a3c8da0a3746476ef636b65
timeout-minutes: 5
with:
install: false
command: |
pnpm nx affected --target=e2e --configuration=serve-dev --parallel=3
- name: E2E-Serve
if: inputs.affected != 'true' && matrix.command == 'e2e-serve-dev'
uses: cypress-io/github-action@be2de7b1fe1cdafb4a3c8da0a3746476ef636b65
timeout-minutes: 5
with:
install: false
command: |
pnpm nx run-many --target=e2e --configuration=serve-dev --all --parallel=3
4 changes: 3 additions & 1 deletion .lintstagedrc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export default {
'nx affected --target=validate',
'nx affected --target=test',
'nx affected --target=e2e',
'nx affected --target=e2e --configuration start',
'nx affected --target=e2e --configuration=start',
'nx affected --target=e2e --configuration=serve',
'nx affected --target=e2e --configuration=serve-dev',
],
};
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,41 @@ This is the equifilent of using `graphql-mesh start`.

<br/>

### `serve`

Serves a GraphQL server.

This combines `dev` & `start` via a `dev` option toggle.

```json
"targets": {
"serve": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:serve",
"options": {
"dir": "path/to/app/or/lib",
"dev": true,
},
"configuration": {
"production": {
"dev": false,
}
}
},
}
```

#### Options

| Name | Type | Required | Default | Description |
| ------------- | ---------- | -------- | ------- | ------------------------------------------------------------------------------------------- |
| **`debug`** | `boolean` | `false` | `false` | Display debugging info by applying the `DEBUG` env variable. |
| **`dev`** | `string` | `false` | `false` | Run the server in dev or production mode. |
| **`dir`** | `string` | `true` | - | The path of the directory containing the GraphQL Mesh config. |
| **`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/>

### `validate`

Validates artifacts.
Expand Down
6 changes: 6 additions & 0 deletions apps/api-gateway/stackexchange-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
},
"start": {
"devServerTarget": "api-gateway-stackexchange:start:e2e"
},
"serve-dev": {
"devServerTarget": "api-gateway-stackexchange:serve"
},
"serve": {
"devServerTarget": "api-gateway-stackexchange:serve:production"
}
}
},
Expand Down
15 changes: 15 additions & 0 deletions apps/api-gateway/stackexchange/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@
}
}
},
"serve": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:serve",
"options": {
"dir": "apps/api-gateway/stackexchange",
"port": 4700,
"dev": true
},
"configurations": {
"production": {
"dir": "dist/apps/api-gateway/stackexchange",
"dev": false,
"port": 5200
}
}
},
"dev": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:dev",
"options": {
Expand Down
6 changes: 6 additions & 0 deletions apps/api-gateway/trippin-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
},
"start": {
"devServerTarget": "api-gateway-trippin:start:e2e"
},
"serve-dev": {
"devServerTarget": "api-gateway-trippin:serve"
},
"serve": {
"devServerTarget": "api-gateway-trippin:serve:production"
}
}
},
Expand Down
15 changes: 15 additions & 0 deletions apps/api-gateway/trippin/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@
}
}
},
"serve": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:serve",
"options": {
"dir": "apps/api-gateway/trippin",
"port": 5001,
"dev": true
},
"configurations": {
"production": {
"dir": "dist/apps/api-gateway/trippin",
"dev": false,
"port": 5100
}
}
},
"dev": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:dev",
"options": {
Expand Down
6 changes: 6 additions & 0 deletions apps/api-gateway/weatherbit-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
},
"start": {
"devServerTarget": "api-gateway-weatherbit:start:e2e"
},
"serve-dev": {
"devServerTarget": "api-gateway-weatherbit:serve"
},
"serve": {
"devServerTarget": "api-gateway-weatherbit:serve:production"
}
}
},
Expand Down
15 changes: 15 additions & 0 deletions apps/api-gateway/weatherbit/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@
}
}
},
"serve": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:serve",
"options": {
"dir": "apps/api-gateway/weatherbit",
"port": 4600,
"dev": true
},
"configurations": {
"production": {
"dir": "dist/apps/api-gateway/weatherbit",
"dev": false,
"port": 5300
}
}
},
"dev": {
"executor": "@domjtalbot/nx-plugin-graphql-mesh:dev",
"options": {
Expand Down
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 @@ -24,6 +24,12 @@
"description": "Builds artifacts using SWC.",
"hasher": "./src/executors/build-swc/hasher"
},
"serve": {
"implementation": "./src/executors/serve/serve.impl",
"schema": "./src/executors/serve/schema.json",
"description": "Serves a GraphQL server.",
"hasher": "./src/executors/serve/hasher"
},
"start": {
"implementation": "./src/executors/start/start.impl",
"schema": "./src/executors/start/schema.json",
Expand Down
4 changes: 3 additions & 1 deletion libs/nx-plugin-graphql-mesh/src/executors/dev/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DevExecutorSchema } from './schema';

const readyWhenMsg = 'Serving GraphQL Mesh:';

export default async function* devExecutor(
export async function* devExecutor(
options: DevExecutorSchema,
context: ExecutorContext
) {
Expand Down Expand Up @@ -60,3 +60,5 @@ export default async function* devExecutor(
// This Promise intentionally never resolves, leaving the process running.
});
}

export default devExecutor;
25 changes: 25 additions & 0 deletions libs/nx-plugin-graphql-mesh/src/executors/serve/hasher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Hasher, HasherContext } from '@nrwl/devkit';

import { serveHasher } from './hasher';

describe('serveHasher', () => {
it('should generate hash', async () => {
const mockHasher: Hasher = {
hashTask: jest.fn().mockReturnValue({ value: 'hashed-task' }),
} as unknown as Hasher;
const hash = await serveHasher(
{
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/serve/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 serveHasher: CustomHasher = async (task, context) => {
return context.hasher.hashTask(task);
};

export default serveHasher;
8 changes: 8 additions & 0 deletions libs/nx-plugin-graphql-mesh/src/executors/serve/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { DevExecutorSchema } from '../dev/schema';
import type { StartExecutorSchema } from '../start/schema';

export interface ServeExecutorSchema
extends DevExecutorSchema,
StartExecutorSchema {
dev: boolean;
}
Loading

0 comments on commit 6b85c28

Please sign in to comment.