@newmo/graphql-codegen-fake-server-client 0.11.0 → 0.13.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 CHANGED
@@ -41,12 +41,15 @@ You can use `./generated/fake-client.ts` to register the fake to the fake server
41
41
 
42
42
  ```ts
43
43
  import { it, expect } from "vitest";
44
- import { registerFake } from "./generated/fake-client";
44
+ import { createFakeClient } from "./generated/fake-client";
45
45
 
46
+ const fakeClient = createFakeClient({
47
+ fakeServerEndpoint: "http://localhost:4000"
48
+ })
46
49
  it("register fake response for query", async () => {
47
50
  const sequenceId = crypto.randomUUID();
48
51
  // register fake response for GetBooks query
49
- const resRegister = await registerGetBooksQueryResponse(sequenceId, {
52
+ const resRegister = await fakeClient.registerGetBooksQueryResponse(sequenceId, {
50
53
  books: [
51
54
  {
52
55
  id: "new id",
@@ -73,6 +76,10 @@ it("register fake response for query", async () => {
73
76
  ],
74
77
  }
75
78
  `);
79
+ // Get actual request and response for testing
80
+ const calledResults = await fakeClient.calledGetBooksDocumentQuery(sequenceId);
81
+ console.log(calledResults[0].request);
82
+ console.log(calledResults[0].response);
76
83
  });
77
84
  ```
78
85
 
@@ -34,12 +34,14 @@ ${exportsFunctions
34
34
  return [
35
35
  indentEachLine(`${indent}${indent}`, generateRegisterOperationMethod(fn.name, "options.fakeServerEndpoint")),
36
36
  indentEachLine(`${indent}${indent}`, generateRegisterOperationErrorMethod(fn.name, "options.fakeServerEndpoint")),
37
+ indentEachLine(`${indent}${indent}`, generateCalledQuery(fn.name, `options.fakeServerEndpoint + "/called"`)),
37
38
  ];
38
39
  }
39
40
  if (fn.type === "mutation") {
40
41
  return [
41
42
  indentEachLine(`${indent}${indent}`, generateRegisterMutationMethod(fn.name, "options.fakeServerEndpoint")),
42
43
  indentEachLine(`${indent}${indent}`, generateRegisterMutationErrorMethod(fn.name, "options.fakeServerEndpoint")),
44
+ indentEachLine(`${indent}${indent}`, generateCalledMutation(fn.name, `options.fakeServerEndpoint + "/called"`)),
43
45
  ];
44
46
  }
45
47
  throw new Error(`Unknown type${fn}`);
@@ -112,13 +114,111 @@ ${exportsFunctions
112
114
  errors
113
115
  }),
114
116
  }).then((res) => res.json()) as ${registerOperationResponseType};
117
+ }`;
118
+ };
119
+ const generateCalledQuery = (name, calledEndpoint) => {
120
+ return `async called${name}Query(sequenceId:string): Promise<{
121
+ ok: true;
122
+ data: {
123
+ requestTimestamp: number;
124
+ request: {
125
+ headers: Record<string, unknown>;
126
+ body: {
127
+ operationName: string;
128
+ query: string;
129
+ };
130
+ };
131
+ response: {
132
+ statusCode: number;
133
+ headers: Record<string, unknown>;
134
+ body: ${(0, convertName_1.convertName)(name, config)}Query;
135
+ };
136
+ }[]
137
+ }> {
138
+ return await fetch(${calledEndpoint}, {
139
+ method: 'POST',
140
+ headers: {
141
+ 'Content-Type': 'application/json',
142
+ 'sequence-id': sequenceId
143
+ },
144
+ body: JSON.stringify({
145
+ operationName: "${name}"
146
+ }),
147
+ }).then((res) => res.json()) as {
148
+ ok: true;
149
+ data: {
150
+ requestTimestamp: number;
151
+ request: {
152
+ headers: Record<string, unknown>;
153
+ body: {
154
+ operationName: string;
155
+ query: string;
156
+ };
157
+ };
158
+ response: {
159
+ statusCode: number;
160
+ headers: Record<string, unknown>;
161
+ body: ${(0, convertName_1.convertName)(name, config)}Query;
162
+ };
163
+ }[];
164
+ };
165
+ }`;
166
+ };
167
+ const generateCalledMutation = (name, calledEndpoint) => {
168
+ return `async called${name}Mutation(sequenceId:string): Promise<{
169
+ ok: true;
170
+ data: {
171
+ requestTimestamp: number;
172
+ request: {
173
+ headers: Record<string, unknown>;
174
+ body: {
175
+ operationName: string;
176
+ query: string;
177
+ variables: ${(0, convertName_1.convertName)(name, config)}MutationVariables;
178
+ };
179
+ };
180
+ response: {
181
+ statusCode: number;
182
+ headers: Record<string, unknown>;
183
+ body: ${(0, convertName_1.convertName)(name, config)}Mutation;
184
+ };
185
+ }[];
186
+ }> {
187
+ return await fetch(${calledEndpoint}, {
188
+ method: 'POST',
189
+ headers: {
190
+ 'Content-Type': 'application/json',
191
+ 'sequence-id': sequenceId
192
+ },
193
+ body: JSON.stringify({
194
+ operationName: "${name}"
195
+ }),
196
+ }).then((res) => res.json()) as {
197
+ ok: true;
198
+ data: {
199
+ requestTimestamp: number;
200
+ request: {
201
+ headers: Record<string, unknown>;
202
+ body: {
203
+ operationName: string;
204
+ query: string;
205
+ variables: ${(0, convertName_1.convertName)(name, config)}MutationVariables;
206
+ };
207
+ };
208
+ response: {
209
+ statusCode: number;
210
+ headers: Record<string, unknown>;
211
+ body: ${(0, convertName_1.convertName)(name, config)}Mutation;
212
+ };
213
+ }[];
214
+ }
115
215
  }`;
116
216
  };
