Skip to content

Commit

Permalink
feat(nx-graphql-codegen): adding new plugin to graphql code generate …
Browse files Browse the repository at this point in the history
…support
  • Loading branch information
Rene Bonilla committed Oct 13, 2023
1 parent daf620c commit 2571045
Show file tree
Hide file tree
Showing 29 changed files with 668 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ A collection of Nx plugins and commons tools to build them.

## Plugins

| Plugin | Description |
| ----------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| [`@nx-tools/nx-container`](packages/nx-container/README.md) | First class support for Container builds in your Nx workspace. Docker, Podman and Kaniko engines are supported |
| [`@nx-tools/nx-prisma`](packages/nx-prisma/README.md) | First class support for [Prisma](https://prisma.io/) in your Nx workspace. |
| Plugin | Description |
| ----------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| [`@nx-tools/nx-container`](packages/nx-container/README.md) | First class support for Container builds in your Nx workspace. Docker, Podman and Kaniko engines are supported |
| [`@nx-tools/nx-prisma`](packages/nx-prisma/README.md) | First class support for [Prisma](https://prisma.io/) in your Nx workspace. |
| [`@nx-tools/nx-graphql-codegen`](packages/nx-graphql-codegen/README.md) | First class support for [graphql code generator](https://the-guild.dev/graphql/codegen) in your Nx workspace. |

## Authors

Expand Down
37 changes: 37 additions & 0 deletions packages/nx-graphql-codegen/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": [
"error",
{
"ignoredDependencies": ["@graphql-codegen/cli", "ts-node"]
}
]
}
},
{
"files": ["./package.json", "./generators.json", "./executors.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/nx-plugin-checks": "error"
}
}
]
}
29 changes: 29 additions & 0 deletions packages/nx-graphql-codegen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# nx-graphql-codegen

The Nx Plugin for Graphql Code Generator contains executor and generator for managing Graphql applications within an Nx workspace.

## Setting up

Adding the Graphql Code Generator plugin to an existing Nx workspace.

```yarn
yarn add -D @nx-tools/nx-graphql-codegen
```

```npm
npm install -D @nx-tools/nx-graphql-codegen
```

## Using Plugin

We can use plugin with bash or Nx console.

```bash
nx g @nx-tools/nx-graphql-codegen:init appName
```

We can the call codegen generate with the followin command:

```
nx codegen-generate appName
```
9 changes: 9 additions & 0 deletions packages/nx-graphql-codegen/executors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"executors": {
"generate": {
"implementation": "./src/executors/generate/executor",
"schema": "./src/executors/generate/schema.json",
"description": "The generate command generates the data types corresponding to the graphql schema"
}
}
}
9 changes: 9 additions & 0 deletions packages/nx-graphql-codegen/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"generators": {
"configuration": {
"factory": "./src/generators/configuration/generator",
"schema": "./src/generators/configuration/schema.json",
"description": "Add graphql-code-generator configuration to project"
}
}
}
10 changes: 10 additions & 0 deletions packages/nx-graphql-codegen/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable */
export default {
displayName: 'nx-graphql-codegen',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/nx-graphql-codegen',
};
22 changes: 22 additions & 0 deletions packages/nx-graphql-codegen/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@nx-tools/nx-graphql-codegen",
"version": "1.0.0-alpha.1",
"type": "commonjs",
"author": "Rene Bonilla <rbonillajr@hotmail.com>",
"repository": "https://github.com/gperdomor/nx-tools",
"bugs": "https://github.com/gperdomor/nx-tools/issues",
"license": "MIT",
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"generators": "./generators.json",
"executors": "./executors.json",
"dependencies": {
"@nx-tools/core": "6.0.0-alpha.1"
},
"peerDependencies": {
"@nx/devkit": "^16.0.0",
"@graphql-codegen/cli": "5.0.0",
"ts-node": "^10.0.0",
"tslib": "^2.5.3"
}
}
71 changes: 71 additions & 0 deletions packages/nx-graphql-codegen/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "nx-graphql-codegen",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/nx-graphql-codegen/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/nx-graphql-codegen",
"main": "packages/nx-graphql-codegen/src/index.ts",
"tsConfig": "packages/nx-graphql-codegen/tsconfig.lib.json",
"assets": [
"packages/nx-graphql-codegen/*.md",
{
"input": "./packages/nx-graphql-codegen/src",
"glob": "**/!(*.ts)",
"output": "./src"
},
{
"input": "./packages/nx-graphql-codegen/src",
"glob": "**/*.d.ts",
"output": "./src"
},
{
"input": "./packages/nx-graphql-codegen",
"glob": "generators.json",
"output": "."
},
{
"input": "./packages/nx-graphql-codegen",
"glob": "executors.json",
"output": "."
}
]
}
},
"publish": {
"command": "node tools/scripts/publish.mjs nx-graphql-codegen {args.ver} {args.tag}",
"dependsOn": ["build"]
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"packages/nx-graphql-codegen/**/*.ts",
"packages/nx-graphql-codegen/package.json",
"packages/nx-graphql-codegen/generators.json",
"packages/nx-graphql-codegen/executors.json"
]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "packages/nx-graphql-codegen/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { getExecOutput } from '@nx-tools/core';
import { GenerateExecutorSchema } from './schema';
import executor from './executor';
import { ExecutorContext } from '@nx/devkit';

