@graphql-tools/mock 8.6.13 → 8.7.0-alpha-6c480b2d.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/MockList.js +75 -0
- package/cjs/MockStore.js +489 -0
- package/cjs/addMocksToSchema.js +204 -0
- package/cjs/index.js +9 -0
- package/cjs/mockServer.js +39 -0
- package/cjs/package.json +1 -0
- package/cjs/pagination.js +74 -0
- package/cjs/types.js +17 -0
- package/cjs/utils.js +54 -0
- package/esm/MockList.js +69 -0
- package/{index.mjs → esm/MockStore.js} +7 -432
- package/esm/addMocksToSchema.js +200 -0
- package/esm/index.js +6 -0
- package/esm/mockServer.js +35 -0
- package/esm/pagination.js +70 -0
- package/esm/types.js +11 -0
- package/esm/utils.js +43 -0
- package/package.json +33 -12
- package/{MockList.d.ts → typings/MockList.d.ts} +0 -0
- package/{MockStore.d.ts → typings/MockStore.d.ts} +1 -1
- package/{addMocksToSchema.d.ts → typings/addMocksToSchema.d.ts} +1 -1
- package/typings/index.d.ts +6 -0
- package/{mockServer.d.ts → typings/mockServer.d.ts} +1 -1
- package/{pagination.d.ts → typings/pagination.d.ts} +1 -1
- package/{types.d.ts → typings/types.d.ts} +0 -0
- package/{utils.d.ts → typings/utils.d.ts} +1 -1
- package/index.d.ts +0 -6
- package/index.js +0 -925
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { isRootType, makeRef } from './utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* Produces a resolver that'll mock a [Relay-style cursor pagination](https://relay.dev/graphql/connections.htm).
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* const schemaWithMocks = addMocksToSchema({
|
|
7
|
+
* schema,
|
|
8
|
+
* resolvers: (store) => ({
|
|
9
|
+
* User: {
|
|
10
|
+
* friends: relayStylePaginationMock(store),
|
|
11
|
+
* }
|
|
12
|
+
* }),
|
|
13
|
+
* })
|
|
14
|
+
* ```
|
|
15
|
+
* @param store the MockStore
|
|
16
|
+
*/
|
|
17
|
+
export const relayStylePaginationMock = (store, { cursorFn = node => `${node.$ref.key}`, applyOnNodes, allNodesFn, } = {}) => {
|
|
18
|
+
return (parent, args, context, info) => {
|
|
19
|
+
const source = isRootType(info.parentType, info.schema) ? makeRef(info.parentType.name, 'ROOT') : parent;
|
|
20
|
+
const allNodesFn_ = allNodesFn !== null && allNodesFn !== void 0 ? allNodesFn : defaultAllNodesFn(store);
|
|
21
|
+
let allNodes = allNodesFn_(source, args, context, info);
|
|
22
|
+
if (applyOnNodes) {
|
|
23
|
+
allNodes = applyOnNodes(allNodes, args);
|
|
24
|
+
}
|
|
25
|
+
const allEdges = allNodes.map(node => ({
|
|
26
|
+
node,
|
|
27
|
+
cursor: cursorFn(node),
|
|
28
|
+
}));
|
|
29
|
+
let start, end;
|
|
30
|
+
const { first, after, last, before } = args;
|
|
31
|
+
if (typeof first === 'number') {
|
|
32
|
+
// forward pagination
|
|
33
|
+
if (last || before) {
|
|
34
|
+
throw new Error("if `first` is provided, `last` or `before` can't be provided");
|
|
35
|
+
}
|
|
36
|
+
const afterIndex = after ? allEdges.findIndex(e => e.cursor === after) : -1;
|
|
37
|
+
start = afterIndex + 1;
|
|
38
|
+
end = afterIndex + 1 + first;
|
|
39
|
+
}
|
|
40
|
+
else if (typeof last === 'number') {
|
|
41
|
+
// backward pagination
|
|
42
|
+
if (first || after) {
|
|
43
|
+
throw new Error("if `last` is provided, `first` or `after` can't be provided");
|
|
44
|
+
}
|
|
45
|
+
const foundBeforeIndex = before ? allEdges.findIndex(e => e.cursor === before) : -1;
|
|
46
|
+
const beforeIndex = foundBeforeIndex !== -1 ? foundBeforeIndex : allNodes.length;
|
|
47
|
+
start = allEdges.length - (allEdges.length - beforeIndex) - last;
|
|
48
|
+
// negative index on Array.slice indicate offset from end of sequence => we don't want
|
|
49
|
+
if (start < 0)
|
|
50
|
+
start = 0;
|
|
51
|
+
end = beforeIndex;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
throw new Error('A `first` or a `last` arguments should be provided');
|
|
55
|
+
}
|
|
56
|
+
const edges = allEdges.slice(start, end);
|
|
57
|
+
const pageInfo = {
|
|
58
|
+
startCursor: edges.length > 0 ? edges[0].cursor : '',
|
|
59
|
+
endCursor: edges.length > 0 ? edges[edges.length - 1].cursor : '',
|
|
60
|
+
hasNextPage: end < allEdges.length - 1,
|
|
61
|
+
hasPreviousPage: start > 0,
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
edges,
|
|
65
|
+
pageInfo,
|
|
66
|
+
totalCount: allEdges.length,
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
const defaultAllNodesFn = (store) => (parent, _, __, info) => store.get(parent, [info.fieldName, 'edges']).map(e => store.get(e, 'node'));
|
package/esm/types.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function isRef(maybeRef) {
|
|
2
|
+
return !!(maybeRef && typeof maybeRef === 'object' && '$ref' in maybeRef);
|
|
3
|
+
}
|
|
4
|
+
export function assertIsRef(maybeRef, message) {
|
|
5
|
+
if (!isRef(maybeRef)) {
|
|
6
|
+
throw new Error(message || `Expected ${maybeRef} to be a valid Ref.`);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function isRecord(obj) {
|
|
10
|
+
return typeof obj === 'object' && obj !== null;
|
|
11
|
+
}
|
package/esm/utils.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { getRootTypeNames } from '@graphql-tools/utils';
|
|
2
|
+
export function uuidv4() {
|
|
3
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
|
4
|
+
const r = (Math.random() * 16) | 0;
|
|
5
|
+
// eslint-disable-next-line eqeqeq
|
|
6
|
+
const v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
7
|
+
return v.toString(16);
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
export const randomListLength = () => {
|
|
11
|
+
// Mocking has always returned list of length 2 by default
|
|
12
|
+
// return 1 + Math.round(Math.random() * 10)
|
|
13
|
+
return 2;
|
|
14
|
+
};
|
|
15
|
+
export const takeRandom = (arr) => arr[Math.floor(Math.random() * arr.length)];
|
|
16
|
+
export function makeRef(typeName, key) {
|
|
17
|
+
return { $ref: { key, typeName } };
|
|
18
|
+
}
|
|
19
|
+
export function isObject(thing) {
|
|
20
|
+
return thing === Object(thing) && !Array.isArray(thing);
|
|
21
|
+
}
|
|
22
|
+
export function copyOwnPropsIfNotPresent(target, source) {
|
|
23
|
+
for (const prop of Object.getOwnPropertyNames(source)) {
|
|
24
|
+
if (!Object.getOwnPropertyDescriptor(target, prop)) {
|
|
25
|
+
const propertyDescriptor = Object.getOwnPropertyDescriptor(source, prop);
|
|
26
|
+
Object.defineProperty(target, prop, propertyDescriptor == null ? {} : propertyDescriptor);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export function copyOwnProps(target, ...sources) {
|
|
31
|
+
for (const source of sources) {
|
|
32
|
+
let chain = source;
|
|
33
|
+
while (chain != null) {
|
|
34
|
+
copyOwnPropsIfNotPresent(target, chain);
|
|
35
|
+
chain = Object.getPrototypeOf(chain);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return target;
|
|
39
|
+
}
|
|
40
|
+
export const isRootType = (type, schema) => {
|
|
41
|
+
const rootTypeNames = getRootTypeNames(schema);
|
|
42
|
+
return rootTypeNames.has(type.name);
|
|
43
|
+
};
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-tools/mock",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.7.0-alpha-6c480b2d.0",
|
|
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": "8.
|
|
11
|
-
"@graphql-tools/utils": "8.
|
|
10
|
+
"@graphql-tools/schema": "8.5.0-alpha-6c480b2d.0",
|
|
11
|
+
"@graphql-tools/utils": "8.8.0-alpha-6c480b2d.0",
|
|
12
12
|
"fast-json-stable-stringify": "^2.1.0",
|
|
13
13
|
"tslib": "^2.4.0"
|
|
14
14
|
},
|
|
@@ -18,21 +18,42 @@
|
|
|
18
18
|
"directory": "packages/mock"
|
|
19
19
|
},
|
|
20
20
|
"license": "MIT",
|
|
21
|
-
"main": "index.js",
|
|
22
|
-
"module": "index.
|
|
23
|
-
"typings": "index.d.ts",
|
|
21
|
+
"main": "cjs/index.js",
|
|
22
|
+
"module": "esm/index.js",
|
|
23
|
+
"typings": "typings/index.d.ts",
|
|
24
24
|
"typescript": {
|
|
25
|
-
"definition": "index.d.ts"
|
|
25
|
+
"definition": "typings/index.d.ts"
|
|
26
26
|
},
|
|
27
|
+
"type": "module",
|
|
27
28
|
"exports": {
|
|
28
29
|
".": {
|
|
29
|
-
"require":
|
|
30
|
-
|
|
30
|
+
"require": {
|
|
31
|
+
"types": "./typings/index.d.ts",
|
|
32
|
+
"default": "./cjs/index.js"
|
|
33
|
+
},
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./typings/index.d.ts",
|
|
36
|
+
"default": "./esm/index.js"
|
|
37
|
+
},
|
|
38
|
+
"default": {
|
|
39
|
+
"types": "./typings/index.d.ts",
|
|
40
|
+
"default": "./esm/index.js"
|
|
41
|
+
}
|
|
31
42
|
},
|
|
32
43
|
"./*": {
|
|
33
|
-
"require":
|
|
34
|
-
|
|
44
|
+
"require": {
|
|
45
|
+
"types": "./typings/*.d.ts",
|
|
46
|
+
"default": "./cjs/*.js"
|
|
47
|
+
},
|
|
48
|
+
"import": {
|
|
49
|
+
"types": "./typings/*.d.ts",
|
|
50
|
+
"default": "./esm/*.js"
|
|
51
|
+
},
|
|
52
|
+
"default": {
|
|
53
|
+
"types": "./typings/*.d.ts",
|
|
54
|
+
"default": "./esm/*.js"
|
|
55
|
+
}
|
|
35
56
|
},
|
|
36
57
|
"./package.json": "./package.json"
|
|
37
58
|
}
|
|
38
|
-
}
|
|
59
|
+
}
|
|
File without changes
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GraphQLSchema } from 'graphql';
|
|
2
|
-
import { IMockStore, GetArgs, SetArgs, Ref, TypePolicy, IMocks, KeyTypeConstraints } from './types';
|
|
2
|
+
import { IMockStore, GetArgs, SetArgs, Ref, TypePolicy, IMocks, KeyTypeConstraints } from './types.js';
|
|
3
3
|
export declare const defaultMocks: {
|
|
4
4
|
Int: () => number;
|
|
5
5
|
Float: () => number;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { GraphQLSchema } from 'graphql';
|
|
2
2
|
import { IResolvers } from '@graphql-tools/utils';
|
|
3
|
-
import { IMockStore, IMocks, TypePolicy } from './types';
|
|
3
|
+
import { IMockStore, IMocks, TypePolicy } from './types.js';
|
|
4
4
|
declare type IMockOptions<TResolvers = IResolvers> = {
|
|
5
5
|
schema: GraphQLSchema;
|
|
6
6
|
store?: IMockStore;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TypeSource } from '@graphql-tools/utils';
|
|
2
|
-
import { IMockServer, IMocks } from './types';
|
|
2
|
+
import { IMockServer, IMocks } 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
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IFieldResolver } from '@graphql-tools/utils';
|
|
2
2
|
import { GraphQLResolveInfo } from 'graphql';
|
|
3
|
-
import { IMockStore, Ref } from './types';
|
|
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> = {
|
|
6
6
|
/**
|
|
File without changes
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GraphQLObjectType, GraphQLSchema } from 'graphql';
|
|
2
|
-
import { Ref, KeyTypeConstraints } from './types';
|
|
2
|
+
import { Ref, KeyTypeConstraints } from './types.js';
|
|
3
3
|
export declare function uuidv4(): string;
|
|
4
4
|
export declare const randomListLength: () => number;
|
|
5
5
|
export declare const takeRandom: <T>(arr: T[]) => T;
|