117
217
  const importQueryIdentifierName = (documentName) => {
118
218
  return `import type { ${(0, convertName_1.convertName)(documentName, config)}Query } from '${config.typesFile}';`;
119
219
  };
120
220
  const importMutationIdentifierName = (documentName) => {
121
- return `import type { ${(0, convertName_1.convertName)(documentName, config)}Mutation } from '${config.typesFile}';`;
221
+ return `import type { ${(0, convertName_1.convertName)(documentName, config)}Mutation, ${(0, convertName_1.convertName)(documentName, config)}MutationVariables } from '${config.typesFile}';`;
122
222
  };
123
223
  return `/* eslint-disable */
124
224
  // This file was generated by a @newmo/graphql-codegen-fake-server-operation
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@newmo/graphql-codegen-fake-server-client",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "private": false,
5
5
  "description": "GraphQL Codegen plugin for generating a fake server client",
6
6
  "keywords": [
@@ -61,5 +61,5 @@
61
61
  "access": "public",
62
62
  "registry": "https://registry.npmjs.org/"
63
63
  },
64
- "gitHead": "4adbbdbca5cf43dc7e86f749aca8130d1e9b214a"
64
+ "gitHead": "4ec846144d318fc5354b6666fb8301ea0c157c9b"
65
65
  }
@@ -49,6 +49,10 @@ ${exportsFunctions
49
49
  `${indent}${indent}`,
50
50
  generateRegisterOperationErrorMethod(fn.name, "options.fakeServerEndpoint"),
51
51
  ),
52
+ indentEachLine(
53
+ `${indent}${indent}`,
54
+ generateCalledQuery(fn.name, `options.fakeServerEndpoint + "/called"`),
55
+ ),
52
56
  ];
53
57
  }
54
58
  if (fn.type === "mutation") {
@@ -61,6 +65,10 @@ ${exportsFunctions
61
65
  `${indent}${indent}`,
62
66
  generateRegisterMutationErrorMethod(fn.name, "options.fakeServerEndpoint"),
63
67
  ),
68
+ indentEachLine(
69
+ `${indent}${indent}`,
70
+ generateCalledMutation(fn.name, `options.fakeServerEndpoint + "/called"`),
71
+ ),
64
72
  ];
65
73
  }
66
74
  throw new Error(`Unknown type${fn}`);
@@ -144,15 +152,116 @@ ${exportsFunctions
144
152
  }).then((res) => res.json()) as ${registerOperationResponseType};
145
153
  }`;
