graphql-mocks
Advanced tools
Comparing version 0.4.1 to 0.4.2
@@ -8,34 +8,32 @@ "use strict"; | ||
var _a; | ||
const useFindInCommon = '__testUseFindInCommon' in context ? context.__testUseFindInCommon : true; | ||
const useFindInCommon = '__useFindInCommon' in context ? context.__useFindInCommon : true; | ||
const { graphqlSchema } = extract_dependencies_1.extractDependencies(context, ['graphqlSchema']); | ||
const { mirageMapper } = extract_dependencies_1.extractDependencies(context, ['mirageMapper'], { required: false }); | ||
const modelName = utils_1.convertModelNameToTypeName(obj === null || obj === void 0 ? void 0 : obj.modelName); | ||
const possibleConcreteTypes = graphqlSchema.getPossibleTypes(abstractType); | ||
const possibleConcreteTypeNames = possibleConcreteTypes.map((type) => type.name); | ||
const modelName = utils_1.convertModelNameToTypeName(obj === null || obj === void 0 ? void 0 : obj.modelName); | ||
// Find the candidate that matches most closely based on matching number of fields | ||
let matchingFieldsCandidate; | ||
let matchingFieldsCandidateError; | ||
try { | ||
matchingFieldsCandidate = useFindInCommon ? utils_1.findMostInCommon(obj, possibleConcreteTypes) : undefined; | ||
matchingFieldsCandidate = useFindInCommon ? utils_1.findTypeWithFieldsMostInCommon(obj, possibleConcreteTypes) : undefined; | ||
} | ||
catch (error) { | ||
matchingFieldsCandidateError = error; | ||
catch (_b) { | ||
'Tried to find a match automatically based on matching properties' + | ||
'from the object against fields in the possible types...'; | ||
} | ||
const mappedModelName = mirageMapper && modelName && mirageMapper.findMatchForModel(modelName); | ||
const candidates = [mappedModelName, modelName, matchingFieldsCandidate].filter(Boolean); | ||
const match = candidates.find((candidateName) => { | ||
const candidate = graphqlSchema.getType(candidateName); | ||
if (!candidate || !(candidate === null || candidate === void 0 ? void 0 : candidate.name)) | ||
return false; | ||
return possibleConcreteTypeNames.includes(candidate.name); | ||
}); | ||
const typenameInModel = '__typename' in obj ? obj.__typename : undefined; | ||
// Prefer a matching type name in this order: | ||
// 1. typenameInModel - embedded __typename property on the object or a model | ||
// 2. mappedModelName - modelName mapped to a type name | ||
// 3. modelName - the modelName property on the object | ||
// 4. matchingFieldsCandidate - a lucky guess at looking and hoping the most | ||
// matching fields is the way to go | ||
const typeNameCandidates = [typenameInModel, mappedModelName, modelName, matchingFieldsCandidate].filter(Boolean); | ||
const match = typeNameCandidates.find((typeNameCandidate) => possibleConcreteTypeNames.includes(typeNameCandidate)); | ||
if (!match) { | ||
const formattedPossibleTypes = possibleConcreteTypeNames.map((c) => `* ${c}`); | ||
const formattedCandidates = candidates.map((c) => `* ${c}`); | ||
const formattedCandidates = typeNameCandidates.map((c) => `* ${c}`); | ||
// InterfaceType or UnionType | ||
const kindName = (_a = abstractType === null || abstractType === void 0 ? void 0 : abstractType.astNode) === null || _a === void 0 ? void 0 : _a.kind.replace('Definition', ''); | ||
let matchingFieldsError; | ||
if (matchingFieldsCandidateError) { | ||
matchingFieldsError = | ||
`Tried to automatically find type based on matching fields. ` + matchingFieldsCandidateError.message; | ||
} | ||
let mirageModelMessage; | ||
@@ -45,11 +43,15 @@ if (modelName) { | ||
`Received model "${modelName}" which did not match one of the possible types above. ` + | ||
`If "${modelName}" represents of the possible types, fix the model name ` + | ||
`or use a MirageGraphQLMapper instance with a type mapping. ` + | ||
`If "${modelName}" represents one of the possible types, fix the model name ` + | ||
`or use a MirageGraphQLMapper instance with a type mapping ["TypeName", "${modelName}"]. ` + | ||
`\n\n` + | ||
`If "${modelName}" is the abstract type "${abstractType.name}", consider ` + | ||
`only creating models for discrete types and using { polymorphic: true } ` + | ||
`for the relationships that represent "${abstractType.name}"`; | ||
`only creating models for discrete types and using { polymorphic: true } along with any ` + | ||
`corresponding { inverse: true } relationships that point to the polymorphic field ` + | ||
`that represent "${abstractType.name}.`; | ||
} | ||
const lastResortMessage = `As a last resort, a manual Type Resolver can be added to the Resolver Map at ` + | ||
`["${abstractType.name}", "__resolveType"] that is passed into the \`createRouteHandler\`, ` + | ||
`\`GraphQLHandler\` instance, or \`pack\` function. Ensure that it returns one of the possible types listed above.`; | ||
`\`GraphQLHandler\` instance, or \`pack\` function. Ensure that it returns one of the possible types listed above.` + | ||
`\n\n` + | ||
`See the the Mirage JS section of the docs at www.graphql-mocks.com for examples.`; | ||
const message = [ | ||
@@ -64,4 +66,2 @@ `Unable to find a matching type for resolving for ${kindName} type "${abstractType.name}".`, | ||
' ', | ||
matchingFieldsError, | ||
' ', | ||
mirageModelMessage, | ||
@@ -73,3 +73,2 @@ ' ', | ||
.join('\n'); | ||
debugger; | ||
throw new Error(message); | ||
@@ -76,0 +75,0 @@ } |
import { GraphQLObjectType } from 'graphql'; | ||
export declare function findMostInCommon(parent: any, eligibleTypes: GraphQLObjectType[]): string | undefined; | ||
export declare function findTypeWithFieldsMostInCommon(parent: any, eligibleTypes: GraphQLObjectType[]): string | undefined; | ||
export declare function convertModelNameToTypeName(modelName: 'string'): string | undefined; | ||
export declare function cleanRelayConnectionName(name: string): string | undefined; | ||
export declare function mirageCursorForNode(node: any): string; |
@@ -16,3 +16,3 @@ "use strict"; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function findMostInCommon(parent, eligibleTypes) { | ||
function findTypeWithFieldsMostInCommon(parent, eligibleTypes) { | ||
var _a; | ||
@@ -37,3 +37,3 @@ let matchedTypes = []; | ||
const matchedFields = parentFields.map((field) => `"${field}"`).join(', '); | ||
const errorMessage = `Multiple types matched (${matchingTypeNames}) the fields: ${matchedFields}.`; | ||
const errorMessage = `Multiple types matched (${matchingTypeNames}) against the fields from the object passed in: ${matchedFields}.`; | ||
throw new Error(errorMessage); | ||
@@ -43,3 +43,3 @@ } | ||
} | ||
exports.findMostInCommon = findMostInCommon; | ||
exports.findTypeWithFieldsMostInCommon = findTypeWithFieldsMostInCommon; | ||
function convertModelNameToTypeName(modelName) { | ||
@@ -46,0 +46,0 @@ return typeof modelName === 'string' ? toPascalCase(modelName.replace('-', '_')) : modelName; |
{ | ||
"name": "graphql-mocks", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "Tools for setting up graphql test resolvers", | ||
@@ -44,2 +44,3 @@ "main": ".index.js", | ||
"eslint-config-prettier": "^6.10.0", | ||
"eslint-plugin-mocha": "^8.0.0", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
@@ -46,0 +47,0 @@ "graphql": "^14", |
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
82368
23
1723