@schema-hub/zod-graphql-fake-client 0.0.2 → 0.0.3
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/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Mathias Schreck <schreck.mathias@gmail.com>",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"@schema-hub/zod-graphql-query-builder": "0.0.
|
|
4
|
+
"@schema-hub/zod-graphql-query-builder": "0.0.4"
|
|
5
5
|
},
|
|
6
6
|
"description": "Fake GraphQL client for testing @schema-hub/zod-graphql-client",
|
|
7
7
|
"engines": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"main": "zod-graphql-fake-client/fake-client.js",
|
|
16
16
|
"name": "@schema-hub/zod-graphql-fake-client",
|
|
17
17
|
"peerDependencies": {
|
|
18
|
-
"@schema-hub/zod-graphql-client": "0.0.
|
|
18
|
+
"@schema-hub/zod-graphql-client": "0.0.3"
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|
|
@@ -23,5 +23,5 @@
|
|
|
23
23
|
},
|
|
24
24
|
"type": "module",
|
|
25
25
|
"types": "zod-graphql-fake-client/fake-client.d.ts",
|
|
26
|
-
"version": "0.0.
|
|
26
|
+
"version": "0.0.3"
|
|
27
27
|
}
|
package/readme.md
CHANGED
|
@@ -22,9 +22,9 @@ const client = createFakeGraphqlClient();
|
|
|
22
22
|
|
|
23
23
|
It works best with dependency injection, so it's recommended to inject a new client instance for every test case.
|
|
24
24
|
|
|
25
|
-
### Customizing
|
|
25
|
+
### Customizing Operation Results
|
|
26
26
|
|
|
27
|
-
Specify custom
|
|
27
|
+
Specify custom operation results for individual operations by passing an array of `FakeResult` objects to `createFakeGraphqlClient`. Each `FakeResult` can contain either a successful data payload or an error object, allowing simulation of various scenarios in tests.
|
|
28
28
|
|
|
29
29
|
```typescript
|
|
30
30
|
import { createFakeGraphqlClient } from '@schema-hub/zod-graphql-fake-client';
|
|
@@ -32,29 +32,35 @@ import { createFakeGraphqlClient } from '@schema-hub/zod-graphql-fake-client';
|
|
|
32
32
|
const customResults = [
|
|
33
33
|
{ data: { foo: 'bar' } }, // Successful response
|
|
34
34
|
{ error: { type: 'network', message: 'Request timed out' } }, // Network error
|
|
35
|
-
{
|
|
35
|
+
{
|
|
36
|
+
error: {
|
|
37
|
+
type: 'server',
|
|
38
|
+
statusCode: 500,
|
|
39
|
+
message: 'Internal server error'
|
|
40
|
+
}
|
|
41
|
+
} // Server error
|
|
36
42
|
// Add more custom results as needed...
|
|
37
43
|
];
|
|
38
44
|
|
|
39
45
|
const client = createFakeGraphqlClient({ results: customResults });
|
|
40
46
|
```
|
|
41
47
|
|
|
42
|
-
The order of the array matters, so the first result will be returned by the first `query()` call.
|
|
48
|
+
The order of the array matters, so the first result will be returned by the first `query()` or `mutate()` call.
|
|
43
49
|
|
|
44
|
-
### Inspecting
|
|
50
|
+
### Inspecting Operation Payloads
|
|
45
51
|
|
|
46
|
-
The fake GraphQL client keeps track of all
|
|
52
|
+
The fake GraphQL client keeps track of all operation payloads sent to the server (without actually sending them), allowing inspection during tests. Retrieve individual operation payloads by index or inspect the first operation payload recorded.
|
|
47
53
|
|
|
48
54
|
```typescript
|
|
49
55
|
import { createFakeGraphqlClient } from '@schema-hub/zod-graphql-fake-client';
|
|
50
56
|
|
|
51
57
|
const client = createFakeGraphqlClient();
|
|
52
58
|
|
|
53
|
-
// Inspect the payload of the first
|
|
54
|
-
const firstPayload = client.
|
|
59
|
+
// Inspect the payload of the first operation
|
|
60
|
+
const firstPayload = client.inspectFirstOperationPayload();
|
|
55
61
|
console.log(firstPayload);
|
|
56
62
|
|
|
57
|
-
// Inspect the payload of the second
|
|
58
|
-
const secondPayload = client.
|
|
63
|
+
// Inspect the payload of the second operation
|
|
64
|
+
const secondPayload = client.inspectOperationPayload(1);
|
|
59
65
|
console.log(secondPayload);
|
|
60
66
|
```
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
operationName?: string | undefined;
|
|
4
|
-
variables: Record<string, unknown>;
|
|
5
|
-
query: string;
|
|
6
|
-
};
|
|
1
|
+
import type { GraphqlOverHttpOperationRequestPayload } from '@schema-hub/zod-graphql-client/zod-graphql-client/client.d.ts';
|
|
2
|
+
import { type GraphqlClient, type OperationErrorDetails } from '@schema-hub/zod-graphql-client/zod-graphql-client/entry-point.d.ts';
|
|
7
3
|
export type FakeGraphqlClient = GraphqlClient & {
|
|
8
|
-
readonly
|
|
9
|
-
readonly
|
|
4
|
+
readonly inspectOperationPayload: (index: number) => GraphqlOverHttpOperationRequestPayload;
|
|
5
|
+
readonly inspectFirstOperationPayload: () => GraphqlOverHttpOperationRequestPayload;
|
|
10
6
|
};
|
|
11
7
|
type FakeSuccessData = {
|
|
12
8
|
error?: undefined;
|
|
@@ -14,7 +10,7 @@ type FakeSuccessData = {
|
|
|
14
10
|
};
|
|
15
11
|
type FakeFailureResult = {
|
|
16
12
|
data?: undefined;
|
|
17
|
-
error:
|
|
13
|
+
error: OperationErrorDetails;
|
|
18
14
|
};
|
|
19
15
|
export type FakeResult = FakeFailureResult | FakeSuccessData;
|
|
20
16
|
type FakeClientOptions = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fake-client.d.ts","sourceRoot":"","sources":["../../../../source/zod-graphql-fake-client/fake-client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fake-client.d.ts","sourceRoot":"","sources":["../../../../source/zod-graphql-fake-client/fake-client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sCAAsC,EAAoB,MAAM,iCAAiC,CAAC;AAChH,OAAO,EACH,KAAK,aAAa,EAElB,KAAK,qBAAqB,EAG7B,MAAM,sCAAsC,CAAC;AAI9C,MAAM,MAAM,iBAAiB,GAAG,aAAa,GAAG;IAC5C,QAAQ,CAAC,uBAAuB,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,sCAAsC,CAAC;IAC5F,QAAQ,CAAC,4BAA4B,EAAE,MAAM,sCAAsC,CAAC;CACvF,CAAC;AAEF,KAAK,eAAe,GAAG;IACnB,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,IAAI,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACrB,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,KAAK,EAAE,qBAAqB,CAAC;CAChC,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,iBAAiB,GAAG,eAAe,CAAC;AAE7D,KAAK,iBAAiB,GAAG;IACrB,QAAQ,CAAC,OAAO,CAAC,EAAE,UAAU,EAAE,CAAC;CACnC,CAAC;AACF,wBAAgB,uBAAuB,CAAC,aAAa,GAAE,iBAAsB,GAAG,iBAAiB,CA0FhG"}
|
|
@@ -1,27 +1,42 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GraphqlOperationError } from '@schema-hub/zod-graphql-client/zod-graphql-client/entry-point.js';
|
|
2
2
|
import { extractVariableDefinitions, extractVariableValues } from '@schema-hub/zod-graphql-client/zod-graphql-client/variables.js';
|
|
3
|
-
import { buildGraphqlQuery } from '@schema-hub/zod-graphql-query-builder/zod-graphql-query-builder/entry-point.js';
|
|
3
|
+
import { buildGraphqlMutation, buildGraphqlQuery } from '@schema-hub/zod-graphql-query-builder/zod-graphql-query-builder/entry-point.js';
|
|
4
4
|
export function createFakeGraphqlClient(clientOptions = {}) {
|
|
5
5
|
const { results = [] } = clientOptions;
|
|
6
|
-
const
|
|
6
|
+
const operationPayloads = [];
|
|
7
7
|
const defaultResult = { data: {} };
|
|
8
|
-
async function
|
|
8
|
+
async function collectOperation(schema, type, options = {}) {
|
|
9
9
|
const { variables = {} } = options;
|
|
10
10
|
const variableDefinitions = extractVariableDefinitions(variables);
|
|
11
11
|
const variableValues = extractVariableValues(variables);
|
|
12
|
-
const serializedQuery =
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
const serializedQuery = type === 'query'
|
|
13
|
+
? buildGraphqlQuery(schema, {
|
|
14
|
+
operationName: options.operationName,
|
|
15
|
+
variableDefinitions
|
|
16
|
+
})
|
|
17
|
+
: buildGraphqlMutation(schema, {
|
|
18
|
+
operationName: options.operationName,
|
|
19
|
+
variableDefinitions
|
|
20
|
+
});
|
|
21
|
+
const result = results[operationPayloads.length] ?? defaultResult;
|
|
22
|
+
operationPayloads.push({
|
|
23
|
+
operationName: options.operationName,
|
|
24
|
+
query: serializedQuery,
|
|
25
|
+
variables: variableValues
|
|
15
26
|
});
|
|
16
|
-
const result = results[queryPayloads.length] ?? defaultResult;
|
|
17
|
-
queryPayloads.push({ operationName: options.queryName, query: serializedQuery, variables: variableValues });
|
|
18
27
|
if (result.error !== undefined) {
|
|
19
28
|
return { success: false, errorDetails: result.error };
|
|
20
29
|
}
|
|
21
30
|
return { success: true, data: result.data };
|
|
22
31
|
}
|
|
23
|
-
function
|
|
24
|
-
|
|
32
|
+
async function query(schema, options = {}) {
|
|
33
|
+
return collectOperation(schema, 'query', options);
|
|
34
|
+
}
|
|
35
|
+
async function mutate(schema, options = {}) {
|
|
36
|
+
return collectOperation(schema, 'mutation', options);
|
|
37
|
+
}
|
|
38
|
+
function inspectOperationPayload(index) {
|
|
39
|
+
const payload = operationPayloads[index];
|
|
25
40
|
if (payload === undefined) {
|
|
26
41
|
throw new Error(`No query payload at index ${index} recorded`);
|
|
27
42
|
}
|
|
@@ -34,11 +49,19 @@ export function createFakeGraphqlClient(clientOptions = {}) {
|
|
|
34
49
|
if (result.success) {
|
|
35
50
|
return result.data;
|
|
36
51
|
}
|
|
37
|
-
throw new
|
|
52
|
+
throw new GraphqlOperationError(result.errorDetails);
|
|
53
|
+
},
|
|
54
|
+
mutate,
|
|
55
|
+
async mutateOrThrow(schema, options) {
|
|
56
|
+
const result = await mutate(schema, options);
|
|
57
|
+
if (result.success) {
|
|
58
|
+
return result.data;
|
|
59
|
+
}
|
|
60
|
+
throw new GraphqlOperationError(result.errorDetails);
|
|
38
61
|
},
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return
|
|
62
|
+
inspectOperationPayload,
|
|
63
|
+
inspectFirstOperationPayload() {
|
|
64
|
+
return inspectOperationPayload(0);
|
|
42
65
|
}
|
|
43
66
|
};
|
|
44
67
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fake-client.js","sourceRoot":"","sources":["../../../../source/zod-graphql-fake-client/fake-client.ts"],"names":[],"mappings":"AAEA,OAAO,EAEH,
|
|
1
|
+
{"version":3,"file":"fake-client.js","sourceRoot":"","sources":["../../../../source/zod-graphql-fake-client/fake-client.ts"],"names":[],"mappings":"AAEA,OAAO,EAEH,qBAAqB,EAIxB,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EAAE,0BAA0B,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AACvG,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,6CAA6C,CAAC;AAsBtG,MAAM,UAAU,uBAAuB,CAAC,gBAAmC,EAAE;IACzE,MAAM,EAAE,OAAO,GAAG,EAAE,EAAE,GAAG,aAAa,CAAC;IACvC,MAAM,iBAAiB,GAA6C,EAAE,CAAC;IACvE,MAAM,aAAa,GAAe,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;IAE/C,KAAK,UAAU,gBAAgB,CAC3B,MAAc,EACd,IAA0B,EAC1B,UAA4B,EAAE;QAE9B,MAAM,EAAE,SAAS,GAAG,EAAE,EAAE,GAAG,OAAO,CAAC;QACnC,MAAM,mBAAmB,GAAG,0BAA0B,CAAC,SAAS,CAAC,CAAC;QAClE,MAAM,cAAc,GAAG,qBAAqB,CAAC,SAAS,CAAC,CAAC;QAExD,MAAM,eAAe,GAAG,IAAI,KAAK,OAAO;YACpC,CAAC,CAAC,iBAAiB,CAAC,MAAM,EAAE;gBACxB,aAAa,EAAE,OAAO,CAAC,aAAa;gBACpC,mBAAmB;aACtB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