-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx-graphql-codegen): adding new plugin to graphql code generate …
…support
- Loading branch information
Rene Bonilla
committed
Oct 13, 2023
1 parent
daf620c
commit 2571045
Showing
29 changed files
with
668 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/nx-graphql-codegen/src/executors/generate/executor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
packages/nx-graphql-codegen/src/executors/generate/executor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
2 changes: 2 additions & 0 deletions
2
packages/nx-graphql-codegen/src/executors/generate/schema.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
packages/nx-graphql-codegen/src/executors/generate/schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/nx-graphql-codegen/src/generators/configuration/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ''; |
13 changes: 13 additions & 0 deletions
13
packages/nx-graphql-codegen/src/generators/configuration/files/codegen.xml.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
20 changes: 20 additions & 0 deletions
20
packages/nx-graphql-codegen/src/generators/configuration/files/ts/codegen.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
13 changes: 13 additions & 0 deletions
13
packages/nx-graphql-codegen/src/generators/configuration/files/yml/codegen.yml.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
Oops, something went wrong.