@vantreeseba/graphql-mocks-codegen 2.1.0 → 2.1.1
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/dist/cjs/index.js +74 -0
- package/dist/cjs/package.json +3 -0
- package/package.json +8 -3
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
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
|
|
5
|
+
* type 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
|
+
* ```ts
|
|
10
|
+
* import { buildMocks } from '@vantreeseba/graphql-mocks';
|
|
11
|
+
* import type { SchemaTypeMap } from './__generated__/schema-type-map';
|
|
12
|
+
*
|
|
13
|
+
* const mocks = buildMocks<SchemaTypeMap>(schema);
|
|
14
|
+
* mocks.User; // User[] — typed, no cast
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* The map KEY is the raw GraphQL type name (what the mock pools are keyed by at
|
|
18
|
+
* runtime, e.g. `AWS_Address`); the VALUE references the generated TS type, whose
|
|
19
|
+
* name comes from codegen's own naming convention via `convertFactory`, so the
|
|
20
|
+
* references stay correct even if the naming convention changes.
|
|
21
|
+
*
|
|
22
|
+
* Run it pointed at the same generated types file your `typescript` plugin /
|
|
23
|
+
* `client` preset emits (default import path `./graphql`).
|
|
24
|
+
*
|
|
25
|
+
* @packageDocumentation
|
|
26
|
+
*/
|
|
27
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
+
exports.validate = exports.plugin = void 0;
|
|
29
|
+
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
30
|
+
const graphql_1 = require("graphql");
|
|
31
|
+
const DEFAULTS = {
|
|
32
|
+
typeMapName: 'SchemaTypeMap',
|
|
33
|
+
typesImportPath: './graphql',
|
|
34
|
+
typesNamespace: 'Types',
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* The schema's mockable object type names: every object type, excluding the root
|
|
38
|
+
* operation types (Query/Mutation/Subscription) — which aren't mocked as pools —
|
|
39
|
+
* and introspection types (`__*`), sorted for deterministic output.
|
|
40
|
+
*/
|
|
41
|
+
function objectTypeNames(schema) {
|
|
42
|
+
const roots = new Set([schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]
|
|
43
|
+
.filter((type) => type != null)
|
|
44
|
+
.map((type) => type.name));
|
|
45
|
+
return Object.values(schema.getTypeMap())
|
|
46
|
+
.filter((type) => (0, graphql_1.isObjectType)(type) && !type.name.startsWith('__') && !roots.has(type.name))
|
|
47
|
+
.map((type) => type.name)
|
|
48
|
+
.sort();
|
|
49
|
+
}
|
|
50
|
+
const plugin = (schema, _documents, config) => {
|
|
51
|
+
const opts = { ...DEFAULTS, ...config };
|
|
52
|
+
// `convertFactory` maps a GraphQL type name to its generated TS type name using
|
|
53
|
+
// codegen's own naming convention (read from `config`), so we don't hard-code it.
|
|
54
|
+
const convert = (0, visitor_plugin_common_1.convertFactory)(config ?? {});
|
|
55
|
+
const names = objectTypeNames(schema);
|
|
56
|
+
const entries = names
|
|
57
|
+
.map((name) => ` ${name}: ${opts.typesNamespace}.${convert(name)};`)
|
|
58
|
+
.join('\n');
|
|
59
|
+
const body = entries ? `{\n${entries}\n}` : '{}';
|
|
60
|
+
return {
|
|
61
|
+
prepend: [`import type * as ${opts.typesNamespace} from '${opts.typesImportPath}';`],
|
|
62
|
+
content: `export type ${opts.typeMapName} = ${body};\n`,
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
exports.plugin = plugin;
|
|
66
|
+
const validate = async (_schema, _documents, config) => {
|
|
67
|
+
for (const key of ['typeMapName', 'typesImportPath', 'typesNamespace']) {
|
|
68
|
+
const value = config?.[key];
|
|
69
|
+
if (value !== undefined && typeof value !== 'string') {
|
|
70
|
+
throw new Error(`graphql-mocks-codegen: config option \`${key}\` must be a string.`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
exports.validate = validate;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vantreeseba/graphql-mocks-codegen",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "GraphQL Code Generator plugin that emits a SchemaTypeMap for typing @vantreeseba/graphql-mocks pools.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Benjamin Van Treese <vantreeseba@gmail.com>",
|
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
"exports": {
|
|
27
27
|
".": {
|
|
28
28
|
"types": "./dist/index.d.ts",
|
|
29
|
-
"import": "./dist/index.js"
|
|
29
|
+
"import": "./dist/index.js",
|
|
30
|
+
"require": "./dist/cjs/index.js"
|
|
30
31
|
}
|
|
31
32
|
},
|
|
32
33
|
"files": [
|
|
@@ -36,7 +37,9 @@
|
|
|
36
37
|
"access": "public"
|
|
37
38
|
},
|
|
38
39
|
"scripts": {
|
|
39
|
-
"build": "
|
|
40
|
+
"build": "npm run build:esm && npm run build:cjs",
|
|
41
|
+
"build:esm": "tsc",
|
|
42
|
+
"build:cjs": "tsc -p tsconfig.cjs.json && node scripts/finalize-cjs.mjs",
|
|
40
43
|
"typecheck": "tsc --noEmit",
|
|
41
44
|
"typecheck:tests": "tsc -p tsconfig.tests.json",
|
|
42
45
|
"test": "vitest run",
|
|
@@ -51,7 +54,9 @@
|
|
|
51
54
|
"graphql": ">=16"
|
|
52
55
|
},
|
|
53
56
|
"devDependencies": {
|
|
57
|
+
"@graphql-codegen/cli": "^5.0.0",
|
|
54
58
|
"@graphql-codegen/plugin-helpers": "^5.1.1",
|
|
59
|
+
"@graphql-codegen/typescript": "^4.0.0",
|
|
55
60
|
"graphql": "^16.14.0"
|
|
56
61
|
}
|
|
57
62
|
}
|