146
154
  };
155
+ const generateCalledQuery = (name: string, calledEndpoint: string) => {
156
+ return `async called${name}Query(sequenceId:string): Promise<{
157
+ ok: true;
158
+ data: {
159
+ requestTimestamp: number;
160
+ request: {
161
+ headers: Record<string, unknown>;
162
+ body: {
163
+ operationName: string;
164
+ query: string;
165
+ };
166
+ };
167
+ response: {
168
+ statusCode: number;
169
+ headers: Record<string, unknown>;
170
+ body: ${convertName(name, config)}Query;
171
+ };
172
+ }[]
173
+ }> {
174
+ return await fetch(${calledEndpoint}, {
175
+ method: 'POST',
176
+ headers: {
177
+ 'Content-Type': 'application/json',
178
+ 'sequence-id': sequenceId
179
+ },
180
+ body: JSON.stringify({
181
+ operationName: "${name}"
182
+ }),
183
+ }).then((res) => res.json()) as {
184
+ ok: true;
185
+ data: {
186
+ requestTimestamp: number;
187
+ request: {
188
+ headers: Record<string, unknown>;
189
+ body: {
190
+ operationName: string;
191
+ query: string;
192
+ };
193
+ };
194
+ response: {
195
+ statusCode: number;
196
+ headers: Record<string, unknown>;
197
+ body: ${convertName(name, config)}Query;
198
+ };
199
+ }[];
200
+ };
201
+ }`;
202
+ };
203
+
204
+ const generateCalledMutation = (name: string, calledEndpoint: string) => {
205
+ return `async called${name}Mutation(sequenceId:string): Promise<{
206
+ ok: true;
207
+ data: {
208
+ requestTimestamp: number;
209
+ request: {
210
+ headers: Record<string, unknown>;
211
+ body: {
212
+ operationName: string;
213
+ query: string;
214
+ variables: ${convertName(name, config)}MutationVariables;
215
+ };
216
+ };
217
+ response: {
218
+ statusCode: number;
219
+ headers: Record<string, unknown>;
220
+ body: ${convertName(name, config)}Mutation;
221
+ };
222
+ }[];
223
+ }> {
224
+ return await fetch(${calledEndpoint}, {
225
+ method: 'POST',
226
+ headers: {
227
+ 'Content-Type': 'application/json',
228
+ 'sequence-id': sequenceId
229
+ },
230
+ body: JSON.stringify({
231
+ operationName: "${name}"
232
+ }),
233
+ }).then((res) => res.json()) as {
234
+ ok: true;
235
+ data: {
236
+ requestTimestamp: number;
237
+ request: {
238
+ headers: Record<string, unknown>;
239
+ body: {
240
+ operationName: string;
241
+ query: string;
242
+ variables: ${convertName(name, config)}MutationVariables;
243
+ };
244
+ };
245
+ response: {
246
+ statusCode: number;
247
+ headers: Record<string, unknown>;
248
+ body: ${convertName(name, config)}Mutation;
249
+ };
250
+ }[];
251
+ }
252
+ }`;
253
+ };
254
+
147
255
  const importQueryIdentifierName = (documentName: string) => {
148
256
  return `import type { ${convertName(documentName, config)}Query } from '${
149
257
  config.typesFile
150
258
  }';`;
151
259
  };
152
260
  const importMutationIdentifierName = (documentName: string) => {
153
- return `import type { ${convertName(documentName, config)}Mutation } from '${
154
- config.typesFile
155
- }';`;
261
+ return `import type { ${convertName(documentName, config)}Mutation, ${convertName(
262
+ documentName,
263
+ config,
264
+ )}MutationVariables } from '${config.typesFile}';`;
156
265
  };
157
266
  return `/* eslint-disable */
158
267
  // This file was generated by a @newmo/graphql-codegen-fake-server-operation