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

Add optional graphql-codegen step to build executors #98

Merged
merged 10 commits into from
Dec 19, 2022
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

.cache
.cache-loader
.codegen
.docusaurus
.mesh
.next
Expand Down
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ yarn add -D nx-mesh

### Peer Dependencies

| Name | Version | Required | Auto-installed by generators |
| ------------------- | ---------- | :------: | :--------------------------: |
| `nx` | `>=15` | ✅ | - |
| `@graphql-mesh/cli` | `>=0.71.0` | ✅ | ✅ |
| Name | Version | Required | Auto-installed by generators |
| --------------------- | ---------- | :------: | :--------------------------: |
| `nx` | `>=15` | ✅ | - |
| `@graphql-mesh/cli` | `>=0.71.0` | ✅ | ✅ |
| `@graphql-codgen/cli` | `>=2.16.1` | ✅ | - |

<br/>

Expand Down Expand Up @@ -270,6 +271,14 @@ This is the equivalent of using `graphql-mesh dev`, but with extra steps for pac
| ----------------------------------------- | ------------------------------------ | :------: | ------------------ | -------------------------------------------------------------------------------------------------------------------------------- |
| `assets` | `string[]` | - | - | List of static assets. |
| `buildableProjectDepsInPackageJsonType` | `dependencies` or `peerDependencies` | - | `peerDependencies` | When `updateBuildableProjectDepsInPackageJson` is `true`, this adds dependencies to either `peerDependencies` or `dependencies`. |
| `codegen` | `object` | - | - | GraphQL Codegen settings |
| `codegen.config` | `string` | ✅ | - | Path to GraphQL codegen YAML config file, defaults to `codegen.yml` on the current directory. |
| `codegen.overwrite` | `boolean` | - | `true` | Overwrites existing files. |
| `codegen.profile` | `boolean` | - | `false` | Use profiler to measure performance. |
| `codegen.project` | `string` | - | `` | Name of a project in GraphQL Config. |
| `codegen.require` | `string[]` | - | `[]` | Loads specific require.extensions before running the codegen and reading the configuration. |
| `codegen.silent` | `boolean` | - | `false` | Suppresses printing errors. |
| `codegen.watch` | `boolean` | - | `false` | Watch for changes and execute generation automatically. |
| `debug` | `boolean` | - | `false` | Display debugging info by applying the `DEBUG` env variable. |
| `dir` | `string` | ✅ | - | The path of the directory containing the GraphQL Mesh config. |
| `fileType` | `json`, `ts` or `js` | - | `ts` | The filetype. |
Expand Down Expand Up @@ -343,6 +352,14 @@ This is the equivalent of using `graphql-mesh build`, but with extra steps for p
| ----------------------------------------- | ------------------------------------ | :------: | ------------------ | -------------------------------------------------------------------------------------------------------------------------------- |
| `assets` | `string[]` | - | - | List of static assets. |
| `buildableProjectDepsInPackageJsonType` | `dependencies` or `peerDependencies` | - | `peerDependencies` | When `updateBuildableProjectDepsInPackageJson` is `true`, this adds dependencies to either `peerDependencies` or `dependencies`. |
| `codegen` | `object` | - | - | GraphQL Codegen settings |
| `codegen.config` | `string` | ✅ | - | Path to GraphQL codegen YAML config file, defaults to `codegen.yml` on the current directory. |
| `codegen.overwrite` | `boolean` | - | `true` | Overwrites existing files. |
| `codegen.profile` | `boolean` | - | `false` | Use profiler to measure performance. |
| `codegen.project` | `string` | - | `` | Name of a project in GraphQL Config. |
| `codegen.require` | `string[]` | - | `[]` | Loads specific require.extensions before running the codegen and reading the configuration. |
| `codegen.silent` | `boolean` | - | `false` | Suppresses printing errors. |
| `codegen.watch` | `boolean` | - | `false` | Watch for changes and execute generation automatically. |
| `debug` | `boolean` | - | `false` | Display debugging info by applying the `DEBUG` env variable. |
| `dir` | `string` | ✅ | - | The path of the directory containing the GraphQL Mesh config. |
| `fileType` | `json`, `ts` or `js` | - | `ts` | The filetype. |
Expand Down
16 changes: 16 additions & 0 deletions libs/example/sdk/graphql/star-wars-api/codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
overwrite: true,
schema: 'libs/example/sdk/graphql/star-wars-api/.mesh/schema.graphql',
documents:
'libs/example/sdk/graphql/star-wars-api/src/graphql/**/*.*.graphql',
generates: {
'libs/example/sdk/graphql/star-wars-api/.codegen/': {
preset: 'client',
plugins: [],
},
},
};

