@graphql-tools/mock
Advanced tools
Comparing version 5.0.1-alpha-2ac6e4a.0 to 5.0.1-alpha-3268f17.0
156
index.cjs.js
@@ -21,3 +21,3 @@ 'use strict'; | ||
} | ||
addMocksToSchema({ schema: mySchema, mocks, preserveResolvers }); | ||
mySchema = addMocksToSchema({ schema: mySchema, mocks, preserveResolvers }); | ||
return { query: (query, vars) => graphql.graphql(mySchema, query, {}, {}, vars) }; | ||
@@ -145,56 +145,87 @@ } | ||
}; | ||
utils.forEachField(schema, (field, typeName, fieldName) => { | ||
assignResolveType(field.type, preserveResolvers); | ||
let mockResolver = mockType(field.type, typeName, fieldName); | ||
// we have to handle the root mutation and root query types differently, | ||
// because no resolver is called at the root. | ||
const queryType = schema.getQueryType(); | ||
const isOnQueryType = queryType != null && queryType.name === typeName; | ||
const mutationType = schema.getMutationType(); | ||
const isOnMutationType = mutationType != null && mutationType.name === typeName; | ||
if (isOnQueryType || isOnMutationType) { | ||
if (mockFunctionMap.has(typeName)) { | ||
const rootMock = mockFunctionMap.get(typeName); | ||
// XXX: BUG in here, need to provide proper signature for rootMock. | ||
if (typeof rootMock(undefined, {}, {}, {})[fieldName] === 'function') { | ||
mockResolver = (root, args, context, info) => { | ||
const updatedRoot = root !== null && root !== void 0 ? root : {}; // TODO: should we clone instead? | ||
updatedRoot[fieldName] = rootMock(root, args, context, info)[fieldName]; | ||
// XXX this is a bit of a hack to still use mockType, which | ||
// lets you mock lists etc. as well | ||
// otherwise we could just set field.resolve to rootMock()[fieldName] | ||
// it's like pretending there was a resolver that ran before | ||
// the root resolver. | ||
return mockType(field.type, typeName, fieldName)(updatedRoot, args, context, info); | ||
}; | ||
return utils.mapSchema(schema, { | ||
[utils.MapperKind.ABSTRACT_TYPE]: type => { | ||
const oldResolveType = type.resolveType; | ||
if (preserveResolvers && oldResolveType != null && oldResolveType.length) { | ||
return; | ||
} | ||
// the default `resolveType` always returns null. We add a fallback | ||
// resolution that works with how unions and interface are mocked | ||
const resolveType = (data, _context, info) => info.schema.getType(data.__typename); | ||
if (graphql.isInterfaceType(type)) { | ||
return new graphql.GraphQLInterfaceType({ | ||
...type.toConfig(), | ||
resolveType, | ||
}); | ||
} | ||
else { | ||
return new graphql.GraphQLUnionType({ | ||
...type.toConfig(), | ||
resolveType, | ||
}); | ||
} | ||
}, | ||
[utils.MapperKind.OBJECT_FIELD]: (fieldConfig, fieldName, typeName) => { | ||
const fieldType = fieldConfig.type; | ||
const fieldResolver = fieldConfig.resolve; | ||
const newFieldConfig = { | ||
...fieldConfig, | ||
}; | ||
let mockResolver = mockType(fieldType, typeName, fieldName); | ||
// we have to handle the root mutation and root query types differently, | ||
// because no resolver is called at the root. | ||
const queryType = schema.getQueryType(); | ||
const isOnQueryType = queryType != null && queryType.name === typeName; | ||
const mutationType = schema.getMutationType(); | ||
const isOnMutationType = mutationType != null && mutationType.name === typeName; | ||
if (isOnQueryType || isOnMutationType) { | ||
if (mockFunctionMap.has(typeName)) { | ||
const rootMock = mockFunctionMap.get(typeName); | ||
// XXX: BUG in here, need to provide proper signature for rootMock. | ||
if (typeof rootMock(undefined, {}, {}, {})[fieldName] === 'function') { | ||
mockResolver = (root, args, context, info) => { | ||
const updatedRoot = root !== null && root !== void 0 ? root : {}; // TODO: should we clone instead? | ||
updatedRoot[fieldName] = rootMock(root, args, context, info)[fieldName]; | ||
// XXX this is a bit of a hack to still use mockType, which | ||
// lets you mock lists etc. as well | ||
// otherwise we could just set field.resolve to rootMock()[fieldName] | ||
// it's like pretending there was a resolver that ran before | ||
// the root resolver. | ||
return mockType(fieldConfig.type, typeName, fieldName)(updatedRoot, args, context, info); | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
if (!preserveResolvers || !field.resolve) { | ||
field.resolve = mockResolver; | ||
} | ||
else { | ||
const oldResolver = field.resolve; | ||
field.resolve = (rootObject, args, context, info) => Promise.all([mockResolver(rootObject, args, context, info), oldResolver(rootObject, args, context, info)]).then(values => { | ||
const [mockedValue, resolvedValue] = values; | ||
// In case we couldn't mock | ||
if (mockedValue instanceof Error) { | ||
// only if value was not resolved, populate the error. | ||
if (undefined === resolvedValue) { | ||
throw mockedValue; | ||
if (!preserveResolvers || !fieldResolver) { | ||
newFieldConfig.resolve = mockResolver; | ||
} | ||
else { | ||
const oldResolver = fieldResolver; | ||
newFieldConfig.resolve = (rootObject, args, context, info) => Promise.all([ | ||
mockResolver(rootObject, args, context, info), | ||
oldResolver(rootObject, args, context, info), | ||
]).then(values => { | ||
const [mockedValue, resolvedValue] = values; | ||
// In case we couldn't mock | ||
if (mockedValue instanceof Error) { | ||
// only if value was not resolved, populate the error. | ||
if (undefined === resolvedValue) { | ||
throw mockedValue; | ||
} | ||
return resolvedValue; | ||
} | ||
return resolvedValue; | ||
} | ||
if (resolvedValue instanceof Date && mockedValue instanceof Date) { | ||
if (resolvedValue instanceof Date && mockedValue instanceof Date) { | ||
return undefined !== resolvedValue ? resolvedValue : mockedValue; | ||
} | ||
if (isObject(mockedValue) && isObject(resolvedValue)) { | ||
// Object.assign() won't do here, as we need to all properties, including | ||
// the non-enumerable ones and defined using Object.defineProperty | ||
const emptyObject = Object.create(Object.getPrototypeOf(resolvedValue)); | ||
return copyOwnProps(emptyObject, resolvedValue, mockedValue); | ||
} | ||
return undefined !== resolvedValue ? resolvedValue : mockedValue; | ||
} | ||
if (isObject(mockedValue) && isObject(resolvedValue)) { | ||
// Object.assign() won't do here, as we need to all properties, including | ||
// the non-enumerable ones and defined using Object.defineProperty | ||
const emptyObject = Object.create(Object.getPrototypeOf(resolvedValue)); | ||
return copyOwnProps(emptyObject, resolvedValue, mockedValue); | ||
} | ||
return undefined !== resolvedValue ? resolvedValue : mockedValue; | ||
}); | ||
} | ||
}); | ||
} | ||
return newFieldConfig; | ||
}, | ||
}); | ||
@@ -244,20 +275,2 @@ } | ||
} | ||
function getResolveType(namedFieldType) { | ||
if (graphql.isAbstractType(namedFieldType)) { | ||
return namedFieldType.resolveType; | ||
} | ||
} | ||
function assignResolveType(type, preserveResolvers) { | ||
const fieldType = graphql.getNullableType(type); | ||
const namedFieldType = graphql.getNamedType(fieldType); | ||
const oldResolveType = getResolveType(namedFieldType); | ||
if (preserveResolvers && oldResolveType != null && oldResolveType.length) { | ||
return; | ||
} | ||
if (graphql.isInterfaceType(namedFieldType) || graphql.isUnionType(namedFieldType)) { | ||
// the default `resolveType` always returns null. We add a fallback | ||
// resolution that works with how unions and interface are mocked | ||
namedFieldType.resolveType = (data, _context, info) => info.schema.getType(data.__typename); | ||
} | ||
} | ||
function isMockList(obj) { | ||
@@ -311,9 +324,4 @@ if (typeof (obj === null || obj === void 0 ? void 0 : obj.len) === 'number' || (Array.isArray(obj === null || obj === void 0 ? void 0 : obj.len) && typeof (obj === null || obj === void 0 ? void 0 : obj.len[0]) === 'number')) { | ||
} | ||
// retain addMockFunctionsToSchema for backwards compatibility | ||
function addMockFunctionsToSchema({ schema, mocks = {}, preserveResolvers = false }) { | ||
addMocksToSchema({ schema, mocks, preserveResolvers }); | ||
} | ||
exports.MockList = MockList; | ||
exports.addMockFunctionsToSchema = addMockFunctionsToSchema; | ||
exports.addMocksToSchema = addMocksToSchema; | ||
@@ -320,0 +328,0 @@ exports.isMockList = isMockList; |
161
index.esm.js
@@ -1,4 +0,4 @@ | ||
import { isSchema, graphql, getNullableType, getNamedType, isListType, isAbstractType, isObjectType, isEnumType, isInterfaceType, isUnionType } from 'graphql'; | ||
import { isSchema, isInterfaceType, GraphQLInterfaceType, GraphQLUnionType, graphql, getNullableType, getNamedType, isListType, isAbstractType, isObjectType, isEnumType } from 'graphql'; | ||
import { buildSchemaFromTypeDefinitions } from '@graphql-tools/schema'; | ||
import { forEachField } from '@graphql-tools/utils'; | ||
import { mapSchema, MapperKind } from '@graphql-tools/utils'; | ||
@@ -17,3 +17,3 @@ /** | ||
} | ||
addMocksToSchema({ schema: mySchema, mocks, preserveResolvers }); | ||
mySchema = addMocksToSchema({ schema: mySchema, mocks, preserveResolvers }); | ||
return { query: (query, vars) => graphql(mySchema, query, {}, {}, vars) }; | ||
@@ -141,56 +141,87 @@ } | ||
}; | ||
forEachField(schema, (field, typeName, fieldName) => { | ||
assignResolveType(field.type, preserveResolvers); | ||
let mockResolver = mockType(field.type, typeName, fieldName); | ||
// we have to handle the root mutation and root query types differently, | ||
// because no resolver is called at the root. | ||
const queryType = schema.getQueryType(); | ||
const isOnQueryType = queryType != null && queryType.name === typeName; | ||
const mutationType = schema.getMutationType(); | ||
const isOnMutationType = mutationType != null && mutationType.name === typeName; | ||
if (isOnQueryType || isOnMutationType) { | ||
if (mockFunctionMap.has(typeName)) { | ||
const rootMock = mockFunctionMap.get(typeName); | ||
// XXX: BUG in here, need to provide proper signature for rootMock. | ||
if (typeof rootMock(undefined, {}, {}, {})[fieldName] === 'function') { | ||
mockResolver = (root, args, context, info) => { | ||
const updatedRoot = root !== null && root !== void 0 ? root : {}; // TODO: should we clone instead? | ||
updatedRoot[fieldName] = rootMock(root, args, context, info)[fieldName]; | ||
// XXX this is a bit of a hack to still use mockType, which | ||
// lets you mock lists etc. as well | ||
// otherwise we could just set field.resolve to rootMock()[fieldName] | ||
// it's like pretending there was a resolver that ran before | ||
// the root resolver. | ||
return mockType(field.type, typeName, fieldName)(updatedRoot, args, context, info); | ||
}; | ||
return mapSchema(schema, { | ||
[MapperKind.ABSTRACT_TYPE]: type => { | ||
const oldResolveType = type.resolveType; | ||
if (preserveResolvers && oldResolveType != null && oldResolveType.length) { | ||
return; | ||
} | ||
// the default `resolveType` always returns null. We add a fallback | ||
// resolution that works with how unions and interface are mocked | ||
const resolveType = (data, _context, info) => info.schema.getType(data.__typename); | ||
if (isInterfaceType(type)) { | ||
return new GraphQLInterfaceType({ | ||
...type.toConfig(), | ||
resolveType, | ||
}); | ||
} | ||
else { | ||
return new GraphQLUnionType({ | ||
...type.toConfig(), | ||
resolveType, | ||
}); | ||
} | ||
}, | ||
[MapperKind.OBJECT_FIELD]: (fieldConfig, fieldName, typeName) => { | ||
const fieldType = fieldConfig.type; | ||
const fieldResolver = fieldConfig.resolve; | ||
const newFieldConfig = { | ||
...fieldConfig, | ||
}; | ||
let mockResolver = mockType(fieldType, typeName, fieldName); | ||
// we have to handle the root mutation and root query types differently, | ||
// because no resolver is called at the root. | ||
const queryType = schema.getQueryType(); | ||
const isOnQueryType = queryType != null && queryType.name === typeName; | ||
const mutationType = schema.getMutationType(); | ||
const isOnMutationType = mutationType != null && mutationType.name === typeName; | ||
if (isOnQueryType || isOnMutationType) { | ||
if (mockFunctionMap.has(typeName)) { | ||
const rootMock = mockFunctionMap.get(typeName); | ||
// XXX: BUG in here, need to provide proper signature for rootMock. | ||
if (typeof rootMock(undefined, {}, {}, {})[fieldName] === 'function') { | ||
mockResolver = (root, args, context, info) => { | ||
const updatedRoot = root !== null && root !== void 0 ? root : {}; // TODO: should we clone instead? | ||
updatedRoot[fieldName] = rootMock(root, args, context, info)[fieldName]; | ||
// XXX this is a bit of a hack to still use mockType, which | ||
// lets you mock lists etc. as well | ||
// otherwise we could just set field.resolve to rootMock()[fieldName] | ||
// it's like pretending there was a resolver that ran before | ||
// the root resolver. | ||
return mockType(fieldConfig.type, typeName, fieldName)(updatedRoot, args, context, info); | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
if (!preserveResolvers || !field.resolve) { | ||
field.resolve = mockResolver; | ||
} | ||
else { | ||
const oldResolver = field.resolve; | ||
field.resolve = (rootObject, args, context, info) => Promise.all([mockResolver(rootObject, args, context, info), oldResolver(rootObject, args, context, info)]).then(values => { | ||
const [mockedValue, resolvedValue] = values; | ||
// In case we couldn't mock | ||
if (mockedValue instanceof Error) { | ||
// only if value was not resolved, populate the error. | ||
if (undefined === resolvedValue) { | ||
throw mockedValue; | ||
if (!preserveResolvers || !fieldResolver) { | ||
newFieldConfig.resolve = mockResolver; | ||
} | ||
else { | ||
const oldResolver = fieldResolver; | ||
newFieldConfig.resolve = (rootObject, args, context, info) => Promise.all([ | ||
mockResolver(rootObject, args, context, info), | ||
oldResolver(rootObject, args, context, info), | ||
]).then(values => { | ||
const [mockedValue, resolvedValue] = values; | ||
// In case we couldn't mock | ||
if (mockedValue instanceof Error) { | ||
// only if value was not resolved, populate the error. | ||
if (undefined === resolvedValue) { | ||
throw mockedValue; | ||
} | ||
return resolvedValue; | ||
} | ||
return resolvedValue; | ||
} | ||
if (resolvedValue instanceof Date && mockedValue instanceof Date) { | ||
if (resolvedValue instanceof Date && mockedValue instanceof Date) { | ||
return undefined !== resolvedValue ? resolvedValue : mockedValue; | ||
} | ||
if (isObject(mockedValue) && isObject(resolvedValue)) { | ||
// Object.assign() won't do here, as we need to all properties, including | ||
// the non-enumerable ones and defined using Object.defineProperty | ||
const emptyObject = Object.create(Object.getPrototypeOf(resolvedValue)); | ||
return copyOwnProps(emptyObject, resolvedValue, mockedValue); | ||
} | ||
return undefined !== resolvedValue ? resolvedValue : mockedValue; | ||
} | ||
if (isObject(mockedValue) && isObject(resolvedValue)) { | ||
// Object.assign() won't do here, as we need to all properties, including | ||
// the non-enumerable ones and defined using Object.defineProperty | ||
const emptyObject = Object.create(Object.getPrototypeOf(resolvedValue)); | ||
return copyOwnProps(emptyObject, resolvedValue, mockedValue); | ||
} | ||
return undefined !== resolvedValue ? resolvedValue : mockedValue; | ||
}); | ||
} | ||
}); | ||
} | ||
return newFieldConfig; | ||
}, | ||
}); | ||
@@ -240,20 +271,2 @@ } | ||
} | ||
function getResolveType(namedFieldType) { | ||
if (isAbstractType(namedFieldType)) { | ||
return namedFieldType.resolveType; | ||
} | ||
} | ||
function assignResolveType(type, preserveResolvers) { | ||
const fieldType = getNullableType(type); | ||
const namedFieldType = getNamedType(fieldType); | ||
const oldResolveType = getResolveType(namedFieldType); | ||
if (preserveResolvers && oldResolveType != null && oldResolveType.length) { | ||
return; | ||
} | ||
if (isInterfaceType(namedFieldType) || isUnionType(namedFieldType)) { | ||
// the default `resolveType` always returns null. We add a fallback | ||
// resolution that works with how unions and interface are mocked | ||
namedFieldType.resolveType = (data, _context, info) => info.schema.getType(data.__typename); | ||
} | ||
} | ||
function isMockList(obj) { | ||
@@ -307,8 +320,4 @@ if (typeof (obj === null || obj === void 0 ? void 0 : obj.len) === 'number' || (Array.isArray(obj === null || obj === void 0 ? void 0 : obj.len) && typeof (obj === null || obj === void 0 ? void 0 : obj.len[0]) === 'number')) { | ||
} | ||
// retain addMockFunctionsToSchema for backwards compatibility | ||
function addMockFunctionsToSchema({ schema, mocks = {}, preserveResolvers = false }) { | ||
addMocksToSchema({ schema, mocks, preserveResolvers }); | ||
} | ||
export { MockList, addMockFunctionsToSchema, addMocksToSchema, isMockList, mockServer }; | ||
export { MockList, addMocksToSchema, isMockList, mockServer }; | ||
//# sourceMappingURL=index.esm.js.map |
@@ -8,3 +8,3 @@ import { GraphQLSchema, GraphQLList, GraphQLResolveInfo, GraphQLFieldResolver } from 'graphql'; | ||
export declare function mockServer(schema: GraphQLSchema | ITypeDefinitions, mocks: IMocks, preserveResolvers?: boolean): IMockServer; | ||
export declare function addMocksToSchema({ schema, mocks, preserveResolvers }: IMockOptions): void; | ||
export declare function addMocksToSchema({ schema, mocks, preserveResolvers }: IMockOptions): GraphQLSchema; | ||
export declare function isMockList(obj: any): obj is MockList; | ||
@@ -18,2 +18,1 @@ export declare class MockList { | ||
} | ||
export declare function addMockFunctionsToSchema({ schema, mocks, preserveResolvers }: IMockOptions): void; |
{ | ||
"name": "@graphql-tools/mock", | ||
"version": "5.0.1-alpha-2ac6e4a.0", | ||
"version": "5.0.1-alpha-3268f17.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -9,4 +9,4 @@ "peerDependencies": { | ||
"dependencies": { | ||
"@graphql-tools/schema": "5.0.1-alpha-2ac6e4a.0", | ||
"@graphql-tools/utils": "5.0.1-alpha-2ac6e4a.0", | ||
"@graphql-tools/schema": "5.0.1-alpha-3268f17.0", | ||
"@graphql-tools/utils": "5.0.1-alpha-3268f17.0", | ||
"tslib": "1.11.1" | ||
@@ -13,0 +13,0 @@ }, |
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
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
87211
671
+ Added@graphql-tools/schema@5.0.1-alpha-3268f17.0(transitive)
+ Added@graphql-tools/utils@5.0.1-alpha-3268f17.0(transitive)
- Removed@graphql-tools/schema@5.0.1-alpha-2ac6e4a.0(transitive)
- Removed@graphql-tools/utils@5.0.1-alpha-2ac6e4a.0(transitive)