@newmo/graphql-codegen-fake-server-client 0.9.0 → 0.9.7
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 +4 -0
- package/dist/config.js +13 -0
- package/dist/convertName.js +15 -0
- package/dist/graphql-codegen-fake-server-client.js +11 -9
- package/package.json +5 -4
- package/src/config.ts +41 -0
- package/src/convertName.ts +14 -0
- package/src/graphql-codegen-fake-server-client.ts +16 -23
package/README.md
CHANGED
|
@@ -80,6 +80,10 @@ it("register fake response for query", async () => {
|
|
|
80
80
|
|
|
81
81
|
- `typesFile` (required): Path to the generated client's graphql file.
|
|
82
82
|
- `fakeServerEndpoint` (optional): Fake server endpoint. Default is `http://127.0.0.1:4000/fake`.
|
|
83
|
+
- `namingConvention` (optional): Naming convention for the generated types. Default is `change-case#pascalCase`.
|
|
84
|
+
- [Naming Convention](https://the-guild.dev/graphql/codegen/docs/config-reference/naming-convention)
|
|
85
|
+
- `typesPrefix` (optional): Prefix for the generated types.
|
|
86
|
+
- `typesSuffix` (optional): Suffix for the generated types.
|
|
83
87
|
|
|
84
88
|
## License
|
|
85
89
|
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeConfig = void 0;
|
|
4
|
+
function normalizeConfig(rawConfig) {
|
|
5
|
+
return {
|
|
6
|
+
typesFile: rawConfig.typesFile,
|
|
7
|
+
fakeServerEndpoint: rawConfig.fakeServerEndpoint ?? "http://127.0.0.1:4000/fake",
|
|
8
|
+
typesPrefix: rawConfig.typesPrefix ?? "",
|
|
9
|
+
typesSuffix: rawConfig.typesSuffix ?? "",
|
|
10
|
+
namingConvention: rawConfig.namingConvention ?? "",
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
exports.normalizeConfig = normalizeConfig;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertName = void 0;
|
|
4
|
+
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
5
|
+
function convertName(node, config) {
|
|
6
|
+
const convert = config.namingConvention
|
|
7
|
+
? (0, visitor_plugin_common_1.convertFactory)({ namingConvention: config.namingConvention })
|
|
8
|
+
: (0, visitor_plugin_common_1.convertFactory)({});
|
|
9
|
+
let convertedName = "";
|
|
10
|
+
convertedName += config.typesPrefix;
|
|
11
|
+
convertedName += convert(node);
|
|
12
|
+
convertedName += config.typesSuffix;
|
|
13
|
+
return convertedName;
|
|
14
|
+
}
|
|
15
|
+
exports.convertName = convertName;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const config_1 = require("./config");
|
|
4
|
+
const convertName_1 = require("./convertName");
|
|
3
5
|
const plugin = {
|
|
4
|
-
plugin(schema, documents,
|
|
5
|
-
|
|
6
|
-
const fakeEndpoint = config.fakeServerEndpoint
|
|
6
|
+
plugin(schema, documents, rawConfig, _info) {
|
|
7
|
+
const config = (0, config_1.normalizeConfig)(rawConfig);
|
|
8
|
+
const fakeEndpoint = config.fakeServerEndpoint;
|
|
7
9
|
const registerOperationResponseType = "{ ok: true } | { ok: false; errors: string[] }";
|
|
8
10
|
const generateRegisterOperation = (name) => {
|
|
9
11
|
return `export async function register${name}QueryResponse(sequenceId:string, queryResponse: ${name}Query): Promise<${registerOperationResponseType}> {
|
|
@@ -72,10 +74,10 @@ const plugin = {
|
|
|
72
74
|
}`;
|
|
73
75
|
};
|
|
74
76
|
const importQueryIdentifierName = (documentName) => {
|
|
75
|
-
return `import type { ${documentName}Query } from '${config.typesFile}';`;
|
|
77
|
+
return `import type { ${(0, convertName_1.convertName)(documentName, config)}Query } from '${config.typesFile}';`;
|
|
76
78
|
};
|
|
77
79
|
const importMutationIdentifierName = (documentName) => {
|
|
78
|
-
return `import type { ${documentName}Mutation } from '${config.typesFile}';`;
|
|
80
|
+
return `import type { ${(0, convertName_1.convertName)(documentName, config)}Mutation } from '${config.typesFile}';`;
|
|
79
81
|
};
|
|
80
82
|
return `/* eslint-disable */
|
|
81
83
|
// This file was generated by a @newmo/graphql-codegen-fake-server-operation
|
|
@@ -105,16 +107,16 @@ ${documents
|
|
|
105
107
|
definition.operation === "query" &&
|
|
106
108
|
definition.name) {
|
|
107
109
|
return [
|
|
108
|
-
generateRegisterOperation(definition.name.value),
|
|
109
|
-
generateRegisterOperationError(definition.name.value),
|
|
110
|
+
generateRegisterOperation((0, convertName_1.convertName)(definition.name.value, config)),
|
|
111
|
+
generateRegisterOperationError((0, convertName_1.convertName)(definition.name.value, config)),
|
|
110
112
|
];
|
|
111
113
|
}
|
|
112
114
|
if (definition.kind === "OperationDefinition" &&
|
|
113
115
|
definition.operation === "mutation" &&
|
|
114
116
|
definition.name) {
|
|
115
117
|
return [
|
|
116
|
-
generateRegisterMutation(definition.name.value),
|
|
117
|
-
generateRegisterMutationError(definition.name.value),
|
|
118
|
+
generateRegisterMutation((0, convertName_1.convertName)(definition.name.value, config)),
|
|
119
|
+
generateRegisterMutationError((0, convertName_1.convertName)(definition.name.value, config)),
|
|
118
120
|
];
|
|
119
121
|
}
|
|
120
122
|
return [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@newmo/graphql-codegen-fake-server-client",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.7",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "GraphQL Codegen plugin for generating a fake server client",
|
|
6
6
|
"keywords": [
|
|
@@ -32,11 +32,12 @@
|
|
|
32
32
|
"prepare": "npm run build",
|
|
33
33
|
"build": "tsc",
|
|
34
34
|
"lint": "tsc --noEmit",
|
|
35
|
-
"test": "npm run build && npm run
|
|
35
|
+
"test": "npm run build && npm run codegen",
|
|
36
36
|
"codegen": "graphql-codegen --config graphql-codegen.ts"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@graphql-codegen/plugin-helpers": "^5.0.3"
|
|
39
|
+
"@graphql-codegen/plugin-helpers": "^5.0.3",
|
|
40
|
+
"@graphql-codegen/visitor-plugin-common": "^5.2.0"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
42
43
|
"@graphql-codegen/cli": "^5.0.0",
|
|
@@ -58,5 +59,5 @@
|
|
|
58
59
|
"access": "public",
|
|
59
60
|
"registry": "https://registry.npmjs.org/"
|
|
60
61
|
},
|
|
61
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "35f4b493d4d352571fcd9b224df5d42c155fdf55"
|
|
62
63
|
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { RawTypesConfig } from "@graphql-codegen/visitor-plugin-common";
|
|
2
|
+
|
|
3
|
+
export type RawPluginConfig = {
|
|
4
|
+
/**
|
|
5
|
+
* The path to the generated types file
|
|
6
|
+
* @example
|
|
7
|
+
* typeFile: "./graphql.ts"
|
|
8
|
+
**/
|
|
9
|
+
typesFile: string;
|
|
10
|
+
/**
|
|
11
|
+
* The URL of the fake server
|
|
12
|
+
* Default: 'http://127.0.0.1:4000/fake'
|
|
13
|
+
*/
|
|
14
|
+
fakeServerEndpoint: string;
|
|
15
|
+
/**
|
|
16
|
+
* Naming convention for the generated types
|
|
17
|
+
* GraphQL Codegen Plugin Common Options
|
|
18
|
+
**/
|
|
19
|
+
namingConvention?: RawTypesConfig["namingConvention"];
|
|
20
|
+
/**
|
|
21
|
+
* Prefix for the generated types
|
|
22
|
+
* GraphQL Codegen Plugin Common Options
|
|
23
|
+
**/
|
|
24
|
+
typesPrefix?: RawTypesConfig["typesPrefix"];
|
|
25
|
+
/**
|
|
26
|
+
* Suffix for the generated types
|
|
27
|
+
* GraphQL Codegen Plugin Common Options
|
|
28
|
+
**/
|
|
29
|
+
typesSuffix?: RawTypesConfig["typesSuffix"];
|
|
30
|
+
};
|
|
31
|
+
export type PluginConfig = Required<RawPluginConfig>;
|
|
32
|
+
|
|
33
|
+
export function normalizeConfig(rawConfig: RawPluginConfig): PluginConfig {
|
|
34
|
+
return {
|
|
35
|
+
typesFile: rawConfig.typesFile,
|
|
36
|
+
fakeServerEndpoint: rawConfig.fakeServerEndpoint ?? "http://127.0.0.1:4000/fake",
|
|
37
|
+
typesPrefix: rawConfig.typesPrefix ?? "",
|
|
38
|
+
typesSuffix: rawConfig.typesSuffix ?? "",
|
|
39
|
+
namingConvention: rawConfig.namingConvention ?? "",
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { convertFactory } from "@graphql-codegen/visitor-plugin-common";
|
|
2
|
+
import type { ASTNode } from "graphql/index.js";
|
|
3
|
+
import type { PluginConfig } from "./config";
|
|
4
|
+
|
|
5
|
+
export function convertName(node: ASTNode | string, config: PluginConfig): string {
|
|
6
|
+
const convert = config.namingConvention
|
|
7
|
+
? convertFactory({ namingConvention: config.namingConvention })
|
|
8
|
+
: convertFactory({});
|
|
9
|
+
let convertedName = "";
|
|
10
|
+
convertedName += config.typesPrefix;
|
|
11
|
+
convertedName += convert(node);
|
|
12
|
+
convertedName += config.typesSuffix;
|
|
13
|
+
return convertedName;
|
|
14
|
+
}
|
|
@@ -1,22 +1,11 @@
|
|
|
1
1
|
import type { CodegenPlugin } from "@graphql-codegen/plugin-helpers";
|
|
2
|
+
import { type RawPluginConfig, normalizeConfig } from "./config";
|
|
3
|
+
import { convertName } from "./convertName";
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* typeFile: "./graphql.ts"
|
|
8
|
-
**/
|
|
9
|
-
typesFile: string;
|
|
10
|
-
/**
|
|
11
|
-
* The URL of the fake server
|
|
12
|
-
* Default: 'http://127.0.0.1:4000/fake'
|
|
13
|
-
*/
|
|
14
|
-
fakeServerEndpoint: string;
|
|
15
|
-
};
|
|
16
|
-
const plugin: CodegenPlugin<PluginConfig> = {
|
|
17
|
-
plugin(schema, documents, config, _info) {
|
|
18
|
-
console.log(config);
|
|
19
|
-
const fakeEndpoint = config.fakeServerEndpoint || "http://127.0.0.1:4000/fake";
|
|
5
|
+
const plugin: CodegenPlugin<RawPluginConfig> = {
|
|
6
|
+
plugin(schema, documents, rawConfig, _info) {
|
|
7
|
+
const config = normalizeConfig(rawConfig);
|
|
8
|
+
const fakeEndpoint = config.fakeServerEndpoint;
|
|
20
9
|
const registerOperationResponseType = "{ ok: true } | { ok: false; errors: string[] }";
|
|
21
10
|
const generateRegisterOperation = (name: string) => {
|
|
22
11
|
return `export async function register${name}QueryResponse(sequenceId:string, queryResponse: ${name}Query): Promise<${registerOperationResponseType}> {
|
|
@@ -85,10 +74,14 @@ const plugin: CodegenPlugin<PluginConfig> = {
|
|
|
85
74
|
}`;
|
|
86
75
|
};
|
|
87
76
|
const importQueryIdentifierName = (documentName: string) => {
|
|
88
|
-
return `import type { ${documentName}Query } from '${
|
|
77
|
+
return `import type { ${convertName(documentName, config)}Query } from '${
|
|
78
|
+
config.typesFile
|
|
79
|
+
}';`;
|
|
89
80
|
};
|
|
90
81
|
const importMutationIdentifierName = (documentName: string) => {
|
|
91
|
-
return `import type { ${documentName}Mutation } from '${
|
|
82
|
+
return `import type { ${convertName(documentName, config)}Mutation } from '${
|
|
83
|
+
config.typesFile
|
|
84
|
+
}';`;
|
|
92
85
|
};
|
|
93
86
|
return `/* eslint-disable */
|
|
94
87
|
// This file was generated by a @newmo/graphql-codegen-fake-server-operation
|
|
@@ -124,8 +117,8 @@ ${documents
|
|
|
124
117
|
definition.name
|
|
125
118
|
) {
|
|
126
119
|
return [
|
|
127
|
-
generateRegisterOperation(definition.name.value),
|
|
128
|
-
generateRegisterOperationError(definition.name.value),
|
|
120
|
+
generateRegisterOperation(convertName(definition.name.value, config)),
|
|
121
|
+
generateRegisterOperationError(convertName(definition.name.value, config)),
|
|
129
122
|
];
|
|
130
123
|
}
|
|
131
124
|
if (
|
|
@@ -134,8 +127,8 @@ ${documents
|
|
|
134
127
|
definition.name
|
|
135
128
|
) {
|
|
136
129
|
return [
|
|
137
|
-
generateRegisterMutation(definition.name.value),
|
|
138
|
-
generateRegisterMutationError(definition.name.value),
|
|
130
|
+
generateRegisterMutation(convertName(definition.name.value, config)),
|
|
131
|
+
generateRegisterMutationError(convertName(definition.name.value, config)),
|
|
139
132
|
];
|
|
140
133
|
}
|
|
141
134
|
return [];
|