export default config;
11 changes: 9 additions & 2 deletions libs/example/sdk/graphql/star-wars-api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@
},
"build": {
"executor": "nx-mesh:build-swc",
"outputs": ["{projectRoot}/.mesh", "{options.outputPath}"],
"outputs": [
"{projectRoot}/.codegen",
"{projectRoot}/.mesh",
"{options.outputPath}"
],
"options": {
"dir": "libs/example/sdk/graphql/star-wars-api",
"main": "libs/example/sdk/graphql/star-wars-api/src/index.ts",
"outputPath": "dist/libs/example/sdk/graphql/star-wars-api",
"tsConfig": "libs/example/sdk/graphql/star-wars-api/tsconfig.lib.json"
"tsConfig": "libs/example/sdk/graphql/star-wars-api/tsconfig.lib.json",
"codegen": {
"config": "libs/example/sdk/graphql/star-wars-api/codegen.ts"
}
}
},
"serve": {
Expand Down
1 change: 1 addition & 0 deletions libs/example/sdk/graphql/star-wars-api/src/lib/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../.codegen';
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { ExecutorContext } from '@nrwl/devkit';

import { logger } from '@nrwl/devkit';
import { directoryExists } from '@nrwl/workspace/src/utilities/fileutils';
import { copySync, mkdir } from 'fs-extra';
import { join, resolve } from 'path';

import { createPackageJson, runMeshCli } from '../../utils';
import { createPackageJson } from '../../utils';
import { runMeshCli } from '../../utils/mesh-cli';
import { BuildGatewayExecutorSchema } from './schema';

export default async function* buildExecutor(
Expand Down Expand Up @@ -39,6 +41,9 @@ export default async function* buildExecutor(

copySync(join(dir, '.mesh'), join(options.outputPath, '.mesh'));

logger.info('');
logger.info('Creating package.json...');

await createPackageJson(
{
dir: options.dir,
Expand Down
2 changes: 1 addition & 1 deletion libs/nx-mesh/src/executors/build-gateway/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Options } from '../../utils';
import type { Options } from '../../utils/mesh-cli';

type MeshBuildSchema = Options<'build'>;

Expand Down
26 changes: 25 additions & 1 deletion libs/nx-mesh/src/executors/build-swc/build-swc.impl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { ExecutorContext } from '@nrwl/devkit';

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

import { createPackageJson, runMeshCli } from '../../utils';
import { createPackageJson } from '../../utils';
import { runCodegenCli } from '../../utils/graphql-codegen-cli';
import { runMeshCli } from '../../utils/mesh-cli';
import { swcExecutor } from './swc-executor/swc.impl';
import { BuildSWCExecutorSchema } from './schema';

Expand Down Expand Up @@ -31,6 +34,9 @@ export default async function* buildExecutor(
context
);

logger.info('');
logger.info('Running SWC compiler...');

const tsc = swcExecutor(
{
assets: [...options.assets],
Expand All @@ -50,6 +56,9 @@ export default async function* buildExecutor(
}

if (success) {
logger.info('');
logger.info('Creating package.json...');

await createPackageJson(
{
dir: options.dir,
Expand All @@ -60,6 +69,21 @@ export default async function* buildExecutor(
);
}

if (options.codegen?.config) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

logger.info('');
logger.info('Running GraphQL Codegen...');

await runCodegenCli(
{
...options.codegen,
debug: options.debug,
verbose: true,
watch: options.watch,
},
context
);
}

yield {
success,
};
Expand Down
42 changes: 42 additions & 0 deletions libs/nx-mesh/src/executors/build-swc/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,48 @@
"outputPath": {
"type": "string",
"description": "The output path of the generated files."
},
"codegen": {
"type": "object",
"properties": {
"config": {
"type": "string",
"description": "Path to GraphQL codegen YAML config file, defaults to `codegen.yml` on the current directory."
},
"overwrite": {
"type": "boolean",
"description": "Overwrites existing files.",
"default": true
},
"profile": {
"type": "boolean",
"description": "Use profiler to measure performance",
"default": false
},
"project": {
"type": "string",
"description": "Name of a project in GraphQL Config",
"default": ""
},
"require": {
"type": "array",
"description": "Loads specific require.extensions before running the codegen and reading the configuration.",
"items": {
"type": "string"
},
"default": []
},
"silent": {
"type": "boolean",
"description": "Suppresses printing errors.",
"default": false
},
"watch": {
"type": "boolean",
"description": "Watch for changes and execute generation automatically.",
"default": false
}
}
}
},
"required": ["main", "dir", "outputPath", "tsConfig"],
Expand Down
8 changes: 7 additions & 1 deletion libs/nx-mesh/src/executors/build-swc/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Options } from '../../utils';
import type { Arguments } from '../../utils/graphql-codegen-cli';
import type { Options } from '../../utils/mesh-cli';

interface SwcExecutorSchema {
assets: string[];
Expand All @@ -17,6 +18,11 @@ type MeshBuildSchema = Options<'build'>;
export type BuildSWCExecutorSchema = SwcExecutorSchema &
MeshBuildSchema['args'] &
MeshBuildSchema['env'] & {
/**
* GraphQL Codegen config
*/
codegen?: Arguments;

/**
* The output path of the generated files.
*/
Expand Down
24 changes: 23 additions & 1 deletion libs/nx-mesh/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { logger } from '@nrwl/devkit';
import { tscExecutor } from '@nrwl/js/src/executors/tsc/tsc.impl';
import { resolve } from 'path';

import { createPackageJson, runMeshCli } from '../../utils';
import { createPackageJson } from '../../utils';
import { runCodegenCli } from '../../utils/graphql-codegen-cli';
import { runMeshCli } from '../../utils/mesh-cli';

import { BuildExecutorSchema } from './schema';

export default async function* buildExecutor(
Expand Down Expand Up @@ -33,6 +36,7 @@ export default async function* buildExecutor(
);

logger.info('');
logger.info('Running Typescript compiler...');

const tsc = tscExecutor(
{
Expand All @@ -51,6 +55,9 @@ export default async function* buildExecutor(
}

if (success) {
logger.info('');
logger.info('Creating package.json...');

await createPackageJson(
{
dir: options.dir,
Expand All @@ -61,6 +68,21 @@ export default async function* buildExecutor(
);
}

if (options.codegen?.config) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

logger.info('');
logger.info('Running GraphQL Codegen...');

await runCodegenCli(
{
...options.codegen,
debug: options.debug,
verbose: true,
watch: options.watch,
},
context
);
}

yield {
success,
};
Expand Down
42 changes: 42 additions & 0 deletions libs/nx-mesh/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,48 @@
"outputPath": {
"type": "string",
"description": "The output path of the generated files."
},
"codegen": {
"type": "object",
"properties": {
"config": {
"type": "string",
"description": "Path to GraphQL codegen YAML config file, defaults to `codegen.yml` on the current directory."
},
"overwrite": {
"type": "boolean",
"description": "Overwrites existing files.",
"default": true
},
"profile": {
"type": "boolean",
"description": "Use profiler to measure performance",
"default": false
},
"project": {
"type": "string",
"description": "Name of a project in GraphQL Config",
"default": ""
},
"require": {
"type": "array",
"description": "Loads specific require.extensions before running the codegen and reading the configuration.",
"items": {
"type": "string"
},
"default": []
},
"silent": {
"type": "boolean",
"description": "Suppresses printing errors",
"default": false
},
"watch": {
"type": "boolean",
"description": "Watch for changes and execute generation automatically.",
"default": false
}
}
}
},
"required": ["main", "dir", "outputPath", "tsConfig"],
Expand Down
8 changes: 7 additions & 1 deletion libs/nx-mesh/src/executors/build/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Options } from '../../utils';
import type { Arguments } from '../../utils/graphql-codegen-cli';
import type { Options } from '../../utils/mesh-cli';

interface TscExecutorSchema {
assets: string[];
Expand All @@ -15,6 +16,11 @@ type MeshBuildSchema = Options<'build'>;
export type BuildExecutorSchema = TscExecutorSchema &
MeshBuildSchema['args'] &
MeshBuildSchema['env'] & {
/**
* GraphQL Codegen config
*/
codegen?: Arguments;

/**
* The output path of the generated files.
*/
Expand Down
2 changes: 1 addition & 1 deletion libs/nx-mesh/src/executors/start/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Options } from '../../utils';
import type { Options } from '../../utils/mesh-cli';

type MeshStartSchema = Options<'start'>;

Expand Down
2 changes: 1 addition & 1 deletion libs/nx-mesh/src/executors/validate/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Options } from '../../utils';
import type { Options } from '../../utils/mesh-cli';

type MeshValidateSchema = Options<'validate'>;

Expand Down
Loading