@newmo/graphql-codegen-fake-server-client 0.4.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/LICENSE +21 -0
- package/README.md +83 -0
- package/dist/graphql-codegen-fake-server-client.js +128 -0
- package/package.json +62 -0
- package/src/graphql-codegen-fake-server-client.ts +149 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 newmo, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# @newmo/graphql-codegen-fake-server-client
|
|
2
|
+
|
|
3
|
+
GraphQL Code Generator plugin for generating a fake server client.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm add --save-dev @newmo/graphql-codegen-fake-server-client
|
|
9
|
+
# This plugin depends on @graphql-codegen/client-preset
|
|
10
|
+
npm install @graphql-codegen/cli @graphql-codegen/client-preset --save-dev
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
GraphQL Code Generator configuration:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import type { CodegenConfig } from "@graphql-codegen/cli";
|
|
17
|
+
|
|
18
|
+
const config: CodegenConfig = {
|
|
19
|
+
overwrite: true,
|
|
20
|
+
schema: "./api/graphql/api.graphqls",
|
|
21
|
+
documents: "./api/graphql/query.graphql",
|
|
22
|
+
generates: {
|
|
23
|
+
"./generated/": {
|
|
24
|
+
preset: "client"
|
|
25
|
+
},
|
|
26
|
+
"./generated/fake-client.ts": {
|
|
27
|
+
plugins: ["@newmo/graphql-codegen-fake-server-client"],
|
|
28
|
+
config: {
|
|
29
|
+
// Required: path to the generated client's graphql file
|
|
30
|
+
typesFile: "./graphql"
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export default config;
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
You can use `./generated/fake-client.ts` to register the fake to the fake server.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { registerFake } from "./generated/fake-client";
|
|
43
|
+
it("register fake response for query", async () => {
|
|
44
|
+
const sequenceId = crypto.randomUUID();
|
|
45
|
+
// register fake response for GetBooks query
|
|
46
|
+
const resRegister = await registerGetBooksQueryResponse(sequenceId, {
|
|
47
|
+
books: [
|
|
48
|
+
{
|
|
49
|
+
id: "new id",
|
|
50
|
+
title: "new title",
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
});
|
|
54
|
+
expect(resRegister).toMatchInlineSnapshot(`"{"ok":true}"`);
|
|
55
|
+
// request to server
|
|
56
|
+
const client = new GraphQLClient(`${fakeServerUrl}/graphql`, {
|
|
57
|
+
headers: {
|
|
58
|
+
"sequence-id": sequenceId,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
// Got fake response
|
|
62
|
+
const response = await client.request(GetBooksDocument);
|
|
63
|
+
expect(response).toMatchInlineSnapshot(`
|
|
64
|
+
{
|
|
65
|
+
"books": [
|
|
66
|
+
{
|
|
67
|
+
"id": "new id",
|
|
68
|
+
"title": "new title",
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
}
|
|
72
|
+
`);
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Options
|
|
77
|
+
|
|
78
|
+
- `typesFile` (required): Path to the generated client's graphql file.
|
|
79
|
+
- `fakeServerEndpoint` (optional): Fake server endpoint. Default is `http://127.0.0.1:4000/fake`.
|
|
80
|
+
|
|
81
|
+
## License
|
|
82
|
+
|
|
83
|
+
MIT
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const plugin = {
|
|
4
|
+
plugin(schema, documents, config, _info) {
|
|
5
|
+
console.log(config);
|
|
6
|
+
const fakeEndpoint = config.fakeServerEndpoint || "http://127.0.0.1:4000/fake";
|
|
7
|
+
const registerOperationResponseType = "{ ok: true } | { ok: false; errors: string[] }";
|
|
8
|
+
const generateRegisterOperation = (name) => {
|
|
9
|
+
return `export async function register${name}QueryResponse(sequenceId:string, queryResponse: ${name}Query): Promise<${registerOperationResponseType}> {
|
|
10
|
+
return await fetch('${fakeEndpoint}', {
|
|
11
|
+
method: 'POST',
|
|
12
|
+
headers: {
|
|
13
|
+
'Content-Type': 'application/json',
|
|
14
|
+
'sequence-id': sequenceId
|
|
15
|
+
},
|
|
16
|
+
body: JSON.stringify({
|
|
17
|
+
type: "operation",
|
|
18
|
+
operationName: "${name}",
|
|
19
|
+
data: queryResponse
|
|
20
|
+
}),
|
|
21
|
+
}).then((res) => res.json()) as ${registerOperationResponseType};
|
|
22
|
+
}`;
|
|
23
|
+
};
|
|
24
|
+
const generateRegisterOperationError = (name) => {
|
|
25
|
+
return `export async function register${name}QueryErrorResponse(sequenceId:string, { errors, responseStatusCode }: { errors: Record<string, unknown>[]; responseStatusCode: number }): Promise<${registerOperationResponseType}> {
|
|
26
|
+
return await fetch('${fakeEndpoint}', {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
headers: {
|
|
29
|
+
'Content-Type': 'application/json',
|
|
30
|
+
'sequence-id': sequenceId
|
|
31
|
+
},
|
|
32
|
+
body: JSON.stringify({
|
|
33
|
+
type: "network-error",
|
|
34
|
+
operationName: "${name}",
|
|
35
|
+
responseStatusCode,
|
|
36
|
+
errors
|
|
37
|
+
}),
|
|
38
|
+
}).then((res) => res.json()) as ${registerOperationResponseType};
|
|
39
|
+
}`;
|
|
40
|
+
};
|
|
41
|
+
const generateRegisterMutation = (name) => {
|
|
42
|
+
return `export async function register${name}MutationResponse(sequenceId:string, mutationResponse: ${name}Mutation): Promise<${registerOperationResponseType}> {
|
|
43
|
+
return await fetch('${fakeEndpoint}', {
|
|
44
|
+
method: 'POST',
|
|
45
|
+
headers: {
|
|
46
|
+
'Content-Type': 'application/json',
|
|
47
|
+
'sequence-id': sequenceId
|
|
48
|
+
},
|
|
49
|
+
body: JSON.stringify({
|
|
50
|
+
type: "operation",
|
|
51
|
+
operationName: "${name}",
|
|
52
|
+
data: mutationResponse
|
|
53
|
+
}),
|
|
54
|
+
}).then((res) => res.json()) as ${registerOperationResponseType};
|
|
55
|
+
}`;
|
|
56
|
+
};
|
|
57
|
+
const generateRegisterMutationError = (name) => {
|
|
58
|
+
return `export async function register${name}MutationErrorResponse(sequenceId:string, { errors, responseStatusCode }: { errors: Record<string, unknown>[]; responseStatusCode: number }): Promise<${registerOperationResponseType}> {
|
|
59
|
+
return await fetch('${fakeEndpoint}', {
|
|
60
|
+
method: 'POST',
|
|
61
|
+
headers: {
|
|
62
|
+
'Content-Type': 'application/json',
|
|
63
|
+
'sequence-id': sequenceId
|
|
64
|
+
},
|
|
65
|
+
body: JSON.stringify({
|
|
66
|
+
type: "network-error",
|
|
67
|
+
operationName: "${name}",
|
|
68
|
+
responseStatusCode,
|
|
69
|
+
errors
|
|
70
|
+
}),
|
|
71
|
+
}).then((res) => res.json()) as ${registerOperationResponseType};
|
|
72
|
+
}`;
|
|
73
|
+
};
|
|
74
|
+
const importQueryIdentifierName = (documentName) => {
|
|
75
|
+
return `import type { ${documentName}Query } from '${config.typesFile}';`;
|
|
76
|
+
};
|
|
77
|
+
const importMutationIdentifierName = (documentName) => {
|
|
78
|
+
return `import type { ${documentName}Mutation } from '${config.typesFile}';`;
|
|
79
|
+
};
|
|
80
|
+
return `/* eslint-disable */
|
|
81
|
+
// This file was generated by a @newmo/graphql-codegen-fake-server-operation
|
|
82
|
+
${documents
|
|
83
|
+
.flatMap((document) => {
|
|
84
|
+
return document.document?.definitions?.map((definition) => {
|
|
85
|
+
// query
|
|
86
|
+
if (definition.kind === "OperationDefinition" &&
|
|
87
|
+
definition.operation === "query" &&
|
|
88
|
+
definition.name) {
|
|
89
|
+
return importQueryIdentifierName(definition.name.value);
|
|
90
|
+
}
|
|
91
|
+
if (definition.kind === "OperationDefinition" &&
|
|
92
|
+
definition.operation === "mutation" &&
|
|
93
|
+
definition.name) {
|
|
94
|
+
return importMutationIdentifierName(definition.name.value);
|
|
95
|
+
}
|
|
96
|
+
return [];
|
|
97
|
+
});
|
|
98
|
+
})
|
|
99
|
+
.join("\n")}
|
|
100
|
+
${documents
|
|
101
|
+
.flatMap((document) => {
|
|
102
|
+
return document.document?.definitions?.flatMap((definition) => {
|
|
103
|
+
// query
|
|
104
|
+
if (definition.kind === "OperationDefinition" &&
|
|
105
|
+
definition.operation === "query" &&
|
|
106
|
+
definition.name) {
|
|
107
|
+
return [
|
|
108
|
+
generateRegisterOperation(definition.name.value),
|
|
109
|
+
generateRegisterOperationError(definition.name.value),
|
|
110
|
+
];
|
|
111
|
+
}
|
|
112
|
+
if (definition.kind === "OperationDefinition" &&
|
|
113
|
+
definition.operation === "mutation" &&
|
|
114
|
+
definition.name) {
|
|
115
|
+
return [
|
|
116
|
+
generateRegisterMutation(definition.name.value),
|
|
117
|
+
generateRegisterMutationError(definition.name.value),
|
|
118
|
+
];
|
|
119
|
+
}
|
|
120
|
+
return [];
|
|
121
|
+
});
|
|
122
|
+
})
|
|
123
|
+
.join("\n")}
|
|
124
|
+
`;
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
// GraphQL Codegen Plugin requires CommonJS export
|
|
128
|
+
module.exports = plugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@newmo/graphql-codegen-fake-server-client",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "GraphQL Codegen plugin for generating a fake server client",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"testing",
|
|
8
|
+
"mock",
|
|
9
|
+
"graphql"
|
|
10
|
+
],
|
|
11
|
+
"repository": "https://github.com/newmo-oss/graphql-fake-server.git",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "newmo, Inc.",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"type": "commonjs",
|
|
16
|
+
"main": "./dist/graphql-codegen-fake-server-client.js",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": "./dist/graphql-codegen-fake-server-client.js",
|
|
20
|
+
"require": "./dist/graphql-codegen-fake-server-client.js",
|
|
21
|
+
"default": "./dist/graphql-codegen-fake-server-client.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"src/",
|
|
26
|
+
"!src/test",
|
|
27
|
+
"!src/**/*.test.ts",
|
|
28
|
+
"!src/**/__snapshots__",
|
|
29
|
+
"dist/"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"prepare": "npm run build",
|
|
33
|
+
"build": "tsc",
|
|
34
|
+
"lint": "tsc --noEmit",
|
|
35
|
+
"test": "npm run build && npm run codege",
|
|
36
|
+
"codegen": "graphql-codegen --config graphql-codegen.ts"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@graphql-codegen/plugin-helpers": "^5.0.3"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@graphql-codegen/cli": "^5.0.0",
|
|
43
|
+
"@graphql-codegen/typescript": "^4.0.1",
|
|
44
|
+
"@tsconfig/node18": "^18.2.0",
|
|
45
|
+
"@tsconfig/strictest": "^2.0.1",
|
|
46
|
+
"@types/eslint": "^8.44.2",
|
|
47
|
+
"@types/node": "^20.5.1",
|
|
48
|
+
"graphql": "^16.8.1",
|
|
49
|
+
"typescript": "^5.4.2"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"graphql": "^16.8.1"
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=18.0.0"
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public",
|
|
59
|
+
"registry": "https://registry.npmjs.org/"
|
|
60
|
+
},
|
|
61
|
+
"gitHead": "bab6c38ebdeffb527923f72106da19456e753ecb"
|
|
62
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { CodegenPlugin } from "@graphql-codegen/plugin-helpers";
|
|
2
|
+
|
|
3
|
+
export type PluginConfig = {
|
|
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
|
+
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";
|
|
20
|
+
const registerOperationResponseType = "{ ok: true } | { ok: false; errors: string[] }";
|
|
21
|
+
const generateRegisterOperation = (name: string) => {
|
|
22
|
+
return `export async function register${name}QueryResponse(sequenceId:string, queryResponse: ${name}Query): Promise<${registerOperationResponseType}> {
|
|
23
|
+
return await fetch('${fakeEndpoint}', {
|
|
24
|
+
method: 'POST',
|
|
25
|
+
headers: {
|
|
26
|
+
'Content-Type': 'application/json',
|
|
27
|
+
'sequence-id': sequenceId
|
|
28
|
+
},
|
|
29
|
+
body: JSON.stringify({
|
|
30
|
+
type: "operation",
|
|
31
|
+
operationName: "${name}",
|
|
32
|
+
data: queryResponse
|
|
33
|
+
}),
|
|
34
|
+
}).then((res) => res.json()) as ${registerOperationResponseType};
|
|
35
|
+
}`;
|
|
36
|
+
};
|
|
37
|
+
const generateRegisterOperationError = (name: string) => {
|
|
38
|
+
return `export async function register${name}QueryErrorResponse(sequenceId:string, { errors, responseStatusCode }: { errors: Record<string, unknown>[]; responseStatusCode: number }): Promise<${registerOperationResponseType}> {
|
|
39
|
+
return await fetch('${fakeEndpoint}', {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
headers: {
|
|
42
|
+
'Content-Type': 'application/json',
|
|
43
|
+
'sequence-id': sequenceId
|
|
44
|
+
},
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
type: "network-error",
|
|
47
|
+
operationName: "${name}",
|
|
48
|
+
responseStatusCode,
|
|
49
|
+
errors
|
|
50
|
+
}),
|
|
51
|
+
}).then((res) => res.json()) as ${registerOperationResponseType};
|
|
52
|
+
}`;
|
|
53
|
+
};
|
|
54
|
+
const generateRegisterMutation = (name: string) => {
|
|
55
|
+
return `export async function register${name}MutationResponse(sequenceId:string, mutationResponse: ${name}Mutation): Promise<${registerOperationResponseType}> {
|
|
56
|
+
return await fetch('${fakeEndpoint}', {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
headers: {
|
|
59
|
+
'Content-Type': 'application/json',
|
|
60
|
+
'sequence-id': sequenceId
|
|
61
|
+
},
|
|
62
|
+
body: JSON.stringify({
|
|
63
|
+
type: "operation",
|
|
64
|
+
operationName: "${name}",
|
|
65
|
+
data: mutationResponse
|
|
66
|
+
}),
|
|
67
|
+
}).then((res) => res.json()) as ${registerOperationResponseType};
|
|
68
|
+
}`;
|
|
69
|
+
};
|
|
70
|
+
const generateRegisterMutationError = (name: string) => {
|
|
71
|
+
return `export async function register${name}MutationErrorResponse(sequenceId:string, { errors, responseStatusCode }: { errors: Record<string, unknown>[]; responseStatusCode: number }): Promise<${registerOperationResponseType}> {
|
|
72
|
+
return await fetch('${fakeEndpoint}', {
|
|
73
|
+
method: 'POST',
|
|
74
|
+
headers: {
|
|
75
|
+
'Content-Type': 'application/json',
|
|
76
|
+
'sequence-id': sequenceId
|
|
77
|
+
},
|
|
78
|
+
body: JSON.stringify({
|
|
79
|
+
type: "network-error",
|
|
80
|
+
operationName: "${name}",
|
|
81
|
+
responseStatusCode,
|
|
82
|
+
errors
|
|
83
|
+
}),
|
|
84
|
+
}).then((res) => res.json()) as ${registerOperationResponseType};
|
|
85
|
+
}`;
|
|
86
|
+
};
|
|
87
|
+
const importQueryIdentifierName = (documentName: string) => {
|
|
88
|
+
return `import type { ${documentName}Query } from '${config.typesFile}';`;
|
|
89
|
+
};
|
|
90
|
+
const importMutationIdentifierName = (documentName: string) => {
|
|
91
|
+
return `import type { ${documentName}Mutation } from '${config.typesFile}';`;
|
|
92
|
+
};
|
|
93
|
+
return `/* eslint-disable */
|
|
94
|
+
// This file was generated by a @newmo/graphql-codegen-fake-server-operation
|
|
95
|
+
${documents
|
|
96
|
+
.flatMap((document) => {
|
|
97
|
+
return document.document?.definitions?.map((definition) => {
|
|
98
|
+
// query
|
|
99
|
+
if (
|
|
100
|
+
definition.kind === "OperationDefinition" &&
|
|
101
|
+
definition.operation === "query" &&
|
|
102
|
+
definition.name
|
|
103
|
+
) {
|
|
104
|
+
return importQueryIdentifierName(definition.name.value);
|
|
105
|
+
}
|
|
106
|
+
if (
|
|
107
|
+
definition.kind === "OperationDefinition" &&
|
|
108
|
+
definition.operation === "mutation" &&
|
|
109
|
+
definition.name
|
|
110
|
+
) {
|
|
111
|
+
return importMutationIdentifierName(definition.name.value);
|
|
112
|
+
}
|
|
113
|
+
return [];
|
|
114
|
+
});
|
|
115
|
+
})
|
|
116
|
+
.join("\n")}
|
|
117
|
+
${documents
|
|
118
|
+
.flatMap((document) => {
|
|
119
|
+
return document.document?.definitions?.flatMap((definition) => {
|
|
120
|
+
// query
|
|
121
|
+
if (
|
|
122
|
+
definition.kind === "OperationDefinition" &&
|
|
123
|
+
definition.operation === "query" &&
|
|
124
|
+
definition.name
|
|
125
|
+
) {
|
|
126
|
+
return [
|
|
127
|
+
generateRegisterOperation(definition.name.value),
|
|
128
|
+
generateRegisterOperationError(definition.name.value),
|
|
129
|
+
];
|
|
130
|
+
}
|
|
131
|
+
if (
|
|
132
|
+
definition.kind === "OperationDefinition" &&
|
|
133
|
+
definition.operation === "mutation" &&
|
|
134
|
+
definition.name
|
|
135
|
+
) {
|
|
136
|
+
return [
|
|
137
|
+
generateRegisterMutation(definition.name.value),
|
|
138
|
+
generateRegisterMutationError(definition.name.value),
|
|
139
|
+
];
|
|
140
|
+
}
|
|
141
|
+
return [];
|
|
142
|
+
});
|
|
143
|
+
})
|
|
144
|
+
.join("\n")}
|
|
145
|
+
`;
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
// GraphQL Codegen Plugin requires CommonJS export
|
|
149
|
+
module.exports = plugin;
|