@schema-hub/zod-graphql-fake-client 0.0.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/LICENSE +21 -0
- package/package.json +27 -0
- package/readme.md +60 -0
- package/zod-graphql-fake-client/fake-client.d.ts +25 -0
- package/zod-graphql-fake-client/fake-client.d.ts.map +1 -0
- package/zod-graphql-fake-client/fake-client.js +45 -0
- package/zod-graphql-fake-client/fake-client.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [2024]
|
|
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/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "Mathias Schreck <schreck.mathias@gmail.com>",
|
|
3
|
+
"dependencies": {
|
|
4
|
+
"@schema-hub/zod-graphql-query-builder": "0.0.2"
|
|
5
|
+
},
|
|
6
|
+
"description": "Fake GraphQL client for testing @schema-hub/zod-graphql-client",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": "^20"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"fake-graphql-client",
|
|
12
|
+
"testing-client"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"main": "zod-graphql-fake-client/fake-client.js",
|
|
16
|
+
"name": "@schema-hub/zod-graphql-fake-client",
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@schema-hub/zod-graphql-client": "0.0.1"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+ssh://git@github.com/enormora/schema-hub.git"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"types": "zod-graphql-fake-client/fake-client.d.ts",
|
|
26
|
+
"version": "0.0.1"
|
|
27
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# zod-graphql-fake-client
|
|
2
|
+
|
|
3
|
+
A fake GraphQL client for testing purposes, compatible with the [`@schema-hub/zod-graphql-client`](../zod-graphql-client/readme.md).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install `@schema-hub/zod-graphql-fake-client` via npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install --dev @schema-hub/zod-graphql-fake-client
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
To use the fake GraphQL client in tests, import it and create an instance with custom configurations:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { createFakeGraphqlClient } from '@schema-hub/zod-graphql-fake-client';
|
|
19
|
+
|
|
20
|
+
const client = createFakeGraphqlClient();
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
It works best with dependency injection, so it's recommended to inject a new client instance for every test case.
|
|
24
|
+
|
|
25
|
+
### Customizing Query Results
|
|
26
|
+
|
|
27
|
+
Specify custom query results for individual queries 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
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { createFakeGraphqlClient } from '@schema-hub/zod-graphql-fake-client';
|
|
31
|
+
|
|
32
|
+
const customResults = [
|
|
33
|
+
{ data: { foo: 'bar' } }, // Successful response
|
|
34
|
+
{ error: { type: 'network', message: 'Request timed out' } }, // Network error
|
|
35
|
+
{ error: { type: 'server', statusCode: 500, message: 'Internal server error' } } // Server error
|
|
36
|
+
// Add more custom results as needed...
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
const client = createFakeGraphqlClient({ results: customResults });
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The order of the array matters, so the first result will be returned by the first `query()` call.
|
|
43
|
+
|
|
44
|
+
### Inspecting Query Payloads
|
|
45
|
+
|
|
46
|
+
The fake GraphQL client keeps track of all query payloads sent to the server (without actually sending them), allowing inspection during tests. Retrieve individual query payloads by index or inspect the first query payload recorded.
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { createFakeGraphqlClient } from '@schema-hub/zod-graphql-fake-client';
|
|
50
|
+
|
|
51
|
+
const client = createFakeGraphqlClient();
|
|
52
|
+
|
|
53
|
+
// Inspect the payload of the first query
|
|
54
|
+
const firstPayload = client.inspectFirstQueryPayload();
|
|
55
|
+
console.log(firstPayload);
|
|
56
|
+
|
|
57
|
+
// Inspect the payload of the second query
|
|
58
|
+
const secondPayload = client.inspectQueryPayload(1);
|
|
59
|
+
console.log(secondPayload);
|
|
60
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type GraphqlClient, type QueryErrorDetails } from '@schema-hub/zod-graphql-client/zod-graphql-client/entry-point.d.ts';
|
|
2
|
+
export type QueryPayload = {
|
|
3
|
+
operationName?: string | undefined;
|
|
4
|
+
variables: Record<string, unknown>;
|
|
5
|
+
query: string;
|
|
6
|
+
};
|
|
7
|
+
export type FakeGraphqlClient = GraphqlClient & {
|
|
8
|
+
readonly inspectQueryPayload: (index: number) => QueryPayload;
|
|
9
|
+
readonly inspectFirstQueryPayload: () => QueryPayload;
|
|
10
|
+
};
|
|
11
|
+
type FakeSuccessData = {
|
|
12
|
+
error?: undefined;
|
|
13
|
+
data: unknown;
|
|
14
|
+
};
|
|
15
|
+
type FakeFailureResult = {
|
|
16
|
+
data?: undefined;
|
|
17
|
+
error: QueryErrorDetails;
|
|
18
|
+
};
|
|
19
|
+
export type FakeResult = FakeFailureResult | FakeSuccessData;
|
|
20
|
+
type FakeClientOptions = {
|
|
21
|
+
readonly results?: FakeResult[];
|
|
22
|
+
};
|
|
23
|
+
export declare function createFakeGraphqlClient(clientOptions?: FakeClientOptions): FakeGraphqlClient;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=fake-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake-client.d.ts","sourceRoot":"","sources":["../../../../source/zod-graphql-fake-client/fake-client.ts"],"names":[],"mappings":"AAEA,OAAO,EACH,KAAK,aAAa,EAElB,KAAK,iBAAiB,EAGzB,MAAM,sCAAsC,CAAC;AAI9C,MAAM,MAAM,YAAY,GAAG;IACvB,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,KAAK,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG,aAAa,GAAG;IAC5C,QAAQ,CAAC,mBAAmB,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,YAAY,CAAC;IAC9D,QAAQ,CAAC,wBAAwB,EAAE,MAAM,YAAY,CAAC;CACzD,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,iBAAiB,CAAC;CAC5B,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,CAuDhG"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { GraphqlQueryError } from '@schema-hub/zod-graphql-client/zod-graphql-client/entry-point.js';
|
|
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';
|
|
4
|
+
export function createFakeGraphqlClient(clientOptions = {}) {
|
|
5
|
+
const { results = [] } = clientOptions;
|
|
6
|
+
const queryPayloads = [];
|
|
7
|
+
const defaultResult = { data: {} };
|
|
8
|
+
async function query(schema, options = {}) {
|
|
9
|
+
const { variables = {} } = options;
|
|
10
|
+
const variableDefinitions = extractVariableDefinitions(variables);
|
|
11
|
+
const variableValues = extractVariableValues(variables);
|
|
12
|
+
const serializedQuery = buildGraphqlQuery(schema, {
|
|
13
|
+
queryName: options.queryName,
|
|
14
|
+
variableDefinitions
|
|
15
|
+
});
|
|
16
|
+
const result = results[queryPayloads.length] ?? defaultResult;
|
|
17
|
+
queryPayloads.push({ operationName: options.queryName, query: serializedQuery, variables: variableValues });
|
|
18
|
+
if (result.error !== undefined) {
|
|
19
|
+
return { success: false, errorDetails: result.error };
|
|
20
|
+
}
|
|
21
|
+
return { success: true, data: result.data };
|
|
22
|
+
}
|
|
23
|
+
function inspectQueryPayload(index) {
|
|
24
|
+
const payload = queryPayloads[index];
|
|
25
|
+
if (payload === undefined) {
|
|
26
|
+
throw new Error(`No query payload at index ${index} recorded`);
|
|
27
|
+
}
|
|
28
|
+
return payload;
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
query,
|
|
32
|
+
async queryOrThrow(schema, options) {
|
|
33
|
+
const result = await query(schema, options);
|
|
34
|
+
if (result.success) {
|
|
35
|
+
return result.data;
|
|
36
|
+
}
|
|
37
|
+
throw new GraphqlQueryError(result.errorDetails);
|
|
38
|
+
},
|
|
39
|
+
inspectQueryPayload,
|
|
40
|
+
inspectFirstQueryPayload() {
|
|
41
|
+
return inspectQueryPayload(0);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=fake-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake-client.js","sourceRoot":"","sources":["../../../../source/zod-graphql-fake-client/fake-client.ts"],"names":[],"mappings":"AAEA,OAAO,EAEH,iBAAiB,EAIpB,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EAAE,0BAA0B,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AACvG,OAAO,EAAE,iBAAiB,EAAE,MAAM,6CAA6C,CAAC;AA4BhF,MAAM,UAAU,uBAAuB,CAAC,gBAAmC,EAAE;IACzE,MAAM,EAAE,OAAO,GAAG,EAAE,EAAE,GAAG,aAAa,CAAC;IACvC,MAAM,aAAa,GAAmB,EAAE,CAAC;IACzC,MAAM,aAAa,GAAe,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;IAE/C,KAAK,UAAU,KAAK,CAChB,MAAc,EACd,UAAwB,EAAE;QAE1B,MAAM,EAAE,SAAS,GAAG,EAAE,EAA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