@graphql-tools/mock
Advanced tools
Comparing version 9.0.0 to 9.0.1-rc-20240122214949-0b7bed92ae33be8ed0fa1bea50b57416fb6b9d1f
@@ -5,7 +5,7 @@ "use strict"; | ||
const graphql_1 = require("graphql"); | ||
const schema_1 = require("@graphql-tools/schema"); | ||
const utils_1 = require("@graphql-tools/utils"); | ||
const schema_1 = require("@graphql-tools/schema"); | ||
const MockStore_js_1 = require("./MockStore.js"); | ||
const types_js_1 = require("./types.js"); | ||
const utils_js_1 = require("./utils.js"); | ||
const MockStore_js_1 = require("./MockStore.js"); | ||
// todo: add option to preserve resolver | ||
@@ -114,3 +114,3 @@ /** | ||
// any is used here because generateFieldValue is a private method at time of writing | ||
return store.generateFieldValue(info.parentType.name, info.fieldName); | ||
return store.generateFieldValue(info.parentType.name, info.fieldName, args); | ||
} | ||
@@ -117,0 +117,0 @@ return undefined; |
@@ -8,3 +8,4 @@ "use strict"; | ||
function isMockList(obj) { | ||
if (typeof obj?.len === 'number' || (Array.isArray(obj?.len) && typeof obj?.len[0] === 'number')) { | ||
if (typeof obj?.len === 'number' || | ||
(Array.isArray(obj?.len) && typeof obj?.len[0] === 'number')) { | ||
if (typeof obj.wrappedFunction === 'undefined' || typeof obj.wrappedFunction === 'function') { | ||
@@ -11,0 +12,0 @@ return true; |
@@ -5,7 +5,7 @@ "use strict"; | ||
const tslib_1 = require("tslib"); | ||
const fast_json_stable_stringify_1 = tslib_1.__importDefault(require("fast-json-stable-stringify")); | ||
const graphql_1 = require("graphql"); | ||
const fast_json_stable_stringify_1 = tslib_1.__importDefault(require("fast-json-stable-stringify")); | ||
const MockList_js_1 = require("./MockList.js"); | ||
const types_js_1 = require("./types.js"); | ||
const utils_js_1 = require("./utils.js"); | ||
const MockList_js_1 = require("./MockList.js"); | ||
exports.defaultMocks = { | ||
@@ -147,7 +147,13 @@ Int: () => Math.round(Math.random() * 200) - 100, | ||
else { | ||
value = this.generateFieldValue(typeName, fieldName, (otherFieldName, otherValue) => { | ||
value = this.generateFieldValue(typeName, fieldName, fieldArgs, (otherFieldName, otherValue) => { | ||
// if we get a key field in the mix we don't care | ||
if (this.isKeyField(typeName, otherFieldName)) | ||
return; | ||
this.set({ typeName, key, fieldName: otherFieldName, value: otherValue, noOverride: true }); | ||
this.set({ | ||
typeName, | ||
key, | ||
fieldName: otherFieldName, | ||
value: otherValue, | ||
noOverride: true, | ||
}); | ||
}); | ||
@@ -244,3 +250,5 @@ } | ||
return value.map((v, index) => { | ||
return this.normalizeValueToStore(nullableFieldType.ofType, v, typeof currentValue === 'object' && currentValue != null && currentValue[index] ? currentValue : undefined, onInsertType); | ||
return this.normalizeValueToStore(nullableFieldType.ofType, v, typeof currentValue === 'object' && currentValue != null && currentValue[index] | ||
? currentValue | ||
: undefined, onInsertType); | ||
}); | ||
@@ -291,4 +299,4 @@ } | ||
} | ||
generateFieldValue(typeName, fieldName, onOtherFieldsGenerated) { | ||
const mockedValue = this.generateFieldValueFromMocks(typeName, fieldName, onOtherFieldsGenerated); | ||
generateFieldValue(typeName, fieldName, fieldArgs, onOtherFieldsGenerated) { | ||
const mockedValue = this.generateFieldValueFromMocks(typeName, fieldName, fieldArgs, onOtherFieldsGenerated); | ||
if (mockedValue !== undefined) | ||
@@ -299,3 +307,3 @@ return mockedValue; | ||
} | ||
generateFieldValueFromMocks(typeName, fieldName, onOtherFieldsGenerated) { | ||
generateFieldValueFromMocks(typeName, fieldName, fieldArgs, onOtherFieldsGenerated) { | ||
let value; | ||
@@ -314,10 +322,13 @@ const mock = this.mocks ? this.mocks[typeName] : undefined; | ||
continue; | ||
onOtherFieldsGenerated && onOtherFieldsGenerated(otherFieldName, values[otherFieldName]); | ||
onOtherFieldsGenerated && | ||
onOtherFieldsGenerated(otherFieldName, values[otherFieldName]); | ||
} | ||
value = values[fieldName]; | ||
if (typeof value === 'function') | ||
value = value(); | ||
value = value(fieldArgs); | ||
} | ||
else if (typeof mock === 'object' && mock != null && typeof mock[fieldName] === 'function') { | ||
value = mock[fieldName](); | ||
else if (typeof mock === 'object' && | ||
mock != null && | ||
typeof mock[fieldName] === 'function') { | ||
value = mock[fieldName](fieldArgs); | ||
} | ||
@@ -334,3 +345,3 @@ } | ||
break; | ||
value = this.generateFieldValueFromMocks(interface_.name, fieldName, onOtherFieldsGenerated); | ||
value = this.generateFieldValueFromMocks(interface_.name, fieldName, fieldArgs, onOtherFieldsGenerated); | ||
} | ||
@@ -344,3 +355,3 @@ } | ||
return (0, utils_js_1.uuidv4)(); | ||
return this.generateFieldValue(typeName, keyFieldName, onOtherFieldsGenerated); | ||
return this.generateFieldValue(typeName, keyFieldName, undefined, onOtherFieldsGenerated); | ||
} | ||
@@ -389,3 +400,5 @@ generateValueFromType(fieldType) { | ||
} | ||
else if (typeof mock === 'object' && mock != null && typeof mock['__typename'] === 'function') { | ||
else if (typeof mock === 'object' && | ||
mock != null && | ||
typeof mock['__typename'] === 'function') { | ||
const mockRes = mock['__typename'](); | ||
@@ -467,3 +480,5 @@ if (typeof mockRes !== 'string') | ||
} | ||
throw new Error(process.env['NODE_ENV'] === 'production' ? 'Invariant failed:' : `Invariant failed: ${message || ''}`); | ||
throw new Error(process.env['NODE_ENV'] === 'production' | ||
? 'Invariant failed:' | ||
: `Invariant failed: ${message || ''}`); | ||
} | ||
@@ -470,0 +485,0 @@ /** |
@@ -22,3 +22,5 @@ "use strict"; | ||
return (parent, args, context, info) => { | ||
const source = (0, utils_js_1.isRootType)(info.parentType, info.schema) ? (0, utils_js_1.makeRef)(info.parentType.name, 'ROOT') : parent; | ||
const source = (0, utils_js_1.isRootType)(info.parentType, info.schema) | ||
? (0, utils_js_1.makeRef)(info.parentType.name, 'ROOT') | ||
: parent; | ||
const allNodesFn_ = allNodesFn ?? defaultAllNodesFn(store); | ||
@@ -25,0 +27,0 @@ let allNodes = allNodesFn_(source, args, context, info); |
@@ -1,7 +0,7 @@ | ||
import { defaultFieldResolver, isUnionType, GraphQLUnionType, GraphQLInterfaceType, isSchema, } from 'graphql'; | ||
import { mapSchema, MapperKind } from '@graphql-tools/utils'; | ||
import { defaultFieldResolver, GraphQLInterfaceType, GraphQLUnionType, isSchema, isUnionType, } from 'graphql'; | ||
import { addResolversToSchema } from '@graphql-tools/schema'; | ||
import { MapperKind, mapSchema } from '@graphql-tools/utils'; | ||
import { createMockStore } from './MockStore.js'; | ||
import { isRef } from './types.js'; | ||
import { copyOwnProps, isObject, isRootType } from './utils.js'; | ||
import { createMockStore } from './MockStore.js'; | ||
// todo: add option to preserve resolver | ||
@@ -110,3 +110,3 @@ /** | ||
// any is used here because generateFieldValue is a private method at time of writing | ||
return store.generateFieldValue(info.parentType.name, info.fieldName); | ||
return store.generateFieldValue(info.parentType.name, info.fieldName, args); | ||
} | ||
@@ -113,0 +113,0 @@ return undefined; |
@@ -5,3 +5,4 @@ /** | ||
export function isMockList(obj) { | ||
if (typeof obj?.len === 'number' || (Array.isArray(obj?.len) && typeof obj?.len[0] === 'number')) { | ||
if (typeof obj?.len === 'number' || | ||
(Array.isArray(obj?.len) && typeof obj?.len[0] === 'number')) { | ||
if (typeof obj.wrappedFunction === 'undefined' || typeof obj.wrappedFunction === 'function') { | ||
@@ -8,0 +9,0 @@ return true; |
@@ -1,2 +0,2 @@ | ||
import { isSchema, graphql } from 'graphql'; | ||
import { graphql, isSchema } from 'graphql'; | ||
import { makeExecutableSchema } from '@graphql-tools/schema'; | ||
@@ -3,0 +3,0 @@ import { addMocksToSchema } from './addMocksToSchema.js'; |
@@ -1,6 +0,6 @@ | ||
import { GraphQLString, isObjectType, isScalarType, getNullableType, isListType, isEnumType, isAbstractType, isCompositeType, isNullableType, isInterfaceType, } from 'graphql'; | ||
import stringify from 'fast-json-stable-stringify'; | ||
import { isRef, assertIsRef, isRecord, } from './types.js'; | ||
import { uuidv4, randomListLength, takeRandom, makeRef } from './utils.js'; | ||
import { getNullableType, GraphQLString, isAbstractType, isCompositeType, isEnumType, isInterfaceType, isListType, isNullableType, isObjectType, isScalarType, } from 'graphql'; | ||
import { deepResolveMockList, isMockList } from './MockList.js'; | ||
import { assertIsRef, isRecord, isRef, } from './types.js'; | ||
import { makeRef, randomListLength, takeRandom, uuidv4 } from './utils.js'; | ||
export const defaultMocks = { | ||
@@ -142,7 +142,13 @@ Int: () => Math.round(Math.random() * 200) - 100, | ||
else { | ||
value = this.generateFieldValue(typeName, fieldName, (otherFieldName, otherValue) => { | ||
value = this.generateFieldValue(typeName, fieldName, fieldArgs, (otherFieldName, otherValue) => { | ||
// if we get a key field in the mix we don't care | ||
if (this.isKeyField(typeName, otherFieldName)) | ||
return; | ||
this.set({ typeName, key, fieldName: otherFieldName, value: otherValue, noOverride: true }); | ||
this.set({ | ||
typeName, | ||
key, | ||
fieldName: otherFieldName, | ||
value: otherValue, | ||
noOverride: true, | ||
}); | ||
}); | ||
@@ -239,3 +245,5 @@ } | ||
return value.map((v, index) => { | ||
return this.normalizeValueToStore(nullableFieldType.ofType, v, typeof currentValue === 'object' && currentValue != null && currentValue[index] ? currentValue : undefined, onInsertType); | ||
return this.normalizeValueToStore(nullableFieldType.ofType, v, typeof currentValue === 'object' && currentValue != null && currentValue[index] | ||
? currentValue | ||
: undefined, onInsertType); | ||
}); | ||
@@ -286,4 +294,4 @@ } | ||
} | ||
generateFieldValue(typeName, fieldName, onOtherFieldsGenerated) { | ||
const mockedValue = this.generateFieldValueFromMocks(typeName, fieldName, onOtherFieldsGenerated); | ||
generateFieldValue(typeName, fieldName, fieldArgs, onOtherFieldsGenerated) { | ||
const mockedValue = this.generateFieldValueFromMocks(typeName, fieldName, fieldArgs, onOtherFieldsGenerated); | ||
if (mockedValue !== undefined) | ||
@@ -294,3 +302,3 @@ return mockedValue; | ||
} | ||
generateFieldValueFromMocks(typeName, fieldName, onOtherFieldsGenerated) { | ||
generateFieldValueFromMocks(typeName, fieldName, fieldArgs, onOtherFieldsGenerated) { | ||
let value; | ||
@@ -309,10 +317,13 @@ const mock = this.mocks ? this.mocks[typeName] : undefined; | ||
continue; | ||
onOtherFieldsGenerated && onOtherFieldsGenerated(otherFieldName, values[otherFieldName]); | ||
onOtherFieldsGenerated && | ||
onOtherFieldsGenerated(otherFieldName, values[otherFieldName]); | ||
} | ||
value = values[fieldName]; | ||
if (typeof value === 'function') | ||
value = value(); | ||
value = value(fieldArgs); | ||
} | ||
else if (typeof mock === 'object' && mock != null && typeof mock[fieldName] === 'function') { | ||
value = mock[fieldName](); | ||
else if (typeof mock === 'object' && | ||
mock != null && | ||
typeof mock[fieldName] === 'function') { | ||
value = mock[fieldName](fieldArgs); | ||
} | ||
@@ -329,3 +340,3 @@ } | ||
break; | ||
value = this.generateFieldValueFromMocks(interface_.name, fieldName, onOtherFieldsGenerated); | ||
value = this.generateFieldValueFromMocks(interface_.name, fieldName, fieldArgs, onOtherFieldsGenerated); | ||
} | ||
@@ -339,3 +350,3 @@ } | ||
return uuidv4(); | ||
return this.generateFieldValue(typeName, keyFieldName, onOtherFieldsGenerated); | ||
return this.generateFieldValue(typeName, keyFieldName, undefined, onOtherFieldsGenerated); | ||
} | ||
@@ -384,3 +395,5 @@ generateValueFromType(fieldType) { | ||
} | ||
else if (typeof mock === 'object' && mock != null && typeof mock['__typename'] === 'function') { | ||
else if (typeof mock === 'object' && | ||
mock != null && | ||
typeof mock['__typename'] === 'function') { | ||
const mockRes = mock['__typename'](); | ||
@@ -461,3 +474,5 @@ if (typeof mockRes !== 'string') | ||
} | ||
throw new Error(process.env['NODE_ENV'] === 'production' ? 'Invariant failed:' : `Invariant failed: ${message || ''}`); | ||
throw new Error(process.env['NODE_ENV'] === 'production' | ||
? 'Invariant failed:' | ||
: `Invariant failed: ${message || ''}`); | ||
} | ||
@@ -464,0 +479,0 @@ /** |
@@ -19,3 +19,5 @@ import { isRootType, makeRef } from './utils.js'; | ||
return (parent, args, context, info) => { | ||
const source = isRootType(info.parentType, info.schema) ? makeRef(info.parentType.name, 'ROOT') : parent; | ||
const source = isRootType(info.parentType, info.schema) | ||
? makeRef(info.parentType.name, 'ROOT') | ||
: parent; | ||
const allNodesFn_ = allNodesFn ?? defaultAllNodesFn(store); | ||
@@ -22,0 +24,0 @@ let allNodes = allNodesFn_(source, args, context, info); |
{ | ||
"name": "@graphql-tools/mock", | ||
"version": "9.0.0", | ||
"version": "9.0.1-rc-20240122214949-0b7bed92ae33be8ed0fa1bea50b57416fb6b9d1f", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -11,3 +11,3 @@ "sideEffects": false, | ||
"@graphql-tools/schema": "^10.0.0", | ||
"@graphql-tools/utils": "^10.0.0", | ||
"@graphql-tools/utils": "10.0.13-rc-20240122214949-0b7bed92ae33be8ed0fa1bea50b57416fb6b9d1f", | ||
"fast-json-stable-stringify": "^2.1.0", | ||
@@ -14,0 +14,0 @@ "tslib": "^2.4.0" |
Check API Reference for more information about this package; | ||
https://www.graphql-tools.com/docs/api/modules/mock_src | ||
You can also learn more about Mocking in this chapter; | ||
https://www.graphql-tools.com/docs/mocking | ||
You can also learn more about Mocking in this chapter; https://www.graphql-tools.com/docs/mocking |
import { GraphQLSchema } from 'graphql'; | ||
import { IResolvers } from '@graphql-tools/utils'; | ||
import { IMockStore, IMocks, TypePolicy } from './types.js'; | ||
import { IMocks, IMockStore, TypePolicy } from './types.js'; | ||
type IMockOptions<TResolvers = IResolvers> = { | ||
@@ -5,0 +5,0 @@ schema: GraphQLSchema; |
import { TypeSource } from '@graphql-tools/utils'; | ||
import { IMockServer, IMocks } from './types.js'; | ||
import { IMocks, IMockServer } from './types.js'; | ||
/** | ||
@@ -4,0 +4,0 @@ * A convenience wrapper on top of addMocksToSchema. It adds your mock resolvers |
import { GraphQLSchema } from 'graphql'; | ||
import { IMockStore, GetArgs, SetArgs, Ref, TypePolicy, IMocks, KeyTypeConstraints } from './types.js'; | ||
import { GetArgs, IMocks, IMockStore, KeyTypeConstraints, Ref, SetArgs, TypePolicy } from './types.js'; | ||
export declare const defaultMocks: { | ||
@@ -4,0 +4,0 @@ Int: () => number; |
@@ -0,3 +1,3 @@ | ||
import { GraphQLResolveInfo } from 'graphql'; | ||
import { IFieldResolver } from '@graphql-tools/utils'; | ||
import { GraphQLResolveInfo } from 'graphql'; | ||
import { IMockStore, Ref } from './types.js'; | ||
@@ -4,0 +4,0 @@ export type AllNodesFn<TContext, TArgs extends RelayPaginationParams> = (parent: Ref, args: TArgs, context: TContext, info: GraphQLResolveInfo) => Ref[]; |
@@ -0,3 +1,3 @@ | ||
import { GraphQLSchema } from 'graphql'; | ||
import { ExecutionResult, IResolvers } from '@graphql-tools/utils'; | ||
import { GraphQLSchema } from 'graphql'; | ||
export type IMockFn = () => unknown; | ||
@@ -4,0 +4,0 @@ export type IScalarMock = unknown | IMockFn; |
import { GraphQLObjectType, GraphQLSchema } from 'graphql'; | ||
import { Ref, KeyTypeConstraints } from './types.js'; | ||
import { KeyTypeConstraints, Ref } from './types.js'; | ||
export declare function uuidv4(): string; | ||
@@ -4,0 +4,0 @@ export declare const randomListLength: () => number; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
111677
2439
2
5