@graphql-tools/mock 8.7.2-alpha-e7752ba5.0 → 8.7.2-alpha-b9e4a92b.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/cjs/MockStore.js +1 -1
- package/cjs/addMocksToSchema.js +1 -1
- package/cjs/mockServer.js +1 -1
- package/esm/MockStore.js +1 -1
- package/esm/addMocksToSchema.js +1 -1
- package/esm/mockServer.js +1 -1
- package/package.json +8 -6
- package/typings/MockList.d.cts +25 -0
- package/typings/MockStore.d.cts +94 -0
- package/typings/MockStore.d.ts +1 -1
- package/typings/addMocksToSchema.d.cts +78 -0
- package/typings/addMocksToSchema.d.ts +1 -1
- package/typings/index.d.cts +6 -0
- package/typings/mockServer.d.cts +15 -0
- package/typings/pagination.d.cts +86 -0
- package/typings/pagination.d.ts +1 -1
- package/typings/types.d.cts +203 -0
- package/typings/types.d.ts +1 -1
- package/typings/utils.d.cts +10 -0
- package/typings/utils.d.ts +1 -1
package/cjs/MockStore.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createMockStore = exports.MockStore = exports.defaultMocks = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const graphql_1 = require("
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
6
|
const fast_json_stable_stringify_1 = tslib_1.__importDefault(require("fast-json-stable-stringify"));
|
|
7
7
|
const types_js_1 = require("./types.js");
|
|
8
8
|
const utils_js_1 = require("./utils.js");
|
package/cjs/addMocksToSchema.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.addMocksToSchema = void 0;
|
|
4
|
-
const graphql_1 = require("
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
5
|
const utils_1 = require("@graphql-tools/utils");
|
|
6
6
|
const schema_1 = require("@graphql-tools/schema");
|
|
7
7
|
const types_js_1 = require("./types.js");
|
package/cjs/mockServer.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.mockServer = void 0;
|
|
4
|
-
const graphql_1 = require("
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
5
|
const schema_1 = require("@graphql-tools/schema");
|
|
6
6
|
const addMocksToSchema_js_1 = require("./addMocksToSchema.js");
|
|
7
7
|
/**
|
package/esm/MockStore.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GraphQLString, isObjectType, isScalarType, getNullableType, isListType, isEnumType, isAbstractType, isCompositeType, isNullableType, isInterfaceType, } from '
|
|
1
|
+
import { GraphQLString, isObjectType, isScalarType, getNullableType, isListType, isEnumType, isAbstractType, isCompositeType, isNullableType, isInterfaceType, } from 'graphql';
|
|
2
2
|
import stringify from 'fast-json-stable-stringify';
|
|
3
3
|
import { isRef, assertIsRef, isRecord, } from './types.js';
|
|
4
4
|
import { uuidv4, randomListLength, takeRandom, makeRef } from './utils.js';
|
package/esm/addMocksToSchema.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defaultFieldResolver, isUnionType, GraphQLUnionType, GraphQLInterfaceType, isSchema, } from '
|
|
1
|
+
import { defaultFieldResolver, isUnionType, GraphQLUnionType, GraphQLInterfaceType, isSchema, } from 'graphql';
|
|
2
2
|
import { mapSchema, MapperKind } from '@graphql-tools/utils';
|
|
3
3
|
import { addResolversToSchema } from '@graphql-tools/schema';
|
|
4
4
|
import { isRef } from './types.js';
|
package/esm/mockServer.js
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-tools/mock",
|
|
3
|
-
"version": "8.7.2-alpha-
|
|
3
|
+
"version": "8.7.2-alpha-b9e4a92b.0",
|
|
4
4
|
"description": "A set of utils for faster development of GraphQL tools",
|
|
5
5
|
"sideEffects": false,
|
|
6
|
+
"peerDependencies": {
|
|
7
|
+
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
|
|
8
|
+
},
|
|
6
9
|
"dependencies": {
|
|
7
|
-
"@graphql-tools/schema": "8.5.2-alpha-
|
|
8
|
-
"@graphql-tools/utils": "8.9.1-alpha-
|
|
9
|
-
"@graphql-tools/graphql": "0.1.0-alpha-e7752ba5.0",
|
|
10
|
+
"@graphql-tools/schema": "8.5.2-alpha-b9e4a92b.0",
|
|
11
|
+
"@graphql-tools/utils": "8.9.1-alpha-b9e4a92b.0",
|
|
10
12
|
"fast-json-stable-stringify": "^2.1.0",
|
|
11
13
|
"tslib": "^2.4.0"
|
|
12
14
|
},
|
|
@@ -26,7 +28,7 @@
|
|
|
26
28
|
"exports": {
|
|
27
29
|
".": {
|
|
28
30
|
"require": {
|
|
29
|
-
"types": "./typings/index.d.
|
|
31
|
+
"types": "./typings/index.d.cts",
|
|
30
32
|
"default": "./cjs/index.js"
|
|
31
33
|
},
|
|
32
34
|
"import": {
|
|
@@ -40,7 +42,7 @@
|
|
|
40
42
|
},
|
|
41
43
|
"./*": {
|
|
42
44
|
"require": {
|
|
43
|
-
"types": "./typings/*.d.
|
|
45
|
+
"types": "./typings/*.d.cts",
|
|
44
46
|
"default": "./cjs/*.js"
|
|
45
47
|
},
|
|
46
48
|
"import": {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
export declare function isMockList(obj: any): obj is MockList;
|
|
5
|
+
/**
|
|
6
|
+
* This is an object you can return from your mock resolvers which calls the
|
|
7
|
+
* provided `mockFunction` once for each list item.
|
|
8
|
+
*/
|
|
9
|
+
export declare class MockList {
|
|
10
|
+
private readonly len;
|
|
11
|
+
private readonly wrappedFunction;
|
|
12
|
+
/**
|
|
13
|
+
* @param length Either the exact length of items to return or an inclusive
|
|
14
|
+
* range of possible lengths.
|
|
15
|
+
* @param mockFunction The function to call for each item in the list to
|
|
16
|
+
* resolve it. It can return another MockList or a value.
|
|
17
|
+
*/
|
|
18
|
+
constructor(length: number | Array<number>, mockFunction?: () => unknown);
|
|
19
|
+
/**
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
mock(): unknown[];
|
|
23
|
+
private randint;
|
|
24
|
+
}
|
|
25
|
+
export declare function deepResolveMockList(mockList: MockList): unknown[];
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { GraphQLSchema } from 'graphql';
|
|
2
|
+
import { IMockStore, GetArgs, SetArgs, Ref, TypePolicy, IMocks, KeyTypeConstraints } from './types.cjs';
|
|
3
|
+
export declare const defaultMocks: {
|
|
4
|
+
Int: () => number;
|
|
5
|
+
Float: () => number;
|
|
6
|
+
String: () => string;
|
|
7
|
+
Boolean: () => boolean;
|
|
8
|
+
ID: () => string;
|
|
9
|
+
};
|
|
10
|
+
declare type Entity = {
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
};
|
|
13
|
+
export declare class MockStore implements IMockStore {
|
|
14
|
+
schema: GraphQLSchema;
|
|
15
|
+
private mocks;
|
|
16
|
+
private typePolicies;
|
|
17
|
+
private store;
|
|
18
|
+
constructor({ schema, mocks, typePolicies, }: {
|
|
19
|
+
schema: GraphQLSchema;
|
|
20
|
+
mocks?: IMocks;
|
|
21
|
+
typePolicies?: {
|
|
22
|
+
[typeName: string]: TypePolicy;
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
has<KeyT extends KeyTypeConstraints = string>(typeName: string, key: KeyT): boolean;
|
|
26
|
+
get<KeyT extends KeyTypeConstraints = string, ReturnKeyT extends KeyTypeConstraints = string>(_typeName: string | Ref<KeyT> | GetArgs<KeyT>, _key?: KeyT | {
|
|
27
|
+
[fieldName: string]: any;
|
|
28
|
+
} | string | string[], _fieldName?: string | string[] | {
|
|
29
|
+
[fieldName: string]: any;
|
|
30
|
+
} | string | {
|
|
31
|
+
[argName: string]: any;
|
|
32
|
+
}, _fieldArgs?: string | {
|
|
33
|
+
[argName: string]: any;
|
|
34
|
+
}): unknown | Ref<ReturnKeyT>;
|
|
35
|
+
set<KeyT extends KeyTypeConstraints>(_typeName: string | Ref<KeyT> | SetArgs<KeyT>, _key?: KeyT | string | {
|
|
36
|
+
[fieldName: string]: any;
|
|
37
|
+
}, _fieldName?: string | {
|
|
38
|
+
[fieldName: string]: any;
|
|
39
|
+
} | unknown, _value?: unknown): void;
|
|
40
|
+
reset(): void;
|
|
41
|
+
filter(key: string, predicate: (val: Entity) => boolean): Entity[];
|
|
42
|
+
find(key: string, predicate: (val: Entity) => boolean): Entity | undefined;
|
|
43
|
+
private getImpl;
|
|
44
|
+
private setImpl;
|
|
45
|
+
private normalizeValueToStore;
|
|
46
|
+
private insert;
|
|
47
|
+
private generateFieldValue;
|
|
48
|
+
private generateFieldValueFromMocks;
|
|
49
|
+
private generateKeyForType;
|
|
50
|
+
private generateValueFromType;
|
|
51
|
+
private getFieldType;
|
|
52
|
+
private getType;
|
|
53
|
+
private isKeyField;
|
|
54
|
+
private getKeyFieldName;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Will create `MockStore` for the given `schema`.
|
|
58
|
+
*
|
|
59
|
+
* A `MockStore` will generate mock values for the given schem when queried.
|
|
60
|
+
*
|
|
61
|
+
* It will stores generated mocks, so that, provided with same arguments
|
|
62
|
+
* the returned values will be the same.
|
|
63
|
+
*
|
|
64
|
+
* Its API also allows to modify the stored values.
|
|
65
|
+
*
|
|
66
|
+
* Basic example:
|
|
67
|
+
* ```ts
|
|
68
|
+
* store.get('User', 1, 'name');
|
|
69
|
+
* // > "Hello World"
|
|
70
|
+
* store.set('User', 1, 'name', 'Alexandre');
|
|
71
|
+
* store.get('User', 1, 'name');
|
|
72
|
+
* // > "Alexandre"
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* The storage key will correspond to the "key field"
|
|
76
|
+
* of the type. Field with name `id` or `_id` will be
|
|
77
|
+
* by default considered as the key field for the type.
|
|
78
|
+
* However, use `typePolicies` to precise the field to use
|
|
79
|
+
* as key.
|
|
80
|
+
*/
|
|
81
|
+
export declare function createMockStore(options: {
|
|
82
|
+
/**
|
|
83
|
+
* The `schema` to based mocks on.
|
|
84
|
+
*/
|
|
85
|
+
schema: GraphQLSchema;
|
|
86
|
+
/**
|
|
87
|
+
* The mocks functions to use.
|
|
88
|
+
*/
|
|
89
|
+
mocks?: IMocks;
|
|
90
|
+
typePolicies?: {
|
|
91
|
+
[typeName: string]: TypePolicy;
|
|
92
|
+
};
|
|
93
|
+
}): IMockStore;
|
|
94
|
+
export {};
|
package/typings/MockStore.d.ts
CHANGED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { GraphQLSchema } from 'graphql';
|
|
2
|
+
import { IResolvers } from '@graphql-tools/utils';
|
|
3
|
+
import { IMockStore, IMocks, TypePolicy } from './types.cjs';
|
|
4
|
+
declare type IMockOptions<TResolvers = IResolvers> = {
|
|
5
|
+
schema: GraphQLSchema;
|
|
6
|
+
store?: IMockStore;
|
|
7
|
+
mocks?: IMocks<TResolvers>;
|
|
8
|
+
typePolicies?: {
|
|
9
|
+
[typeName: string]: TypePolicy;
|
|
10
|
+
};
|
|
11
|
+
resolvers?: Partial<TResolvers> | ((store: IMockStore) => Partial<TResolvers>);
|
|
12
|
+
/**
|
|
13
|
+
* Set to `true` to prevent existing resolvers from being
|
|
14
|
+
* overwritten to provide mock data. This can be used to mock some parts of the
|
|
15
|
+
* server and not others.
|
|
16
|
+
*/
|
|
17
|
+
preserveResolvers?: boolean;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Given a `schema` and a `MockStore`, returns an executable schema that
|
|
21
|
+
* will use the provided `MockStore` to execute queries.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* const schema = buildSchema(`
|
|
25
|
+
* type User {
|
|
26
|
+
* id: ID!
|
|
27
|
+
* name: String!
|
|
28
|
+
* }
|
|
29
|
+
* type Query {
|
|
30
|
+
* me: User!
|
|
31
|
+
* }
|
|
32
|
+
* `)
|
|
33
|
+
*
|
|
34
|
+
* const store = createMockStore({ schema });
|
|
35
|
+
* const mockedSchema = addMocksToSchema({ schema, store });
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
*
|
|
39
|
+
* If a `resolvers` parameter is passed, the query execution will use
|
|
40
|
+
* the provided `resolvers` if, one exists, instead of the default mock
|
|
41
|
+
* resolver.
|
|
42
|
+
*
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* const schema = buildSchema(`
|
|
46
|
+
* type User {
|
|
47
|
+
* id: ID!
|
|
48
|
+
* name: String!
|
|
49
|
+
* }
|
|
50
|
+
* type Query {
|
|
51
|
+
* me: User!
|
|
52
|
+
* }
|
|
53
|
+
* type Mutation {
|
|
54
|
+
* setMyName(newName: String!): User!
|
|
55
|
+
* }
|
|
56
|
+
* `)
|
|
57
|
+
*
|
|
58
|
+
* const store = createMockStore({ schema });
|
|
59
|
+
* const mockedSchema = addMocksToSchema({
|
|
60
|
+
* schema,
|
|
61
|
+
* store,
|
|
62
|
+
* resolvers: {
|
|
63
|
+
* Mutation: {
|
|
64
|
+
* setMyName: (_, { newName }) => {
|
|
65
|
+
* const ref = store.get('Query', 'ROOT', 'viewer');
|
|
66
|
+
* store.set(ref, 'name', newName);
|
|
67
|
+
* return ref;
|
|
68
|
+
* }
|
|
69
|
+
* }
|
|
70
|
+
* }
|
|
71
|
+
* });
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
*
|
|
75
|
+
* `Query` and `Mutation` type will use `key` `'ROOT'`.
|
|
76
|
+
*/
|
|
77
|
+
export declare function addMocksToSchema<TResolvers = IResolvers>({ schema, store: maybeStore, mocks, typePolicies, resolvers: resolversOrFnResolvers, preserveResolvers, }: IMockOptions<TResolvers>): GraphQLSchema;
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { TypeSource } from '@graphql-tools/utils';
|
|
2
|
+
import { IMockServer, IMocks } from './types.cjs';
|
|
3
|
+
/**
|
|
4
|
+
* A convenience wrapper on top of addMocksToSchema. It adds your mock resolvers
|
|
5
|
+
* to your schema and returns a client that will correctly execute your query with
|
|
6
|
+
* variables. Note: when executing queries from the returned server, context and
|
|
7
|
+
* root will both equal `{}`.
|
|
8
|
+
* @param schema The schema to which to add mocks. This can also be a set of type
|
|
9
|
+
* definitions instead.
|
|
10
|
+
* @param mocks The mocks to add to the schema.
|
|
11
|
+
* @param preserveResolvers Set to `true` to prevent existing resolvers from being
|
|
12
|
+
* overwritten to provide mock data. This can be used to mock some parts of the
|
|
13
|
+
* server and not others.
|
|
14
|
+
*/
|
|
15
|
+
export declare function mockServer<TResolvers>(schema: TypeSource, mocks: IMocks<TResolvers>, preserveResolvers?: boolean): IMockServer;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { IFieldResolver } from '@graphql-tools/utils';
|
|
2
|
+
import { GraphQLResolveInfo } from 'graphql';
|
|
3
|
+
import { IMockStore, Ref } from './types.cjs';
|
|
4
|
+
export declare type AllNodesFn<TContext, TArgs extends RelayPaginationParams> = (parent: Ref, args: TArgs, context: TContext, info: GraphQLResolveInfo) => Ref[];
|
|
5
|
+
export declare type RelayStylePaginationMockOptions<TContext, TArgs extends RelayPaginationParams> = {
|
|
6
|
+
/**
|
|
7
|
+
* Use this option to apply filtering or sorting on the nodes given the
|
|
8
|
+
* arguments the paginated field receives.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* {
|
|
12
|
+
* User: {
|
|
13
|
+
* friends: mockedRelayStylePagination<
|
|
14
|
+
* unknown,
|
|
15
|
+
* RelayPaginationParams & { sortByBirthdateDesc?: boolean}
|
|
16
|
+
* >(
|
|
17
|
+
* store, {
|
|
18
|
+
* applyOnEdges: (edges, { sortByBirthdateDesc }) => {
|
|
19
|
+
* if (!sortByBirthdateDesc) return edges
|
|
20
|
+
* return _.sortBy(edges, (e) => store.get(e, ['node', 'birthdate']))
|
|
21
|
+
* }
|
|
22
|
+
* }),
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
applyOnNodes?: (nodeRefs: Ref[], args: TArgs) => Ref[];
|
|
28
|
+
/**
|
|
29
|
+
* A function that'll be used to get all the nodes used for pagination.
|
|
30
|
+
*
|
|
31
|
+
* By default, it will use the nodes of the field this pagination is attached to.
|
|
32
|
+
*
|
|
33
|
+
* This option is handy when several paginable fields should share
|
|
34
|
+
* the same base nodes:
|
|
35
|
+
* ```ts
|
|
36
|
+
* {
|
|
37
|
+
* User: {
|
|
38
|
+
* friends: mockedRelayStylePagination(store),
|
|
39
|
+
* maleFriends: mockedRelayStylePagination(store, {
|
|
40
|
+
* allNodesFn: (userRef) =>
|
|
41
|
+
* store
|
|
42
|
+
* .get(userRef, ['friends', 'edges'])
|
|
43
|
+
* .map((e) => store.get(e, 'node'))
|
|
44
|
+
* .filter((userRef) => store.get(userRef, 'sex') === 'male')
|
|
45
|
+
* })
|
|
46
|
+
* }
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
allNodesFn?: AllNodesFn<TContext, TArgs>;
|
|
51
|
+
/**
|
|
52
|
+
* The function that'll be used to compute the cursor of a node.
|
|
53
|
+
*
|
|
54
|
+
* By default, it'll use `MockStore` internal reference `Ref`'s `key`
|
|
55
|
+
* as cursor.
|
|
56
|
+
*/
|
|
57
|
+
cursorFn?: (nodeRef: Ref) => string;
|
|
58
|
+
};
|
|
59
|
+
export declare type RelayPaginationParams = {
|
|
60
|
+
first?: number;
|
|
61
|
+
after?: string;
|
|
62
|
+
last?: number;
|
|
63
|
+
before?: string;
|
|
64
|
+
};
|
|
65
|
+
export declare type RelayPageInfo = {
|
|
66
|
+
hasPreviousPage: boolean;
|
|
67
|
+
hasNextPage: boolean;
|
|
68
|
+
startCursor: string;
|
|
69
|
+
endCursor: string;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Produces a resolver that'll mock a [Relay-style cursor pagination](https://relay.dev/graphql/connections.htm).
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* const schemaWithMocks = addMocksToSchema({
|
|
76
|
+
* schema,
|
|
77
|
+
* resolvers: (store) => ({
|
|
78
|
+
* User: {
|
|
79
|
+
* friends: relayStylePaginationMock(store),
|
|
80
|
+
* }
|
|
81
|
+
* }),
|
|
82
|
+
* })
|
|
83
|
+
* ```
|
|
84
|
+
* @param store the MockStore
|
|
85
|
+
*/
|
|
86
|
+
export declare const relayStylePaginationMock: <TContext, TArgs extends RelayPaginationParams = RelayPaginationParams>(store: IMockStore, { cursorFn, applyOnNodes, allNodesFn, }?: RelayStylePaginationMockOptions<TContext, TArgs>) => IFieldResolver<Ref<string>, TContext, TArgs, any>;
|
package/typings/pagination.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IFieldResolver } from '@graphql-tools/utils';
|
|
2
|
-
import { GraphQLResolveInfo } from '
|
|
2
|
+
import { GraphQLResolveInfo } from 'graphql';
|
|
3
3
|
import { IMockStore, Ref } from './types.js';
|
|
4
4
|
export declare type AllNodesFn<TContext, TArgs extends RelayPaginationParams> = (parent: Ref, args: TArgs, context: TContext, info: GraphQLResolveInfo) => Ref[];
|
|
5
5
|
export declare type RelayStylePaginationMockOptions<TContext, TArgs extends RelayPaginationParams> = {
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { IResolvers } from '@graphql-tools/utils';
|
|
2
|
+
import { ExecutionResult, GraphQLSchema } from 'graphql';
|
|
3
|
+
export declare type IMockFn = () => unknown;
|
|
4
|
+
export declare type IScalarMock = unknown | IMockFn;
|
|
5
|
+
export declare type ITypeMock = () => {
|
|
6
|
+
[fieldName: string]: unknown | IMockFn;
|
|
7
|
+
} | {
|
|
8
|
+
[fieldName: string]: IMockFn;
|
|
9
|
+
};
|
|
10
|
+
export declare type IMocks<TResolvers = IResolvers> = {
|
|
11
|
+
[TTypeName in keyof TResolvers]?: {
|
|
12
|
+
[TFieldName in keyof TResolvers[TTypeName]]: TResolvers[TTypeName][TFieldName] extends (args: any) => any ? () => ReturnType<TResolvers[TTypeName][TFieldName]> | ReturnType<TResolvers[TTypeName][TFieldName]> : TResolvers[TTypeName][TFieldName];
|
|
13
|
+
};
|
|
14
|
+
} & {
|
|
15
|
+
[typeOrScalarName: string]: IScalarMock | ITypeMock;
|
|
16
|
+
};
|
|
17
|
+
export declare type KeyTypeConstraints = string | number;
|
|
18
|
+
export declare type TypePolicy = {
|
|
19
|
+
/**
|
|
20
|
+
* The name of the field that should be used as store `key`.
|
|
21
|
+
*
|
|
22
|
+
* If `false`, no field will be used and `id` or `_id` will be used,
|
|
23
|
+
* otherwise we'll generate a random string
|
|
24
|
+
* as key.
|
|
25
|
+
*/
|
|
26
|
+
keyFieldName?: string | false;
|
|
27
|
+
};
|
|
28
|
+
export declare type GetArgs<KeyT extends KeyTypeConstraints = string> = {
|
|
29
|
+
typeName: string;
|
|
30
|
+
key?: KeyT;
|
|
31
|
+
fieldName?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Optional arguments when querying the field.
|
|
34
|
+
*
|
|
35
|
+
* Querying the field with the same arguments will return
|
|
36
|
+
* the same value. Deep equality is checked.
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* store.get('User', 1, 'friend', { id: 2 }) === store.get('User', 1, 'friend', { id: 2 })
|
|
40
|
+
* store.get('User', 1, 'friend', { id: 2 }) !== store.get('User', 1, 'friend')
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* Args can be a record, just like `args` argument of field resolver or an
|
|
44
|
+
* arbitrary string.
|
|
45
|
+
*/
|
|
46
|
+
fieldArgs?: string | {
|
|
47
|
+
[argName: string]: any;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* If no value found, insert the `defaultValue`.
|
|
51
|
+
*/
|
|
52
|
+
defaultValue?: unknown | {
|
|
53
|
+
[fieldName: string]: any;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
export declare type SetArgs<KeyT extends KeyTypeConstraints = string> = {
|
|
57
|
+
typeName: string;
|
|
58
|
+
key: KeyT;
|
|
59
|
+
fieldName?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Optional arguments when querying the field.
|
|
62
|
+
*
|
|
63
|
+
* @see GetArgs#fieldArgs
|
|
64
|
+
*/
|
|
65
|
+
fieldArgs?: string | {
|
|
66
|
+
[argName: string]: any;
|
|
67
|
+
};
|
|
68
|
+
value?: unknown | {
|
|
69
|
+
[fieldName: string]: any;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* If the value for this field is already set, it won't
|
|
73
|
+
* be overridden.
|
|
74
|
+
*
|
|
75
|
+
* Propagates down do nested `set`.
|
|
76
|
+
*/
|
|
77
|
+
noOverride?: boolean;
|
|
78
|
+
};
|
|
79
|
+
export interface IMockStore {
|
|
80
|
+
schema: GraphQLSchema;
|
|
81
|
+
/**
|
|
82
|
+
* Get a field value from the store for the given type, key and field
|
|
83
|
+
* name — and optionally field arguments. If the field name is not given,
|
|
84
|
+
* a reference to the type will be returned.
|
|
85
|
+
*
|
|
86
|
+
* If the the value for this field is not set, a value will be
|
|
87
|
+
* generated according to field return type and mock functions.
|
|
88
|
+
*
|
|
89
|
+
* If the field's output type is a `ObjectType` (or list of `ObjectType`),
|
|
90
|
+
* it will return a `Ref` (or array of `Ref`), ie a reference to an entity
|
|
91
|
+
* in the store.
|
|
92
|
+
*
|
|
93
|
+
* Example:
|
|
94
|
+
* ```ts
|
|
95
|
+
* store.get('Query', 'ROOT', 'viewer');
|
|
96
|
+
* > { $ref: { key: 'abc-737dh-djdjd', typeName: 'User' } }
|
|
97
|
+
* store.get('User', 'abc-737dh-djdjd', 'name')
|
|
98
|
+
* > "Hello World"
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
get<KeyT extends KeyTypeConstraints = string, ReturnKeyT extends KeyTypeConstraints = string>(args: GetArgs<KeyT>): unknown | Ref<ReturnKeyT>;
|
|
102
|
+
/**
|
|
103
|
+
* Shorthand for `get({typeName, key, fieldName, fieldArgs})`.
|
|
104
|
+
*/
|
|
105
|
+
get<KeyT extends KeyTypeConstraints = string, ReturnKeyT extends KeyTypeConstraints = string>(typeName: string, key: KeyT, fieldNameOrFieldNames: string | string[], fieldArgs?: string | {
|
|
106
|
+
[argName: string]: any;
|
|
107
|
+
}): unknown | Ref<ReturnKeyT>;
|
|
108
|
+
/**
|
|
109
|
+
* Get a reference to the type.
|
|
110
|
+
*/
|
|
111
|
+
get<KeyT extends KeyTypeConstraints = string>(typeName: string, keyOrDefaultValue?: KeyT | {
|
|
112
|
+
[fieldName: string]: any;
|
|
113
|
+
}, defaultValue?: {
|
|
114
|
+
[fieldName: string]: any;
|
|
115
|
+
}): unknown | Ref<KeyT>;
|
|
116
|
+
/**
|
|
117
|
+
* Shorthand for `get({typeName: ref.$ref.typeName, key: ref.$ref.key, fieldName, fieldArgs})`
|
|
118
|
+
* @param ref
|
|
119
|
+
* @param fieldNameOrFieldNames
|
|
120
|
+
* @param fieldArgs
|
|
121
|
+
*/
|
|
122
|
+
get<KeyT extends KeyTypeConstraints = string, ReturnKeyT extends KeyTypeConstraints = string>(ref: Ref<KeyT>, fieldNameOrFieldNames: string | string[], fieldArgs?: string | {
|
|
123
|
+
[argName: string]: any;
|
|
124
|
+
}): unknown | Ref<ReturnKeyT>;
|
|
125
|
+
/**
|
|
126
|
+
* Set a field value in the store for the given type, key and field
|
|
127
|
+
* name — and optionally field arguments.
|
|
128
|
+
*
|
|
129
|
+
* If the the field return type is an `ObjectType` or a list of
|
|
130
|
+
* `ObjectType`, you can set references to other entity as value:
|
|
131
|
+
*
|
|
132
|
+
* ```ts
|
|
133
|
+
* // set the viewer name
|
|
134
|
+
* store.set('User', 1, 'name', 'Alexandre);
|
|
135
|
+
* store.set('Query', 'ROOT', 'viewer', store.get('User', 1));
|
|
136
|
+
*
|
|
137
|
+
* // set the friends of viewer
|
|
138
|
+
* store.set('User', 2, 'name', 'Emily');
|
|
139
|
+
* store.set('User', 3, 'name', 'Caroline');
|
|
140
|
+
* store.set('User', 1, 'friends', [store.get('User', 2), store.get('User', 3)]);
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
* But it also supports nested set:
|
|
144
|
+
*
|
|
145
|
+
* ```ts
|
|
146
|
+
* store.set('Query', 'ROOT', 'viewer', {
|
|
147
|
+
* name: 'Alexandre',
|
|
148
|
+
* friends: [
|
|
149
|
+
* { name: 'Emily' }
|
|
150
|
+
* { name: 'Caroline }
|
|
151
|
+
* ]
|
|
152
|
+
* });
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
set<KeyT extends KeyTypeConstraints = string>(args: SetArgs<KeyT>): void;
|
|
156
|
+
/**
|
|
157
|
+
* Shorthand for `set({typeName, key, fieldName, value})`.
|
|
158
|
+
*/
|
|
159
|
+
set<KeyT extends KeyTypeConstraints = string>(typeName: string, key: KeyT, fieldName: string, value?: unknown): void;
|
|
160
|
+
/**
|
|
161
|
+
* Set the given field values to the type with key.
|
|
162
|
+
*/
|
|
163
|
+
set<KeyT extends KeyTypeConstraints = string>(typeName: string, key: KeyT, values: {
|
|
164
|
+
[fieldName: string]: any;
|
|
165
|
+
}): void;
|
|
166
|
+
/**
|
|
167
|
+
* Shorthand for `set({ref.$ref.typeName, ref.$ref.key, fieldName, value})`.
|
|
168
|
+
*/
|
|
169
|
+
set<KeyT extends KeyTypeConstraints = string>(ref: Ref<KeyT>, fieldName: string, value?: unknown): void;
|
|
170
|
+
/**
|
|
171
|
+
* Set the given field values to the type with ref.
|
|
172
|
+
*/
|
|
173
|
+
set<KeyT extends KeyTypeConstraints = string>(ref: Ref<KeyT>, values: {
|
|
174
|
+
[fieldName: string]: any;
|
|
175
|
+
}): void;
|
|
176
|
+
/**
|
|
177
|
+
* Checks if a mock is present in the store for the given typeName and key.
|
|
178
|
+
*/
|
|
179
|
+
has<KeyT extends KeyTypeConstraints = string>(typeName: string, key: KeyT): boolean;
|
|
180
|
+
/**
|
|
181
|
+
* Resets the mock store
|
|
182
|
+
*/
|
|
183
|
+
reset(): void;
|
|
184
|
+
}
|
|
185
|
+
export declare type Ref<KeyT extends KeyTypeConstraints = string> = {
|
|
186
|
+
$ref: {
|
|
187
|
+
key: KeyT;
|
|
188
|
+
typeName: string;
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
export declare function isRef<KeyT extends KeyTypeConstraints = string>(maybeRef: unknown): maybeRef is Ref<KeyT>;
|
|
192
|
+
export declare function assertIsRef<KeyT extends KeyTypeConstraints = string>(maybeRef: unknown, message?: string): asserts maybeRef is Ref<KeyT>;
|
|
193
|
+
export declare function isRecord(obj: unknown): obj is {
|
|
194
|
+
[key: string]: unknown;
|
|
195
|
+
};
|
|
196
|
+
export interface IMockServer {
|
|
197
|
+
/**
|
|
198
|
+
* Executes the provided query against the mocked schema.
|
|
199
|
+
* @param query GraphQL query to execute
|
|
200
|
+
* @param vars Variables
|
|
201
|
+
*/
|
|
202
|
+
query: (query: string, vars?: Record<string, any>) => Promise<ExecutionResult>;
|
|
203
|
+
}
|
package/typings/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IResolvers } from '@graphql-tools/utils';
|
|
2
|
-
import { ExecutionResult, GraphQLSchema } from '
|
|
2
|
+
import { ExecutionResult, GraphQLSchema } from 'graphql';
|
|
3
3
|
export declare type IMockFn = () => unknown;
|
|
4
4
|
export declare type IScalarMock = unknown | IMockFn;
|
|
5
5
|
export declare type ITypeMock = () => {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { GraphQLObjectType, GraphQLSchema } from 'graphql';
|
|
2
|
+
import { Ref, KeyTypeConstraints } from './types.cjs';
|
|
3
|
+
export declare function uuidv4(): string;
|
|
4
|
+
export declare const randomListLength: () => number;
|
|
5
|
+
export declare const takeRandom: <T>(arr: T[]) => T;
|
|
6
|
+
export declare function makeRef<KeyT extends KeyTypeConstraints = string>(typeName: string, key: KeyT): Ref<KeyT>;
|
|
7
|
+
export declare function isObject(thing: any): boolean;
|
|
8
|
+
export declare function copyOwnPropsIfNotPresent(target: Record<string, any>, source: Record<string, any>): void;
|
|
9
|
+
export declare function copyOwnProps(target: Record<string, any>, ...sources: Array<Record<string, any>>): Record<string, any>;
|
|
10
|
+
export declare const isRootType: (type: GraphQLObjectType, schema: GraphQLSchema) => boolean;
|
package/typings/utils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GraphQLObjectType, GraphQLSchema } from '
|
|
1
|
+
import { GraphQLObjectType, GraphQLSchema } from 'graphql';
|
|
2
2
|
import { Ref, KeyTypeConstraints } from './types.js';
|
|
3
3
|
export declare function uuidv4(): string;
|
|
4
4
|
export declare const randomListLength: () => number;
|