@graphql-tools/mock 9.0.26-alpha-20251026055227-9bd9835d18cc3f0f03fa9cd7426372cdcd9cb85c → 9.1.0-alpha-20251027010606-226b580f6448e27f51c6c50b3035c631e1902460
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/cjs/MockStore.js +22 -7
- package/cjs/addMocksToSchema.js +2 -1
- package/cjs/mockServer.js +4 -1
- package/cjs/utils.js +8 -3
- package/esm/MockStore.js +22 -7
- package/esm/addMocksToSchema.js +2 -1
- package/esm/mockServer.js +4 -1
- package/esm/utils.js +6 -1
- package/package.json +3 -3
- package/typings/MockStore.d.cts +29 -9
- package/typings/MockStore.d.ts +29 -9
- package/typings/addMocksToSchema.d.cts +3 -2
- package/typings/addMocksToSchema.d.ts +3 -2
- package/typings/mockServer.d.cts +4 -2
- package/typings/mockServer.d.ts +4 -2
- package/typings/types.d.cts +1 -0
- package/typings/types.d.ts +1 -0
- package/typings/utils.d.cts +2 -2
- package/typings/utils.d.ts +2 -2
package/cjs/MockStore.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MockStore =
|
|
3
|
+
exports.MockStore = void 0;
|
|
4
4
|
exports.createMockStore = createMockStore;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const fast_json_stable_stringify_1 = tslib_1.__importDefault(require("fast-json-stable-stringify"));
|
|
@@ -8,22 +8,35 @@ const graphql_1 = require("graphql");
|
|
|
8
8
|
const MockList_js_1 = require("./MockList.js");
|
|
9
9
|
const types_js_1 = require("./types.js");
|
|
10
10
|
const utils_js_1 = require("./utils.js");
|
|
11
|
-
|
|
11
|
+
const defaultRandomMocks = {
|
|
12
12
|
Int: () => Math.round(Math.random() * 200) - 100,
|
|
13
13
|
Float: () => Math.random() * 200 - 100,
|
|
14
|
-
String: () => 'Hello World',
|
|
15
14
|
Boolean: () => Math.random() > 0.5,
|
|
15
|
+
};
|
|
16
|
+
const defaultDeterministicMocks = {
|
|
17
|
+
Int: () => 1,
|
|
18
|
+
Float: () => 1.5,
|
|
19
|
+
Boolean: () => true,
|
|
20
|
+
};
|
|
21
|
+
const defaultCommonMocks = {
|
|
22
|
+
String: () => 'Hello World',
|
|
16
23
|
ID: () => (0, utils_js_1.uuidv4)(),
|
|
17
24
|
};
|
|
18
25
|
const defaultKeyFieldNames = ['id', '_id'];
|
|
19
26
|
class MockStore {
|
|
20
27
|
schema;
|
|
21
28
|
mocks;
|
|
29
|
+
mockGenerationBehavior;
|
|
22
30
|
typePolicies;
|
|
23
31
|
store = {};
|
|
24
|
-
constructor({ schema, mocks, typePolicies, }) {
|
|
32
|
+
constructor({ schema, mocks, mockGenerationBehavior = 'random', typePolicies, }) {
|
|
25
33
|
this.schema = schema;
|
|
26
|
-
this.
|
|
34
|
+
this.mockGenerationBehavior = mockGenerationBehavior;
|
|
35
|
+
this.mocks = {
|
|
36
|
+
...defaultCommonMocks,
|
|
37
|
+
...(mockGenerationBehavior === 'random' ? defaultRandomMocks : defaultDeterministicMocks),
|
|
38
|
+
...mocks,
|
|
39
|
+
};
|
|
27
40
|
this.typePolicies = typePolicies || {};
|
|
28
41
|
}
|
|
29
42
|
has(typeName, key) {
|
|
@@ -374,7 +387,7 @@ class MockStore {
|
|
|
374
387
|
if (typeof mockFn === 'function')
|
|
375
388
|
return mockFn();
|
|
376
389
|
const values = nullableType.getValues().map(v => v.value);
|
|
377
|
-
return (0, utils_js_1.
|
|
390
|
+
return (0, utils_js_1.takeOneOf)(values, this.mockGenerationBehavior);
|
|
378
391
|
}
|
|
379
392
|
else if ((0, graphql_1.isObjectType)(nullableType)) {
|
|
380
393
|
// this will create a new random ref
|
|
@@ -388,7 +401,7 @@ class MockStore {
|
|
|
388
401
|
let typeName;
|
|
389
402
|
let values = {};
|
|
390
403
|
if (!mock) {
|
|
391
|
-
typeName = (0, utils_js_1.
|
|
404
|
+
typeName = (0, utils_js_1.takeOneOf)(this.schema.getPossibleTypes(nullableType).map(t => t.name), this.mockGenerationBehavior);
|
|
392
405
|
}
|
|
393
406
|
else if (typeof mock === 'function') {
|
|
394
407
|
const mockRes = mock();
|
|
@@ -490,6 +503,8 @@ function assertIsDefined(value, message) {
|
|
|
490
503
|
* Will create `MockStore` for the given `schema`.
|
|
491
504
|
*
|
|
492
505
|
* A `MockStore` will generate mock values for the given schema when queried.
|
|
506
|
+
* By default it'll generate random values, if this causes flakiness and you
|
|
507
|
+
* need a more deterministic behavior, use `mockGenerationBehavior` option.
|
|
493
508
|
*
|
|
494
509
|
* It will store generated mocks, so that, provided with same arguments
|
|
495
510
|
* the returned values will be the same.
|
package/cjs/addMocksToSchema.js
CHANGED
|
@@ -66,7 +66,7 @@ const utils_js_1 = require("./utils.js");
|
|
|
66
66
|
*
|
|
67
67
|
* `Query` and `Mutation` type will use `key` `'ROOT'`.
|
|
68
68
|
*/
|
|
69
|
-
function addMocksToSchema({ schema, store: maybeStore, mocks, typePolicies, resolvers: resolversOrFnResolvers, preserveResolvers = false, }) {
|
|
69
|
+
function addMocksToSchema({ schema, store: maybeStore, mocks, mockGenerationBehavior, typePolicies, resolvers: resolversOrFnResolvers, preserveResolvers = false, }) {
|
|
70
70
|
if (!schema) {
|
|
71
71
|
throw new Error('Must provide schema to mock');
|
|
72
72
|
}
|
|
@@ -80,6 +80,7 @@ function addMocksToSchema({ schema, store: maybeStore, mocks, typePolicies, reso
|
|
|
80
80
|
(0, MockStore_js_1.createMockStore)({
|
|
81
81
|
schema,
|
|
82
82
|
mocks,
|
|
83
|
+
mockGenerationBehavior,
|
|
83
84
|
typePolicies,
|
|
84
85
|
});
|
|
85
86
|
const resolvers = typeof resolversOrFnResolvers === 'function'
|
package/cjs/mockServer.js
CHANGED
|
@@ -15,8 +15,10 @@ const addMocksToSchema_js_1 = require("./addMocksToSchema.js");
|
|
|
15
15
|
* @param preserveResolvers Set to `true` to prevent existing resolvers from being
|
|
16
16
|
* overwritten to provide mock data. This can be used to mock some parts of the
|
|
17
17
|
* server and not others.
|
|
18
|
+
* @param mockGenerationBehavior Set to `'deterministic'` if the default random
|
|
19
|
+
* mock generation behavior causes flakiness.
|
|
18
20
|
*/
|
|
19
|
-
function mockServer(schema, mocks, preserveResolvers = false) {
|
|
21
|
+
function mockServer(schema, mocks, preserveResolvers = false, mockGenerationBehavior) {
|
|
20
22
|
const mockedSchema = (0, addMocksToSchema_js_1.addMocksToSchema)({
|
|
21
23
|
schema: (0, graphql_1.isSchema)(schema)
|
|
22
24
|
? schema
|
|
@@ -24,6 +26,7 @@ function mockServer(schema, mocks, preserveResolvers = false) {
|
|
|
24
26
|
typeDefs: schema,
|
|
25
27
|
}),
|
|
26
28
|
mocks,
|
|
29
|
+
mockGenerationBehavior,
|
|
27
30
|
preserveResolvers,
|
|
28
31
|
});
|
|
29
32
|
return {
|
package/cjs/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isRootType = exports.
|
|
3
|
+
exports.isRootType = exports.takeOneOf = exports.randomListLength = void 0;
|
|
4
4
|
exports.uuidv4 = uuidv4;
|
|
5
5
|
exports.makeRef = makeRef;
|
|
6
6
|
exports.isObject = isObject;
|
|
@@ -21,8 +21,13 @@ const randomListLength = () => {
|
|
|
21
21
|
return 2;
|
|
22
22
|
};
|
|
23
23
|
exports.randomListLength = randomListLength;
|
|
24
|
-
const
|
|
25
|
-
|
|
24
|
+
const takeOneOf = (arr, behavior) => {
|
|
25
|
+
if (behavior === 'deterministic') {
|
|
26
|
+
return arr[0];
|
|
27
|
+
}
|
|
28
|
+
return arr[Math.floor(Math.random() * arr.length)];
|
|
29
|
+
};
|
|
30
|
+
exports.takeOneOf = takeOneOf;
|
|
26
31
|
function makeRef(typeName, key) {
|
|
27
32
|
return { $ref: { key, typeName } };
|
|
28
33
|
}
|
package/esm/MockStore.js
CHANGED
|
@@ -2,23 +2,36 @@ import stringify from 'fast-json-stable-stringify';
|
|
|
2
2
|
import { getNullableType, GraphQLString, isAbstractType, isCompositeType, isEnumType, isInterfaceType, isListType, isNonNullType, isObjectType, isScalarType, } from 'graphql';
|
|
3
3
|
import { deepResolveMockList, isMockList } from './MockList.js';
|
|
4
4
|
import { assertIsRef, isRecord, isRef, } from './types.js';
|
|
5
|
-
import { makeRef, randomListLength,
|
|
6
|
-
|
|
5
|
+
import { makeRef, randomListLength, takeOneOf, uuidv4 } from './utils.js';
|
|
6
|
+
const defaultRandomMocks = {
|
|
7
7
|
Int: () => Math.round(Math.random() * 200) - 100,
|
|
8
8
|
Float: () => Math.random() * 200 - 100,
|
|
9
|
-
String: () => 'Hello World',
|
|
10
9
|
Boolean: () => Math.random() > 0.5,
|
|
10
|
+
};
|
|
11
|
+
const defaultDeterministicMocks = {
|
|
12
|
+
Int: () => 1,
|
|
13
|
+
Float: () => 1.5,
|
|
14
|
+
Boolean: () => true,
|
|
15
|
+
};
|
|
16
|
+
const defaultCommonMocks = {
|
|
17
|
+
String: () => 'Hello World',
|
|
11
18
|
ID: () => uuidv4(),
|
|
12
19
|
};
|
|
13
20
|
const defaultKeyFieldNames = ['id', '_id'];
|
|
14
21
|
export class MockStore {
|
|
15
22
|
schema;
|
|
16
23
|
mocks;
|
|
24
|
+
mockGenerationBehavior;
|
|
17
25
|
typePolicies;
|
|
18
26
|
store = {};
|
|
19
|
-
constructor({ schema, mocks, typePolicies, }) {
|
|
27
|
+
constructor({ schema, mocks, mockGenerationBehavior = 'random', typePolicies, }) {
|
|
20
28
|
this.schema = schema;
|
|
21
|
-
this.
|
|
29
|
+
this.mockGenerationBehavior = mockGenerationBehavior;
|
|
30
|
+
this.mocks = {
|
|
31
|
+
...defaultCommonMocks,
|
|
32
|
+
...(mockGenerationBehavior === 'random' ? defaultRandomMocks : defaultDeterministicMocks),
|
|
33
|
+
...mocks,
|
|
34
|
+
};
|
|
22
35
|
this.typePolicies = typePolicies || {};
|
|
23
36
|
}
|
|
24
37
|
has(typeName, key) {
|
|
@@ -369,7 +382,7 @@ export class MockStore {
|
|
|
369
382
|
if (typeof mockFn === 'function')
|
|
370
383
|
return mockFn();
|
|
371
384
|
const values = nullableType.getValues().map(v => v.value);
|
|
372
|
-
return
|
|
385
|
+
return takeOneOf(values, this.mockGenerationBehavior);
|
|
373
386
|
}
|
|
374
387
|
else if (isObjectType(nullableType)) {
|
|
375
388
|
// this will create a new random ref
|
|
@@ -383,7 +396,7 @@ export class MockStore {
|
|
|
383
396
|
let typeName;
|
|
384
397
|
let values = {};
|
|
385
398
|
if (!mock) {
|
|
386
|
-
typeName =
|
|
399
|
+
typeName = takeOneOf(this.schema.getPossibleTypes(nullableType).map(t => t.name), this.mockGenerationBehavior);
|
|
387
400
|
}
|
|
388
401
|
else if (typeof mock === 'function') {
|
|
389
402
|
const mockRes = mock();
|
|
@@ -484,6 +497,8 @@ function assertIsDefined(value, message) {
|
|
|
484
497
|
* Will create `MockStore` for the given `schema`.
|
|
485
498
|
*
|
|
486
499
|
* A `MockStore` will generate mock values for the given schema when queried.
|
|
500
|
+
* By default it'll generate random values, if this causes flakiness and you
|
|
501
|
+
* need a more deterministic behavior, use `mockGenerationBehavior` option.
|
|
487
502
|
*
|
|
488
503
|
* It will store generated mocks, so that, provided with same arguments
|
|
489
504
|
* the returned values will be the same.
|
package/esm/addMocksToSchema.js
CHANGED
|
@@ -63,7 +63,7 @@ import { copyOwnProps, isObject, isRootType } from './utils.js';
|
|
|
63
63
|
*
|
|
64
64
|
* `Query` and `Mutation` type will use `key` `'ROOT'`.
|
|
65
65
|
*/
|
|
66
|
-
export function addMocksToSchema({ schema, store: maybeStore, mocks, typePolicies, resolvers: resolversOrFnResolvers, preserveResolvers = false, }) {
|
|
66
|
+
export function addMocksToSchema({ schema, store: maybeStore, mocks, mockGenerationBehavior, typePolicies, resolvers: resolversOrFnResolvers, preserveResolvers = false, }) {
|
|
67
67
|
if (!schema) {
|
|
68
68
|
throw new Error('Must provide schema to mock');
|
|
69
69
|
}
|
|
@@ -77,6 +77,7 @@ export function addMocksToSchema({ schema, store: maybeStore, mocks, typePolicie
|
|
|
77
77
|
createMockStore({
|
|
78
78
|
schema,
|
|
79
79
|
mocks,
|
|
80
|
+
mockGenerationBehavior,
|
|
80
81
|
typePolicies,
|
|
81
82
|
});
|
|
82
83
|
const resolvers = typeof resolversOrFnResolvers === 'function'
|
package/esm/mockServer.js
CHANGED
|
@@ -12,8 +12,10 @@ import { addMocksToSchema } from './addMocksToSchema.js';
|
|
|
12
12
|
* @param preserveResolvers Set to `true` to prevent existing resolvers from being
|
|
13
13
|
* overwritten to provide mock data. This can be used to mock some parts of the
|
|
14
14
|
* server and not others.
|
|
15
|
+
* @param mockGenerationBehavior Set to `'deterministic'` if the default random
|
|
16
|
+
* mock generation behavior causes flakiness.
|
|
15
17
|
*/
|
|
16
|
-
export function mockServer(schema, mocks, preserveResolvers = false) {
|
|
18
|
+
export function mockServer(schema, mocks, preserveResolvers = false, mockGenerationBehavior) {
|
|
17
19
|
const mockedSchema = addMocksToSchema({
|
|
18
20
|
schema: isSchema(schema)
|
|
19
21
|
? schema
|
|
@@ -21,6 +23,7 @@ export function mockServer(schema, mocks, preserveResolvers = false) {
|
|
|
21
23
|
typeDefs: schema,
|
|
22
24
|
}),
|
|
23
25
|
mocks,
|
|
26
|
+
mockGenerationBehavior,
|
|
24
27
|
preserveResolvers,
|
|
25
28
|
});
|
|
26
29
|
return {
|
package/esm/utils.js
CHANGED
|
@@ -12,7 +12,12 @@ export const randomListLength = () => {
|
|
|
12
12
|
// return 1 + Math.round(Math.random() * 10)
|
|
13
13
|
return 2;
|
|
14
14
|
};
|
|
15
|
-
export const
|
|
15
|
+
export const takeOneOf = (arr, behavior) => {
|
|
16
|
+
if (behavior === 'deterministic') {
|
|
17
|
+
return arr[0];
|
|
18
|
+
}
|
|
19
|
+
return arr[Math.floor(Math.random() * arr.length)];
|
|
20
|
+
};
|
|
16
21
|
export function makeRef(typeName, key) {
|
|
17
22
|
return { $ref: { key, typeName } };
|
|
18
23
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-tools/mock",
|
|
3
|
-
"version": "9.0
|
|
3
|
+
"version": "9.1.0-alpha-20251027010606-226b580f6448e27f51c6c50b3035c631e1902460",
|
|
4
4
|
"description": "A set of utils for faster development of GraphQL tools",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"peerDependencies": {
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@graphql-tools/schema": "10.0.26-alpha-
|
|
11
|
-
"@graphql-tools/utils": "10.10.0-alpha-
|
|
10
|
+
"@graphql-tools/schema": "10.0.26-alpha-20251027010606-226b580f6448e27f51c6c50b3035c631e1902460",
|
|
11
|
+
"@graphql-tools/utils": "10.10.0-alpha-20251027010606-226b580f6448e27f51c6c50b3035c631e1902460",
|
|
12
12
|
"fast-json-stable-stringify": "^2.1.0",
|
|
13
13
|
"tslib": "^2.4.0"
|
|
14
14
|
},
|
package/typings/MockStore.d.cts
CHANGED
|
@@ -1,23 +1,18 @@
|
|
|
1
1
|
import { GraphQLSchema } from 'graphql';
|
|
2
|
-
import { GetArgs, IMocks, IMockStore, KeyTypeConstraints, Ref, SetArgs, TypePolicy } from './types.cjs';
|
|
3
|
-
export declare const defaultMocks: {
|
|
4
|
-
Int: () => number;
|
|
5
|
-
Float: () => number;
|
|
6
|
-
String: () => string;
|
|
7
|
-
Boolean: () => boolean;
|
|
8
|
-
ID: () => string;
|
|
9
|
-
};
|
|
2
|
+
import { GetArgs, IMocks, IMockStore, KeyTypeConstraints, MockGenerationBehavior, Ref, SetArgs, TypePolicy } from './types.cjs';
|
|
10
3
|
type Entity = {
|
|
11
4
|
[key: string]: unknown;
|
|
12
5
|
};
|
|
13
6
|
export declare class MockStore implements IMockStore {
|
|
14
7
|
schema: GraphQLSchema;
|
|
15
8
|
private mocks;
|
|
9
|
+
private mockGenerationBehavior;
|
|
16
10
|
private typePolicies;
|
|
17
11
|
private store;
|
|
18
|
-
constructor({ schema, mocks, typePolicies, }: {
|
|
12
|
+
constructor({ schema, mocks, mockGenerationBehavior, typePolicies, }: {
|
|
19
13
|
schema: GraphQLSchema;
|
|
20
14
|
mocks?: IMocks;
|
|
15
|
+
mockGenerationBehavior?: MockGenerationBehavior;
|
|
21
16
|
typePolicies?: {
|
|
22
17
|
[typeName: string]: TypePolicy;
|
|
23
18
|
};
|
|
@@ -57,6 +52,8 @@ export declare class MockStore implements IMockStore {
|
|
|
57
52
|
* Will create `MockStore` for the given `schema`.
|
|
58
53
|
*
|
|
59
54
|
* A `MockStore` will generate mock values for the given schema when queried.
|
|
55
|
+
* By default it'll generate random values, if this causes flakiness and you
|
|
56
|
+
* need a more deterministic behavior, use `mockGenerationBehavior` option.
|
|
60
57
|
*
|
|
61
58
|
* It will store generated mocks, so that, provided with same arguments
|
|
62
59
|
* the returned values will be the same.
|
|
@@ -87,6 +84,29 @@ export declare function createMockStore(options: {
|
|
|
87
84
|
* The mocks functions to use.
|
|
88
85
|
*/
|
|
89
86
|
mocks?: IMocks;
|
|
87
|
+
/**
|
|
88
|
+
* Configures the default behavior for Scalar, Enum, Union, and Array types.
|
|
89
|
+
*
|
|
90
|
+
* When set to `random`, then every time a value is generated for a field with
|
|
91
|
+
* one of these types
|
|
92
|
+
* - For Unions and Enums one of the allowed values will be picked randomly
|
|
93
|
+
* - For Arrays an array of random length will be generated
|
|
94
|
+
* - For Int and Float scalars a random number will be generated
|
|
95
|
+
* - For Boolean scalars either `true` or `false` will be returned randomly
|
|
96
|
+
*
|
|
97
|
+
* When set to `deterministic`, then
|
|
98
|
+
* - For Unions and Enums the first allowed value is picked
|
|
99
|
+
* - For Arrays an array of two elements will be generated
|
|
100
|
+
* - For Int and Float scalars values 1 and 1.5 will be used respectively
|
|
101
|
+
* - For Boolean scalars we'll always return `true`
|
|
102
|
+
* - For String scalars we'll always return "Hello World"
|
|
103
|
+
*
|
|
104
|
+
* Regardless of the chosen behavior, for ID scalars a random UUID string
|
|
105
|
+
* will always be generated.
|
|
106
|
+
*
|
|
107
|
+
* @default "random"
|
|
108
|
+
*/
|
|
109
|
+
mockGenerationBehavior?: MockGenerationBehavior;
|
|
90
110
|
typePolicies?: {
|
|
91
111
|
[typeName: string]: TypePolicy;
|
|
92
112
|
};
|
package/typings/MockStore.d.ts
CHANGED
|
@@ -1,23 +1,18 @@
|
|
|
1
1
|
import { GraphQLSchema } from 'graphql';
|
|
2
|
-
import { GetArgs, IMocks, IMockStore, KeyTypeConstraints, Ref, SetArgs, TypePolicy } from './types.js';
|
|
3
|
-
export declare const defaultMocks: {
|
|
4
|
-
Int: () => number;
|
|
5
|
-
Float: () => number;
|
|
6
|
-
String: () => string;
|
|
7
|
-
Boolean: () => boolean;
|
|
8
|
-
ID: () => string;
|
|
9
|
-
};
|
|
2
|
+
import { GetArgs, IMocks, IMockStore, KeyTypeConstraints, MockGenerationBehavior, Ref, SetArgs, TypePolicy } from './types.js';
|
|
10
3
|
type Entity = {
|
|
11
4
|
[key: string]: unknown;
|
|
12
5
|
};
|
|
13
6
|
export declare class MockStore implements IMockStore {
|
|
14
7
|
schema: GraphQLSchema;
|
|
15
8
|
private mocks;
|
|
9
|
+
private mockGenerationBehavior;
|
|
16
10
|
private typePolicies;
|
|
17
11
|
private store;
|
|
18
|
-
constructor({ schema, mocks, typePolicies, }: {
|
|
12
|
+
constructor({ schema, mocks, mockGenerationBehavior, typePolicies, }: {
|
|
19
13
|
schema: GraphQLSchema;
|
|
20
14
|
mocks?: IMocks;
|
|
15
|
+
mockGenerationBehavior?: MockGenerationBehavior;
|
|
21
16
|
typePolicies?: {
|
|
22
17
|
[typeName: string]: TypePolicy;
|
|
23
18
|
};
|
|
@@ -57,6 +52,8 @@ export declare class MockStore implements IMockStore {
|
|
|
57
52
|
* Will create `MockStore` for the given `schema`.
|
|
58
53
|
*
|
|
59
54
|
* A `MockStore` will generate mock values for the given schema when queried.
|
|
55
|
+
* By default it'll generate random values, if this causes flakiness and you
|
|
56
|
+
* need a more deterministic behavior, use `mockGenerationBehavior` option.
|
|
60
57
|
*
|
|
61
58
|
* It will store generated mocks, so that, provided with same arguments
|
|
62
59
|
* the returned values will be the same.
|
|
@@ -87,6 +84,29 @@ export declare function createMockStore(options: {
|
|
|
87
84
|
* The mocks functions to use.
|
|
88
85
|
*/
|
|
89
86
|
mocks?: IMocks;
|
|
87
|
+
/**
|
|
88
|
+
* Configures the default behavior for Scalar, Enum, Union, and Array types.
|
|
89
|
+
*
|
|
90
|
+
* When set to `random`, then every time a value is generated for a field with
|
|
91
|
+
* one of these types
|
|
92
|
+
* - For Unions and Enums one of the allowed values will be picked randomly
|
|
93
|
+
* - For Arrays an array of random length will be generated
|
|
94
|
+
* - For Int and Float scalars a random number will be generated
|
|
95
|
+
* - For Boolean scalars either `true` or `false` will be returned randomly
|
|
96
|
+
*
|
|
97
|
+
* When set to `deterministic`, then
|
|
98
|
+
* - For Unions and Enums the first allowed value is picked
|
|
99
|
+
* - For Arrays an array of two elements will be generated
|
|
100
|
+
* - For Int and Float scalars values 1 and 1.5 will be used respectively
|
|
101
|
+
* - For Boolean scalars we'll always return `true`
|
|
102
|
+
* - For String scalars we'll always return "Hello World"
|
|
103
|
+
*
|
|
104
|
+
* Regardless of the chosen behavior, for ID scalars a random UUID string
|
|
105
|
+
* will always be generated.
|
|
106
|
+
*
|
|
107
|
+
* @default "random"
|
|
108
|
+
*/
|
|
109
|
+
mockGenerationBehavior?: MockGenerationBehavior;
|
|
90
110
|
typePolicies?: {
|
|
91
111
|
[typeName: string]: TypePolicy;
|
|
92
112
|
};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { GraphQLSchema } from 'graphql';
|
|
2
2
|
import { IResolvers } from '@graphql-tools/utils';
|
|
3
|
-
import { IMocks, IMockStore, TypePolicy } from './types.cjs';
|
|
3
|
+
import { IMocks, IMockStore, MockGenerationBehavior, TypePolicy } from './types.cjs';
|
|
4
4
|
type IMockOptions<TResolvers = IResolvers> = {
|
|
5
5
|
schema: GraphQLSchema;
|
|
6
6
|
store?: IMockStore;
|
|
7
7
|
mocks?: IMocks<TResolvers>;
|
|
8
|
+
mockGenerationBehavior?: MockGenerationBehavior;
|
|
8
9
|
typePolicies?: {
|
|
9
10
|
[typeName: string]: TypePolicy;
|
|
10
11
|
};
|
|
@@ -74,5 +75,5 @@ type IMockOptions<TResolvers = IResolvers> = {
|
|
|
74
75
|
*
|
|
75
76
|
* `Query` and `Mutation` type will use `key` `'ROOT'`.
|
|
76
77
|
*/
|
|
77
|
-
export declare function addMocksToSchema<TResolvers = IResolvers>({ schema, store: maybeStore, mocks, typePolicies, resolvers: resolversOrFnResolvers, preserveResolvers, }: IMockOptions<TResolvers>): GraphQLSchema;
|
|
78
|
+
export declare function addMocksToSchema<TResolvers = IResolvers>({ schema, store: maybeStore, mocks, mockGenerationBehavior, typePolicies, resolvers: resolversOrFnResolvers, preserveResolvers, }: IMockOptions<TResolvers>): GraphQLSchema;
|
|
78
79
|
export {};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { GraphQLSchema } from 'graphql';
|
|
2
2
|
import { IResolvers } from '@graphql-tools/utils';
|
|
3
|
-
import { IMocks, IMockStore, TypePolicy } from './types.js';
|
|
3
|
+
import { IMocks, IMockStore, MockGenerationBehavior, TypePolicy } from './types.js';
|
|
4
4
|
type IMockOptions<TResolvers = IResolvers> = {
|
|
5
5
|
schema: GraphQLSchema;
|
|
6
6
|
store?: IMockStore;
|
|
7
7
|
mocks?: IMocks<TResolvers>;
|
|
8
|
+
mockGenerationBehavior?: MockGenerationBehavior;
|
|
8
9
|
typePolicies?: {
|
|
9
10
|
[typeName: string]: TypePolicy;
|
|
10
11
|
};
|
|
@@ -74,5 +75,5 @@ type IMockOptions<TResolvers = IResolvers> = {
|
|
|
74
75
|
*
|
|
75
76
|
* `Query` and `Mutation` type will use `key` `'ROOT'`.
|
|
76
77
|
*/
|
|
77
|
-
export declare function addMocksToSchema<TResolvers = IResolvers>({ schema, store: maybeStore, mocks, typePolicies, resolvers: resolversOrFnResolvers, preserveResolvers, }: IMockOptions<TResolvers>): GraphQLSchema;
|
|
78
|
+
export declare function addMocksToSchema<TResolvers = IResolvers>({ schema, store: maybeStore, mocks, mockGenerationBehavior, typePolicies, resolvers: resolversOrFnResolvers, preserveResolvers, }: IMockOptions<TResolvers>): GraphQLSchema;
|
|
78
79
|
export {};
|
package/typings/mockServer.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TypeSource } from '@graphql-tools/utils';
|
|
2
|
-
import { IMocks, IMockServer } from './types.cjs';
|
|
2
|
+
import { IMocks, IMockServer, MockGenerationBehavior } from './types.cjs';
|
|
3
3
|
/**
|
|
4
4
|
* A convenience wrapper on top of `addMocksToSchema`. It adds your mock resolvers
|
|
5
5
|
* to your schema and returns a client that will correctly execute your query with
|
|
@@ -11,5 +11,7 @@ import { IMocks, IMockServer } from './types.cjs';
|
|
|
11
11
|
* @param preserveResolvers Set to `true` to prevent existing resolvers from being
|
|
12
12
|
* overwritten to provide mock data. This can be used to mock some parts of the
|
|
13
13
|
* server and not others.
|
|
14
|
+
* @param mockGenerationBehavior Set to `'deterministic'` if the default random
|
|
15
|
+
* mock generation behavior causes flakiness.
|
|
14
16
|
*/
|
|
15
|
-
export declare function mockServer<TResolvers>(schema: TypeSource, mocks: IMocks<TResolvers>, preserveResolvers?: boolean): IMockServer;
|
|
17
|
+
export declare function mockServer<TResolvers>(schema: TypeSource, mocks: IMocks<TResolvers>, preserveResolvers?: boolean, mockGenerationBehavior?: MockGenerationBehavior): IMockServer;
|
package/typings/mockServer.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TypeSource } from '@graphql-tools/utils';
|
|
2
|
-
import { IMocks, IMockServer } from './types.js';
|
|
2
|
+
import { IMocks, IMockServer, MockGenerationBehavior } from './types.js';
|
|
3
3
|
/**
|
|
4
4
|
* A convenience wrapper on top of `addMocksToSchema`. It adds your mock resolvers
|
|
5
5
|
* to your schema and returns a client that will correctly execute your query with
|
|
@@ -11,5 +11,7 @@ import { IMocks, IMockServer } from './types.js';
|
|
|
11
11
|
* @param preserveResolvers Set to `true` to prevent existing resolvers from being
|
|
12
12
|
* overwritten to provide mock data. This can be used to mock some parts of the
|
|
13
13
|
* server and not others.
|
|
14
|
+
* @param mockGenerationBehavior Set to `'deterministic'` if the default random
|
|
15
|
+
* mock generation behavior causes flakiness.
|
|
14
16
|
*/
|
|
15
|
-
export declare function mockServer<TResolvers>(schema: TypeSource, mocks: IMocks<TResolvers>, preserveResolvers?: boolean): IMockServer;
|
|
17
|
+
export declare function mockServer<TResolvers>(schema: TypeSource, mocks: IMocks<TResolvers>, preserveResolvers?: boolean, mockGenerationBehavior?: MockGenerationBehavior): IMockServer;
|
package/typings/types.d.cts
CHANGED
|
@@ -14,6 +14,7 @@ export type IMocks<TResolvers = IResolvers> = {
|
|
|
14
14
|
} & {
|
|
15
15
|
[typeOrScalarName: string]: IScalarMock | ITypeMock;
|
|
16
16
|
};
|
|
17
|
+
export type MockGenerationBehavior = 'random' | 'deterministic';
|
|
17
18
|
export type KeyTypeConstraints = string | number;
|
|
18
19
|
export type TypePolicy = {
|
|
19
20
|
/**
|
package/typings/types.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ export type IMocks<TResolvers = IResolvers> = {
|
|
|
14
14
|
} & {
|
|
15
15
|
[typeOrScalarName: string]: IScalarMock | ITypeMock;
|
|
16
16
|
};
|
|
17
|
+
export type MockGenerationBehavior = 'random' | 'deterministic';
|
|
17
18
|
export type KeyTypeConstraints = string | number;
|
|
18
19
|
export type TypePolicy = {
|
|
19
20
|
/**
|
package/typings/utils.d.cts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { GraphQLObjectType, GraphQLSchema } from 'graphql';
|
|
2
|
-
import { KeyTypeConstraints, Ref } from './types.cjs';
|
|
2
|
+
import { KeyTypeConstraints, MockGenerationBehavior, Ref } from './types.cjs';
|
|
3
3
|
export declare function uuidv4(): string;
|
|
4
4
|
export declare const randomListLength: () => number;
|
|
5
|
-
export declare const
|
|
5
|
+
export declare const takeOneOf: <T>(arr: T[], behavior: MockGenerationBehavior) => T;
|
|
6
6
|
export declare function makeRef<KeyT extends KeyTypeConstraints = string>(typeName: string, key: KeyT): Ref<KeyT>;
|
|
7
7
|
export declare function isObject(thing: any): boolean;
|
|
8
8
|
export declare function copyOwnPropsIfNotPresent(target: Record<string, any>, source: Record<string, any>): void;
|
package/typings/utils.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { GraphQLObjectType, GraphQLSchema } from 'graphql';
|
|
2
|
-
import { KeyTypeConstraints, Ref } from './types.js';
|
|
2
|
+
import { KeyTypeConstraints, MockGenerationBehavior, Ref } from './types.js';
|
|
3
3
|
export declare function uuidv4(): string;
|
|
4
4
|
export declare const randomListLength: () => number;
|
|
5
|
-
export declare const
|
|
5
|
+
export declare const takeOneOf: <T>(arr: T[], behavior: MockGenerationBehavior) => T;
|
|
6
6
|
export declare function makeRef<KeyT extends KeyTypeConstraints = string>(typeName: string, key: KeyT): Ref<KeyT>;
|
|
7
7
|
export declare function isObject(thing: any): boolean;
|
|
8
8
|
export declare function copyOwnPropsIfNotPresent(target: Record<string, any>, source: Record<string, any>): void;
|