@vantreeseba/graphql-mocks-codegen 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +70 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# @vantreeseba/graphql-mocks-codegen
|
|
2
|
+
|
|
3
|
+
A [GraphQL Code Generator](https://the-guild.dev/graphql/codegen) plugin that
|
|
4
|
+
emits a `SchemaTypeMap` — a `{ TypeName: GeneratedType }` map of every object type
|
|
5
|
+
in your schema — so you can hand it to
|
|
6
|
+
[`@vantreeseba/graphql-mocks`](../graphql-mocks)'s `buildMocks<TTypes>` and get
|
|
7
|
+
typed mock pools back without a cast.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install -D @vantreeseba/graphql-mocks-codegen
|
|
13
|
+
# peer deps you already have for codegen
|
|
14
|
+
npm install -D @graphql-codegen/cli @graphql-codegen/typescript
|
|
15
|
+
# the runtime the type map is used with
|
|
16
|
+
npm install @vantreeseba/graphql-mocks
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
Point the plugin at the same generated types file your `typescript` plugin (or
|
|
22
|
+
the `client` preset) emits — by default it imports from `./graphql`.
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// codegen.ts
|
|
26
|
+
import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
27
|
+
|
|
28
|
+
const config: CodegenConfig = {
|
|
29
|
+
schema: './schema.graphql',
|
|
30
|
+
generates: {
|
|
31
|
+
// your existing typed output
|
|
32
|
+
'./src/__generated__/graphql.ts': {
|
|
33
|
+
plugins: ['typescript'],
|
|
34
|
+
},
|
|
35
|
+
// the type map, emitted alongside it
|
|
36
|
+
'./src/__generated__/schema-type-map.ts': {
|
|
37
|
+
plugins: ['@vantreeseba/graphql-mocks-codegen'],
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export default config;
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Then type your mock pools straight off the generated map:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { buildMocks } from '@vantreeseba/graphql-mocks';
|
|
49
|
+
import type { SchemaTypeMap } from './__generated__/schema-type-map';
|
|
50
|
+
|
|
51
|
+
const mocks = buildMocks<SchemaTypeMap>(schema);
|
|
52
|
+
|
|
53
|
+
mocks.User; // User[] — typed, no cast
|
|
54
|
+
mocks.find('Todo', (t) => t.done); // Todo | undefined — predicate item typed
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Generated output
|
|
58
|
+
|
|
59
|
+
For a schema with `User` and `Todo` object types, the plugin emits:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import type * as Types from './graphql';
|
|
63
|
+
|
|
64
|
+
export type SchemaTypeMap = {
|
|
65
|
+
Todo: Types.Todo;
|
|
66
|
+
User: Types.User;
|
|
67
|
+
};
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The map KEY is the raw GraphQL type name (what the mock pools are keyed by at
|
|
71
|
+
runtime); the VALUE references the generated TS type via codegen's own naming
|
|
72
|
+
convention, so it stays correct even if you change `namingConvention`. Root
|
|
73
|
+
operation types (`Query`/`Mutation`/`Subscription`) and introspection types are
|
|
74
|
+
excluded; entries are sorted for deterministic output.
|
|
75
|
+
|
|
76
|
+
## Configuration
|
|
77
|
+
|
|
78
|
+
All options are optional strings:
|
|
79
|
+
|
|
80
|
+
| Option | Default | Description |
|
|
81
|
+
|---|---|---|
|
|
82
|
+
| `typeMapName` | `SchemaTypeMap` | Name of the generated type-map type. |
|
|
83
|
+
| `typesImportPath` | `./graphql` | Module the generated TS types are imported from. |
|
|
84
|
+
| `typesNamespace` | `Types` | Namespace alias the generated types are imported under. |
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
'./src/__generated__/schema-type-map.ts': {
|
|
88
|
+
plugins: ['@vantreeseba/graphql-mocks-codegen'],
|
|
89
|
+
config: { typeMapName: 'MockTypes', typesImportPath: '../gen/types' },
|
|
90
|
+
},
|
|
91
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A [GraphQL Code Generator](https://the-guild.dev/graphql/codegen) plugin that
|
|
3
|
+
* emits a `SchemaTypeMap` — a `{ TypeName: GeneratedType }` map of every object
|
|
4
|
+
* type in your schema — so you can hand it to
|
|
5
|
+
* [`@vantreeseba/graphql-mocks`](../graphql-mocks)'s `buildMocks<TTypes>` and get
|
|
6
|
+
* typed mock pools back without a cast:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { buildMocks } from '@vantreeseba/graphql-mocks';
|
|
10
|
+
* import type { SchemaTypeMap } from './__generated__/schema-type-map';
|
|
11
|
+
*
|
|
12
|
+
* const mocks = buildMocks<SchemaTypeMap>(schema);
|
|
13
|
+
* mocks.User; // User[] — typed, no cast
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* The map KEY is the raw GraphQL type name (what the mock pools are keyed by at
|
|
17
|
+
* runtime, e.g. `AWS_Address`); the VALUE references the generated TS type, whose
|
|
18
|
+
* name comes from codegen's own naming convention via `convertFactory`, so the
|
|
19
|
+
* references stay correct even if the naming convention changes.
|
|
20
|
+
*
|
|
21
|
+
* Run it pointed at the same generated types file your `typescript` plugin /
|
|
22
|
+
* `client` preset emits (default import path `./graphql`).
|
|
23
|
+
*
|
|
24
|
+
* @packageDocumentation
|
|
25
|
+
*/
|
|
26
|
+
import type { PluginFunction, PluginValidateFn } from '@graphql-codegen/plugin-helpers';
|
|
27
|
+
import { type NamingConvention } from '@graphql-codegen/visitor-plugin-common';
|
|
28
|
+
/** Configuration for the {@link plugin}. Every field has a sensible default. */
|
|
29
|
+
export interface GraphqlMocksPluginConfig {
|
|
30
|
+
/** Name of the generated type-map type. Default `SchemaTypeMap`. */
|
|
31
|
+
typeMapName?: string;
|
|
32
|
+
/** Module path the generated TS types are imported from. Default `./graphql`. */
|
|
33
|
+
typesImportPath?: string;
|
|
34
|
+
/** Namespace alias the generated types are imported under. Default `Types`. */
|
|
35
|
+
typesNamespace?: string;
|
|
36
|
+
/**
|
|
37
|
+
* The codegen naming convention used to derive generated TS type names from
|
|
38
|
+
* GraphQL type names. Set this to match the convention used by the `typescript`
|
|
39
|
+
* plugin / `client` preset producing `typesImportPath`. Passed straight to
|
|
40
|
+
* codegen's `convertFactory`.
|
|
41
|
+
*/
|
|
42
|
+
namingConvention?: NamingConvention;
|
|
43
|
+
}
|
|
44
|
+
export declare const plugin: PluginFunction<GraphqlMocksPluginConfig>;
|
|
45
|
+
export declare const validate: PluginValidateFn;
|
|
46
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACxF,OAAO,EAAE,KAAK,gBAAgB,EAAkB,MAAM,wCAAwC,CAAC;AAG/F,gFAAgF;AAChF,MAAM,WAAW,wBAAwB;IACvC,oEAAoE;IACpE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iFAAiF;IACjF,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,+EAA+E;IAC/E,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;CACrC;AA2BD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,wBAAwB,CAgB3D,CAAC;AAEF,eAAO,MAAM,QAAQ,EAAE,gBAOtB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A [GraphQL Code Generator](https://the-guild.dev/graphql/codegen) plugin that
|
|
3
|
+
* emits a `SchemaTypeMap` — a `{ TypeName: GeneratedType }` map of every object
|
|
4
|
+
* type in your schema — so you can hand it to
|
|
5
|
+
* [`@vantreeseba/graphql-mocks`](../graphql-mocks)'s `buildMocks<TTypes>` and get
|
|
6
|
+
* typed mock pools back without a cast:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { buildMocks } from '@vantreeseba/graphql-mocks';
|
|
10
|
+
* import type { SchemaTypeMap } from './__generated__/schema-type-map';
|
|
11
|
+
*
|
|
12
|
+
* const mocks = buildMocks<SchemaTypeMap>(schema);
|
|
13
|
+
* mocks.User; // User[] — typed, no cast
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* The map KEY is the raw GraphQL type name (what the mock pools are keyed by at
|
|
17
|
+
* runtime, e.g. `AWS_Address`); the VALUE references the generated TS type, whose
|
|
18
|
+
* name comes from codegen's own naming convention via `convertFactory`, so the
|
|
19
|
+
* references stay correct even if the naming convention changes.
|
|
20
|
+
*
|
|
21
|
+
* Run it pointed at the same generated types file your `typescript` plugin /
|
|
22
|
+
* `client` preset emits (default import path `./graphql`).
|
|
23
|
+
*
|
|
24
|
+
* @packageDocumentation
|
|
25
|
+
*/
|
|
26
|
+
import { convertFactory } from '@graphql-codegen/visitor-plugin-common';
|
|
27
|
+
import { isObjectType } from 'graphql';
|
|
28
|
+
const DEFAULTS = {
|
|
29
|
+
typeMapName: 'SchemaTypeMap',
|
|
30
|
+
typesImportPath: './graphql',
|
|
31
|
+
typesNamespace: 'Types',
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* The schema's mockable object type names: every object type, excluding the root
|
|
35
|
+
* operation types (Query/Mutation/Subscription) — which aren't mocked as pools —
|
|
36
|
+
* and introspection types (`__*`), sorted for deterministic output.
|
|
37
|
+
*/
|
|
38
|
+
function objectTypeNames(schema) {
|
|
39
|
+
const roots = new Set([schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]
|
|
40
|
+
.filter((type) => type != null)
|
|
41
|
+
.map((type) => type.name));
|
|
42
|
+
return Object.values(schema.getTypeMap())
|
|
43
|
+
.filter((type) => isObjectType(type) && !type.name.startsWith('__') && !roots.has(type.name))
|
|
44
|
+
.map((type) => type.name)
|
|
45
|
+
.sort();
|
|
46
|
+
}
|
|
47
|
+
export const plugin = (schema, _documents, config) => {
|
|
48
|
+
const opts = { ...DEFAULTS, ...config };
|
|
49
|
+
// `convertFactory` maps a GraphQL type name to its generated TS type name using
|
|
50
|
+
// codegen's own naming convention (read from `config`), so we don't hard-code it.
|
|
51
|
+
const convert = convertFactory(config ?? {});
|
|
52
|
+
const names = objectTypeNames(schema);
|
|
53
|
+
const entries = names
|
|
54
|
+
.map((name) => ` ${name}: ${opts.typesNamespace}.${convert(name)};`)
|
|
55
|
+
.join('\n');
|
|
56
|
+
const body = entries ? `{\n${entries}\n}` : '{}';
|
|
57
|
+
return {
|
|
58
|
+
prepend: [`import type * as ${opts.typesNamespace} from '${opts.typesImportPath}';`],
|
|
59
|
+
content: `export type ${opts.typeMapName} = ${body};\n`,
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
export const validate = async (_schema, _documents, config) => {
|
|
63
|
+
for (const key of ['typeMapName', 'typesImportPath', 'typesNamespace']) {
|
|
64
|
+
const value = config?.[key];
|
|
65
|
+
if (value !== undefined && typeof value !== 'string') {
|
|
66
|
+
throw new Error(`graphql-mocks-codegen: config option \`${key}\` must be a string.`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;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KAAK,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;IAEtC,MAAM,OAAO,GAAG,KAAK;SAClB,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,IAAI,KAAK,IAAI,CAAC,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC;SACpE,IAAI,CAAC,IAAI,CAAC,CAAC;IACd,MAAM,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC,MAAM,OAAO,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;IAEjD,OAAO;QACL,OAAO,EAAE,CAAC,oBAAoB,IAAI,CAAC,cAAc,UAAU,IAAI,CAAC,eAAe,IAAI,CAAC;QACpF,OAAO,EAAE,eAAe,IAAI,CAAC,WAAW,MAAM,IAAI,KAAK;KACxD,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,QAAQ,GAAqB,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,EAAE;IAC9E,KAAK,MAAM,GAAG,IAAI,CAAC,aAAa,EAAE,iBAAiB,EAAE,gBAAgB,CAAU,EAAE,CAAC;QAChF,MAAM,KAAK,GAAG,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC;QAC5B,IAAI,KAAK,KAAK,SAAS,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YACrD,MAAM,IAAI,KAAK,CAAC,0CAA0C,GAAG,sBAAsB,CAAC,CAAC;QACvF,CAAC;IACH,CAAC;AACH,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vantreeseba/graphql-mocks-codegen",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "GraphQL Code Generator plugin that emits a SchemaTypeMap for typing @vantreeseba/graphql-mocks pools.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"author": "Benjamin Van Treese <vantreeseba@gmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/vantreeseba/graphql-mocks#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/vantreeseba/graphql-mocks.git",
|
|
12
|
+
"directory": "packages/graphql-mocks-codegen"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/vantreeseba/graphql-mocks/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"graphql",
|
|
19
|
+
"graphql-codegen",
|
|
20
|
+
"graphql-code-generator",
|
|
21
|
+
"codegen",
|
|
22
|
+
"plugin",
|
|
23
|
+
"mocks",
|
|
24
|
+
"typescript"
|
|
25
|
+
],
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsc",
|
|
40
|
+
"typecheck": "tsc --noEmit",
|
|
41
|
+
"typecheck:tests": "tsc -p tsconfig.tests.json",
|
|
42
|
+
"test": "vitest run",
|
|
43
|
+
"test:watch": "vitest",
|
|
44
|
+
"coverage": "vitest run --coverage"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@graphql-codegen/visitor-plugin-common": "^5.8.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@graphql-codegen/plugin-helpers": ">=5",
|
|
51
|
+
"graphql": ">=16"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@graphql-codegen/plugin-helpers": "^5.1.1",
|
|
55
|
+
"graphql": "^16.14.0"
|
|
56
|
+
}
|
|
57
|
+
}
|