jest.mock('@nx-tools/core', () => {
const originalModule = jest.requireActual('@nx-tools/core');
return {
__esModule: true,
...originalModule,
getExecOutput: jest.fn(async () => Promise.resolve({ stderr: '', exitCode: 0 })),
};
});

const mockContext: Partial<ExecutorContext> = {
root: 'workspace-root',
workspace: { version: 2, projects: { foo: { root: 'apps/foo' } } },
projectName: 'foo',
};

export const expectCommandToHaveBeenCalled = (cmd: string, args: string[]) => {
expect(getExecOutput).toHaveBeenCalledWith(cmd, args, { ignoreReturnCode: true });
};

describe('Generate Executor', () => {
beforeEach(() => {
jest.spyOn(console, 'info').mockImplementation(() => true);
});

it('empty options', async () => {
const options: GenerateExecutorSchema = {};
const output = await executor(options, mockContext as ExecutorContext);
expect(expectCommandToHaveBeenCalled('npx graphql-codegen', ['--config=workspace-root/apps/foo/codegen.ts']));
expect(output.success).toBeTruthy();
});

it('with config options', async () => {
const options: GenerateExecutorSchema = {
config: 'workspace-root/apps/foo/codegen.ts',
};
const output = await executor(options, mockContext as ExecutorContext);
expect(expectCommandToHaveBeenCalled('npx graphql-codegen', ['--config=workspace-root/apps/foo/codegen.ts']));
expect(output.success).toBeTruthy();
});
});
24 changes: 24 additions & 0 deletions packages/nx-graphql-codegen/src/executors/generate/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { GenerateExecutorSchema } from './schema';
import { ExecutorContext } from '@nx/devkit';
import { getDefaultScheme } from '../../utils';
import { runCommand } from '../../run-commands';

export default async function runExecutor(
options: GenerateExecutorSchema,
ctx: ExecutorContext
): Promise<{ success: true }> {
return runCommand(options, ctx, {
description: 'Generating types',
command: 'graphql-codegen',
getArgs,
});
}

const getArgs = (options: GenerateExecutorSchema, ctx: ExecutorContext): string[] => {
const args = [];
const config = options?.config ?? getDefaultScheme(ctx);

args.push(`--config=${config}`);

return args;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { CodeGenBase } from '../../interfaces';
export interface GenerateExecutorSchema extends CodeGenBase {} // eslint-disable-line
14 changes: 14 additions & 0 deletions packages/nx-graphql-codegen/src/executors/generate/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/schema",
"version": 2,
"title": "Generate executor",
"description": "",
"type": "object",
"properties": {
"config": {
"type": "string",
"description": "Specifies the path to the desired graphql code generator config file to be processed instead of the default path. Both absolute and relative paths are supported."
}
},
"required": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const CODEGEN_VERSION = '5.0.0';
export const GRAPQH_VERSION = '16.8.0';

export const CODEGEN_DEFAULT_DIR = '';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
schema:
- "<%= schema %>"
documents: []
overwrite: true
generates:
./src/graphql/interfaces/types.ts:
config:
scalars:
Date: Date
DateTime: Date
plugins:
- "typescript"
require: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
schema: ['<%= schema %>'],
documents: [],
overwrite: true,
generates: {
'./src/graphql/interfaces/types.ts': {
config: {
scalars: { Date: Date, DateTime: Date, Object: Object },
withHooks: true,
},
plugins: [
'typescript',
],
},
},
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
schema:
- <%= schema %>
documents: []
overwrite: true
generates:
./src/graphql/interfaces/types.ts:
config:
scalars:
Date: Date
DateTime: Date
plugins:
- "typescript"
require: []
Loading

0 comments on commit 2571045

Please sign in to comment.