microfiber
Advanced tools
Comparing version 1.3.2 to 2.0.0
@@ -16,4 +16,5 @@ "use strict"; | ||
const KIND_LIST = 'LIST'; | ||
const KIND_NON_NULL = 'NON_NULL'; // An Object containing all the GraphQL Kind values you may encounter. | ||
const KIND_NON_NULL = 'NON_NULL'; | ||
// An Object containing all the GraphQL Kind values you may encounter. | ||
const KINDS = Object.freeze({ | ||
@@ -28,8 +29,8 @@ SCALAR: KIND_SCALAR, | ||
NON_NULL: KIND_NON_NULL | ||
}); // A function that compares 2 types and determines if they have the same Kind and Name. | ||
}); | ||
// A function that compares 2 types and determines if they have the same Kind and Name. | ||
exports.KINDS = KINDS; | ||
function typesAreSame(typeA, typeB) { | ||
return typeA.kind === typeB.kind && typeA.name === typeB.name; | ||
} |
@@ -6,5 +6,3 @@ "use strict"; | ||
}); | ||
var _microfiber = require("./microfiber"); | ||
Object.keys(_microfiber).forEach(function (key) { | ||
@@ -20,5 +18,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _etc = require("./etc"); | ||
Object.keys(_etc).forEach(function (key) { | ||
@@ -25,0 +21,0 @@ if (key === "default" || key === "__esModule") return; |
@@ -9,19 +9,7 @@ "use strict"; | ||
exports.isReservedType = isReservedType; | ||
var _lodash = _interopRequireDefault(require("lodash.get")); | ||
var _lodash2 = _interopRequireDefault(require("lodash.unset")); | ||
var _lodash3 = _interopRequireDefault(require("lodash.defaults")); | ||
var _etc = require("./etc"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
// TODO: | ||
@@ -32,2 +20,3 @@ // | ||
// optimize to only clean if "dirty" and when pulling schema out | ||
const defaultOpts = Object.freeze({ | ||
@@ -56,6 +45,8 @@ // Perform an analysis of the schema right away. | ||
// removeSubscriptionsWithMissingTypes: true, | ||
// Remove all the types and things that are unreferenced immediately? | ||
cleanupSchemaImmediately: true | ||
}); // Map some opts to their corresponding removeType params for proper defaulting | ||
}); | ||
// Map some opts to their corresponding removeType params for proper defaulting | ||
const optsToRemoveTypeParamsMap = Object.freeze({ | ||
@@ -73,3 +64,2 @@ removeFieldsWithMissingTypes: 'removeFieldsOfType', | ||
}); | ||
class Microfiber { | ||
@@ -80,24 +70,20 @@ constructor(response, opts = {}) { | ||
} | ||
opts = (0, _lodash3.default)({}, opts, defaultOpts); | ||
this.setOpts(opts); // The rest of the initialization can be handled by this public method | ||
this.setOpts(opts); | ||
// The rest of the initialization can be handled by this public method | ||
this.setResponse(response); | ||
} | ||
setOpts(opts) { | ||
this.opts = opts || {}; | ||
} // Set/change the response on the instance | ||
} | ||
// Set/change the response on the instance | ||
setResponse(responseIn) { | ||
const response = JSON.parse(JSON.stringify(responseIn)); | ||
if (this.opts._normalize) { | ||
const normalizedResponse = Microfiber.normalizeIntrospectionResponse(response); | ||
if (normalizedResponse !== response) { | ||
this._wasNormalized = true; | ||
} | ||
this.schema = (0, _lodash.default)(normalizedResponse, '__schema'); | ||
@@ -107,20 +93,17 @@ } else { | ||
} | ||
if (this.opts.fixQueryAndMutationAndSubscriptionTypes) { | ||
this._fixQueryAndMutationAndSubscriptionTypes(); | ||
} // OK, time to validate | ||
} | ||
// OK, time to validate | ||
this._validate(); | ||
if (this.opts._analyze) { | ||
this._analyze(); | ||
} | ||
if (this.opts.cleanupSchemaImmediately) { | ||
this.cleanSchema(); | ||
} | ||
} // This is how you get OUT what you've put in and manipulated | ||
} | ||
// This is how you get OUT what you've put in and manipulated | ||
getResponse() { | ||
@@ -130,3 +113,2 @@ const clonedResponse = { | ||
}; | ||
if (this._wasNormalized) { | ||
@@ -137,6 +119,4 @@ return { | ||
} | ||
return clonedResponse; | ||
} | ||
static normalizeIntrospectionResponse(response) { | ||
@@ -146,10 +126,7 @@ if (response && response.data) { | ||
} | ||
return response; | ||
} | ||
static digUnderlyingType(type) { | ||
return digUnderlyingType(type); | ||
} | ||
getAllTypes({ | ||
@@ -172,19 +149,14 @@ // Include reserved GraphQL types? | ||
} | ||
if (queryType && !includeQuery && (0, _etc.typesAreSame)(type, queryType)) { | ||
return false; | ||
} | ||
if (mutationType && !includeMutation && (0, _etc.typesAreSame)(type, mutationType)) { | ||
return false; | ||
} | ||
if (subscriptionType && !includeSubscription && (0, _etc.typesAreSame)(type, subscriptionType)) { | ||
return false; | ||
} | ||
return true; | ||
}); | ||
} | ||
getType({ | ||
@@ -199,7 +171,5 @@ kind = _etc.KINDS.OBJECT, | ||
} | ||
getDirectives() { | ||
return this.schema.directives; | ||
} | ||
getDirective({ | ||
@@ -211,3 +181,2 @@ name | ||
} | ||
return this.getDirectives()[this._getDirectiveIndex({ | ||
@@ -217,3 +186,2 @@ name | ||
} | ||
getQueryType() { | ||
@@ -223,3 +191,2 @@ if (!this.queryTypeName) { | ||
} | ||
return this.getType({ | ||
@@ -230,3 +197,2 @@ kind: _etc.KINDS.OBJECT, | ||
} | ||
getQuery({ | ||
@@ -236,7 +202,5 @@ name | ||
const queryType = this.getQueryType(); | ||
if (!queryType) { | ||
return false; | ||
} | ||
return this.getField({ | ||
@@ -248,3 +212,2 @@ typeKind: queryType.kind, | ||
} | ||
getMutationType() { | ||
@@ -254,3 +217,2 @@ if (!this.mutationTypeName) { | ||
} | ||
return this.getType({ | ||
@@ -261,3 +223,2 @@ kind: _etc.KINDS.OBJECT, | ||
} | ||
getMutation({ | ||
@@ -267,7 +228,5 @@ name | ||
const mutationType = this.getMutationType(); | ||
if (!mutationType) { | ||
return false; | ||
} | ||
return this.getField({ | ||
@@ -279,3 +238,2 @@ typeKind: mutationType.kind, | ||
} | ||
getSubscriptionType() { | ||
@@ -285,3 +243,2 @@ if (!this.subscriptionTypeName) { | ||
} | ||
return this.getType({ | ||
@@ -292,3 +249,2 @@ kind: _etc.KINDS.OBJECT, | ||
} | ||
getSubscription({ | ||
@@ -298,7 +254,5 @@ name | ||
const subscriptionType = this.getSubscriptionType(); | ||
if (!subscriptionType) { | ||
return false; | ||
} | ||
return this.getField({ | ||
@@ -310,3 +264,2 @@ typeKind: subscriptionType.kind, | ||
} | ||
getField({ | ||
@@ -321,16 +274,11 @@ typeKind = _etc.KINDS.OBJECT, | ||
}); | ||
if (!type) { | ||
return; | ||
} | ||
const fieldsProperty = kindToFieldPropertyMap[typeKind]; | ||
if (!(fieldsProperty && type[fieldsProperty])) { | ||
return; | ||
} | ||
return type[fieldsProperty].find(field => field.name === fieldName); | ||
} | ||
getInputField({ | ||
@@ -346,3 +294,2 @@ typeName, | ||
} | ||
getInterfaceField({ | ||
@@ -358,3 +305,2 @@ typeName, | ||
} | ||
getArg({ | ||
@@ -371,10 +317,7 @@ typeKind = _etc.KINDS.OBJECT, | ||
}); | ||
if (!(field && field.args.length)) { | ||
return; | ||
} | ||
return field.args.find(arg => arg.name === argName); | ||
} | ||
getDirectiveArg({ | ||
@@ -387,10 +330,7 @@ directiveName, | ||
}); | ||
if (!(directive && directive.args.length)) { | ||
return; | ||
} | ||
return directive.args.find(arg => arg.name === argName); | ||
} | ||
removeDirective({ | ||
@@ -403,5 +343,3 @@ name, | ||
} | ||
this.schema.directives = this.schema.directives.filter(directive => directive.name !== name); | ||
if (cleanup) { | ||
@@ -411,3 +349,2 @@ this.cleanSchema(); | ||
} | ||
removeType({ | ||
@@ -429,17 +366,14 @@ kind = _etc.KINDS.OBJECT, | ||
}); | ||
if (!Object.prototype.hasOwnProperty.call(this.typeToIndexMap, typeKey)) { | ||
return false; | ||
} | ||
const typeIndex = this.typeToIndexMap[typeKey]; | ||
if (isUndef(typeIndex)) { | ||
return false; | ||
} // Create an object of some of the opts, but mapped to keys that match the params | ||
} | ||
// Create an object of some of the opts, but mapped to keys that match the params | ||
// of this method. They will then be used as the default value for the params | ||
// so that constructor opts will be the default, but they can be overridden in | ||
// the call. | ||
const mappedOpts = mapProps({ | ||
@@ -455,8 +389,7 @@ props: this.opts, | ||
removeDirectiveArgumentsOfType | ||
}, mappedOpts); // If we are going to clean up afterwards, then the others should not have to | ||
}, mappedOpts); | ||
// If we are going to clean up afterwards, then the others should not have to | ||
const shouldOthersClean = !cleanup; | ||
const originalSchema = this._cloneSchema(); | ||
try { | ||
@@ -483,8 +416,7 @@ // If we are removing a special type like a Query or Mutation or Subscription | ||
delete this.schema.subscriptionType; | ||
} // Do this *after* the special stuff above, if necessary | ||
} | ||
// Do this *after* the special stuff above, if necessary | ||
delete this.schema.types[typeIndex]; | ||
delete this.typeToIndexMap[typeKey]; | ||
if (mergedOpts.removeArgsOfType) { | ||
@@ -497,3 +429,2 @@ this._removeArgumentsOfType({ | ||
} | ||
if (mergedOpts.removeFieldsOfType) { | ||
@@ -506,3 +437,2 @@ this._removeFieldsOfType({ | ||
} | ||
if (mergedOpts.removeInputFieldsOfType) { | ||
@@ -514,5 +444,5 @@ this._removeInputFieldsOfType({ | ||
}); | ||
} // AKA Unions | ||
} | ||
// AKA Unions | ||
if (mergedOpts.removePossibleTypesOfType) { | ||
@@ -525,3 +455,2 @@ this._removePossibleTypesOfType({ | ||
} | ||
if (mergedOpts.removeDirectiveArgumentsOfType) { | ||
@@ -534,7 +463,5 @@ this._removeDirectiveArgumentsOfType({ | ||
} | ||
if (cleanup) { | ||
this.cleanSchema(); | ||
} | ||
return true; | ||
@@ -546,3 +473,2 @@ } catch (err) { | ||
} | ||
removeField({ | ||
@@ -559,16 +485,12 @@ typeKind = _etc.KINDS.OBJECT, | ||
}); | ||
if (!type) { | ||
return false; | ||
} | ||
const fieldsProperty = kindToFieldPropertyMap[typeKind]; | ||
if (!(fieldsProperty && type[fieldsProperty])) { | ||
return false; | ||
} // TODO: build a map for the locations of fields on types? | ||
} | ||
// TODO: build a map for the locations of fields on types? | ||
type[fieldsProperty] = type[fieldsProperty].filter(field => field.name !== fieldName); | ||
if (cleanup) { | ||
@@ -578,3 +500,2 @@ this.cleanSchema(); | ||
} | ||
removeInputField({ | ||
@@ -593,3 +514,2 @@ typeName, | ||
} | ||
removeArg({ | ||
@@ -607,17 +527,16 @@ typeKind, | ||
fieldName | ||
}); // field.args should alwys be an array, never null | ||
}); | ||
// field.args should alwys be an array, never null | ||
if (!field) { | ||
return false; | ||
} // TODO: build a map for the locations of args on fields? | ||
} | ||
// TODO: build a map for the locations of args on fields? | ||
field.args = field.args.filter(arg => arg.name !== argName); | ||
if (cleanup) { | ||
this.cleanSchema(); | ||
} | ||
} // Remove just a single possible value for an Enum, but not the whole Enum | ||
} | ||
// Remove just a single possible value for an Enum, but not the whole Enum | ||
removeEnumValue({ | ||
@@ -633,10 +552,7 @@ // The name of the Enum Type | ||
}); | ||
if (!(type && type.enumValues)) { | ||
return false; | ||
} | ||
type.enumValues = type.enumValues.filter(enumValue => enumValue.name !== value); | ||
} | ||
removePossibleType({ | ||
@@ -656,9 +572,6 @@ // The name of the Union Type | ||
}); | ||
if (!(type && type.possibleTypes)) { | ||
return false; | ||
} | ||
type.possibleTypes = type.possibleTypes.filter(possibleType => possibleType.type !== possibleTypeKind && possibleType.name !== possibleTypeName); | ||
if (cleanup) { | ||
@@ -668,3 +581,2 @@ this.cleanSchema(); | ||
} | ||
removeQuery({ | ||
@@ -678,3 +590,2 @@ name, | ||
} | ||
this.removeField({ | ||
@@ -687,3 +598,2 @@ typeKind: _etc.KINDS.OBJECT, | ||
} | ||
removeMutation({ | ||
@@ -697,3 +607,2 @@ name, | ||
} | ||
this.removeField({ | ||
@@ -706,3 +615,2 @@ typeKind: _etc.KINDS.OBJECT, | ||
} | ||
removeSubscription({ | ||
@@ -716,3 +624,2 @@ name, | ||
} | ||
this.removeField({ | ||
@@ -724,5 +631,5 @@ typeKind: _etc.KINDS.OBJECT, | ||
}); | ||
} // Removes all the undefined gaps created by various removals | ||
} | ||
// Removes all the undefined gaps created by various removals | ||
cleanSchema() { | ||
@@ -734,5 +641,6 @@ // Used to compare the schema before and after it was cleaned | ||
const interfacesEncounteredKeys = new Set(); | ||
const interfacesByKey = {}; // The Query, Mutation and Subscription Types should never be removed due to not being referenced | ||
const interfacesByKey = {}; | ||
// The Query, Mutation and Subscription Types should never be removed due to not being referenced | ||
// by anything | ||
if (this.queryTypeName) { | ||
@@ -744,3 +652,2 @@ typesEncountered.add(buildKey({ | ||
} | ||
if (this.mutationTypeName) { | ||
@@ -752,3 +659,2 @@ typesEncountered.add(buildKey({ | ||
} | ||
if (this.subscriptionTypeName) { | ||
@@ -760,3 +666,2 @@ typesEncountered.add(buildKey({ | ||
} | ||
for (const directive of this.schema.directives) { | ||
@@ -766,20 +671,16 @@ if (!directive) { | ||
} | ||
const args = []; | ||
for (const arg of directive.args) { | ||
const argType = digUnderlyingType(arg.type); // Don't add it if its return type does not exist | ||
const argType = digUnderlyingType(arg.type); | ||
// Don't add it if its return type does not exist | ||
if (!this._hasType(argType)) { | ||
continue; | ||
} // Keep track of this so we know what we can remove | ||
} | ||
// Keep track of this so we know what we can remove | ||
typesEncountered.add(buildKey(argType)); | ||
args.push(arg); | ||
} | ||
directive.args = args; | ||
} | ||
for (const type of this.schema.types) { | ||
@@ -789,12 +690,8 @@ if (!type) { | ||
} | ||
types.push(type); | ||
if (type.kind === _etc.KINDS.INTERFACE) { | ||
interfacesByKey[buildKey(type)] = type; | ||
} | ||
if (type.fields) { | ||
const fields = []; | ||
for (const field of type.fields) { | ||
@@ -804,17 +701,15 @@ if (isUndef(field)) { | ||
} | ||
const fieldType = digUnderlyingType(field.type); | ||
const fieldType = digUnderlyingType(field.type); // Don't add it if its return type does not exist | ||
// Don't add it if its return type does not exist | ||
if (!this._hasType(fieldType)) { | ||
continue; | ||
} | ||
if (fieldType.kind === _etc.KINDS.INTERFACE) { | ||
interfacesEncounteredKeys.add(buildKey(fieldType)); | ||
} // Keep track of this so we know what we can remove | ||
} | ||
// Keep track of this so we know what we can remove | ||
typesEncountered.add(buildKey(fieldType)); | ||
const args = []; | ||
for (const arg of field.args || []) { | ||
@@ -824,25 +719,22 @@ if (isUndef(arg)) { | ||
} | ||
const argType = digUnderlyingType(arg.type); // Don't add it if its return type does not exist | ||
const argType = digUnderlyingType(arg.type); | ||
// Don't add it if its return type does not exist | ||
if (!this._hasType(argType)) { | ||
continue; | ||
} // Keep track of this so we know what we can remove | ||
} | ||
// Keep track of this so we know what we can remove | ||
typesEncountered.add(buildKey(argType)); | ||
args.push(arg); | ||
} // Args will always be an array. Possible empty, but never null | ||
} | ||
// Args will always be an array. Possible empty, but never null | ||
field.args = args; | ||
fields.push(field); | ||
} | ||
type.fields = fields; | ||
} | ||
if (type.inputFields) { | ||
const inputFields = []; // Don't add it in if it's undefined, or the type is gone | ||
const inputFields = []; | ||
// Don't add it in if it's undefined, or the type is gone | ||
for (const inputField of type.inputFields) { | ||
@@ -852,36 +744,32 @@ if (isUndef(inputField)) { | ||
} | ||
const inputFieldType = digUnderlyingType(inputField.type); // Don't add it if its return type does not exist | ||
const inputFieldType = digUnderlyingType(inputField.type); | ||
// Don't add it if its return type does not exist | ||
if (!this._hasType(inputFieldType)) { | ||
continue; | ||
} // Keep track of this so we know what we can remove | ||
} | ||
// Keep track of this so we know what we can remove | ||
typesEncountered.add(buildKey(inputFieldType)); | ||
inputFields.push(inputField); | ||
} | ||
type.inputFields = inputFields; | ||
} | ||
if (type.possibleTypes) { | ||
const possibleTypes = []; | ||
for (const possibleType of type.possibleTypes) { | ||
if (isUndef(possibleType)) { | ||
continue; | ||
} // possibleTypes array entries have no envelope for the type | ||
} | ||
// possibleTypes array entries have no envelope for the type | ||
// so do not do possibleType.type here | ||
const possibleTypeType = digUnderlyingType(possibleType); // Don't add it if its return type does not exist | ||
const possibleTypeType = digUnderlyingType(possibleType); | ||
// Don't add it if its return type does not exist | ||
if (!this._hasType(possibleTypeType)) { | ||
continue; | ||
} // Interfaces themselves list all the things that have "implemented" them | ||
} | ||
// Interfaces themselves list all the things that have "implemented" them | ||
// in the "possibleTypes" array...but we don't want that to be an indication | ||
// that the thing has been used. | ||
if (type.kind !== _etc.KINDS.INTERFACE) { | ||
@@ -891,12 +779,8 @@ // Keep track of this so we know what we can remove | ||
} | ||
possibleTypes.push(possibleType); | ||
} | ||
type.possibleTypes = possibleTypes; | ||
} | ||
if (type.interfaces) { | ||
const interfaces = []; | ||
for (const interfayce of type.interfaces) { | ||
@@ -906,43 +790,43 @@ if (isUndef(interfayce)) { | ||
} | ||
if (!this._hasType(interfayce)) { | ||
continue; | ||
} | ||
typesEncountered.add(buildKey(interfayce)); | ||
interfaces.push(interfayce); | ||
} | ||
type.interfaces = interfaces; | ||
} | ||
} | ||
for (const interfaceEncounteredKey of interfacesEncounteredKeys.values()) { | ||
const interfayce = interfacesByKey[interfaceEncounteredKey]; | ||
for (const possibleType of (interfayce === null || interfayce === void 0 ? void 0 : interfayce.possibleTypes) || []) { | ||
if (!this._hasType(possibleType)) { | ||
continue; | ||
} // Keep track of this so we know what we can remove | ||
} | ||
// Keep track of this so we know what we can remove | ||
typesEncountered.add(buildKey(possibleType)); | ||
} | ||
} // Only include Types that we encountered - if the options say to do so | ||
} | ||
// Only include Types that we encountered - if the options say to do so | ||
const possiblyFilteredTypes = this.opts.removeUnusedTypes ? types.filter(type => isReservedType(type) || typesEncountered.has(buildKey(type))) : types; | ||
const possiblyFilteredTypes = this.opts.removeUnusedTypes ? types.filter(type => isReservedType(type) || typesEncountered.has(buildKey(type))) : types; // Replace the Schema | ||
this.schema = _objectSpread(_objectSpread({}, this.schema), {}, { | ||
// Replace the Schema | ||
this.schema = { | ||
...this.schema, | ||
types: possiblyFilteredTypes | ||
}); // Need to re-analyze it, too | ||
}; | ||
this._analyze(); // If the schema was changed by this cleanup, we should run it again to see if other things | ||
// Need to re-analyze it, too | ||
this._analyze(); | ||
// If the schema was changed by this cleanup, we should run it again to see if other things | ||
// should be removed...and continue to do so until the schema is stable. | ||
if (schemaToStart !== JSON.stringify(this.schema)) { | ||
return this.cleanSchema(); | ||
} | ||
} //****************************************************************** | ||
} | ||
//****************************************************************** | ||
// | ||
@@ -954,3 +838,2 @@ // | ||
_validate() { | ||
@@ -960,8 +843,7 @@ if (!this.schema) { | ||
} | ||
if (!this.schema.types) { | ||
throw new Error('No types detected!'); | ||
} // Must have a Query type...but not necessarily a Mutation type | ||
} | ||
// Must have a Query type...but not necessarily a Mutation type | ||
if (!(0, _lodash.default)(this.schema, `queryType.name`)) { | ||
@@ -971,3 +853,2 @@ throw new Error(`No queryType detected!`); | ||
} | ||
_analyze() { | ||
@@ -977,20 +858,18 @@ // Map the kind + name to the index in the types array | ||
this.fieldsOfTypeMap = {}; | ||
this.inputFieldsOfTypeMap = {}; // AKA Unions | ||
this.inputFieldsOfTypeMap = {}; | ||
// AKA Unions | ||
this.possibleTypesOfTypeMap = {}; | ||
this.argsOfTypeMap = {}; | ||
this.directiveToIndexMap = {}; | ||
this.directiveArgsOfTypeMap = {}; // Need to keep track of these so that we never remove them for not being referenced | ||
this.directiveArgsOfTypeMap = {}; | ||
// Need to keep track of these so that we never remove them for not being referenced | ||
this.queryTypeName = (0, _lodash.default)(this.schema, 'queryType.name'); | ||
this.mutationTypeName = (0, _lodash.default)(this.schema, 'mutationType.name'); | ||
this.subscriptionTypeName = (0, _lodash.default)(this.schema, 'subscriptionType.name'); | ||
for (let directivesIdx = 0; directivesIdx < this.schema.directives.length; directivesIdx++) { | ||
const directive = this.schema.directives[directivesIdx]; | ||
if (isUndef(directive)) { | ||
continue; | ||
} | ||
const directivesKey = buildKey({ | ||
@@ -1002,17 +881,12 @@ kind: 'DIRECTIVE', | ||
const directivePath = `directives.${directivesIdx}`; | ||
for (let argsIdx = 0; argsIdx < directive.args.length; argsIdx++) { | ||
const arg = directive.args[argsIdx]; | ||
if (isUndef(arg)) { | ||
continue; | ||
} | ||
const argType = digUnderlyingType(arg.type); | ||
const argsKey = buildKey(argType); | ||
if (!this.directiveArgsOfTypeMap[argsKey]) { | ||
this.directiveArgsOfTypeMap[argsKey] = []; | ||
} | ||
const argPath = `${directivePath}.args.${argsIdx}`; | ||
@@ -1022,15 +896,12 @@ this.directiveArgsOfTypeMap[argsKey].push(argPath); | ||
} | ||
for (let typesIdx = 0; typesIdx < this.schema.types.length; typesIdx++) { | ||
const type = this.schema.types[typesIdx]; | ||
if (isUndef(type)) { | ||
continue; | ||
} | ||
const { | ||
kind, | ||
name | ||
} = type; // These come in as null, not undefined | ||
} = type; | ||
// These come in as null, not undefined | ||
const fields = type.fields || []; | ||
@@ -1044,36 +915,26 @@ const inputFields = type.inputFields || []; | ||
this.typeToIndexMap[typesKey] = typesIdx; | ||
for (let fieldsIdx = 0; fieldsIdx < fields.length; fieldsIdx++) { | ||
const field = fields[fieldsIdx]; | ||
if (isUndef(field)) { | ||
continue; | ||
} | ||
const fieldType = digUnderlyingType(field.type); // This should always be arrays...maybe empty, never null | ||
const fieldType = digUnderlyingType(field.type); | ||
// This should always be arrays...maybe empty, never null | ||
const args = field.args || []; | ||
const fieldsKey = buildKey(fieldType); | ||
if (!this.fieldsOfTypeMap[fieldsKey]) { | ||
this.fieldsOfTypeMap[fieldsKey] = []; | ||
} | ||
const fieldPath = `types.${typesIdx}.fields.${fieldsIdx}`; | ||
this.fieldsOfTypeMap[fieldsKey].push(fieldPath); | ||
for (let argsIdx = 0; argsIdx < args.length; argsIdx++) { | ||
const arg = args[argsIdx]; | ||
if (isUndef(arg)) { | ||
continue; | ||
} | ||
const argType = digUnderlyingType(arg.type); | ||
const argsKey = buildKey(argType); | ||
if (!this.argsOfTypeMap[argsKey]) { | ||
this.argsOfTypeMap[argsKey] = []; | ||
} | ||
const argPath = `${fieldPath}.args.${argsIdx}`; | ||
@@ -1083,35 +944,25 @@ this.argsOfTypeMap[argsKey].push(argPath); | ||
} | ||
for (let inputFieldsIdx = 0; inputFieldsIdx < inputFields.length; inputFieldsIdx++) { | ||
const inputField = inputFields[inputFieldsIdx]; | ||
if (isUndef(inputField)) { | ||
continue; | ||
} | ||
const inputFieldType = digUnderlyingType(inputField.type); | ||
const inputFieldsKey = buildKey(inputFieldType); | ||
if (!this.inputFieldsOfTypeMap[inputFieldsKey]) { | ||
this.inputFieldsOfTypeMap[inputFieldsKey] = []; | ||
} | ||
const inputFieldPath = `types.${typesIdx}.inputFields.${inputFieldsIdx}`; | ||
this.inputFieldsOfTypeMap[inputFieldsKey].push(inputFieldPath); | ||
} | ||
for (let possibleTypesIdx = 0; possibleTypesIdx < possibleTypes.length; possibleTypesIdx++) { | ||
const possibleType = possibleTypes[possibleTypesIdx]; | ||
if (isUndef(possibleType)) { | ||
continue; | ||
} | ||
const possibleTypeType = digUnderlyingType(possibleType); | ||
const possibleTypeKey = buildKey(possibleTypeType); | ||
if (!this.possibleTypesOfTypeMap[possibleTypeKey]) { | ||
this.possibleTypesOfTypeMap[possibleTypeKey] = []; | ||
} | ||
const possibleTypePath = `types.${typesIdx}.possibleTypes.${possibleTypesIdx}`; | ||
@@ -1122,7 +973,5 @@ this.possibleTypesOfTypeMap[possibleTypeKey].push(possibleTypePath); | ||
} | ||
_fixQueryAndMutationAndSubscriptionTypes(response) { | ||
for (const [key, defaultTypeName] of [['queryType', 'Query'], ['mutationType', 'Mutation'], ['subscriptionType', 'Subscription']]) { | ||
const queryOrMutationOrSubscriptionTypeName = (0, _lodash.default)(response, `__schema.${key}.name`); | ||
if (queryOrMutationOrSubscriptionTypeName && !this.getType({ | ||
@@ -1138,3 +987,2 @@ kind: _etc.KINDS.OBJECT, | ||
} | ||
_getTypeIndex({ | ||
@@ -1148,10 +996,7 @@ kind, | ||
}); | ||
if (Object.prototype.hasOwnProperty.call(this.typeToIndexMap, key)) { | ||
return this.typeToIndexMap[key]; | ||
} | ||
return false; | ||
} | ||
_getDirectiveIndex({ | ||
@@ -1164,10 +1009,7 @@ name | ||
}); | ||
if (Object.prototype.hasOwnProperty.call(this.directiveToIndexMap, key)) { | ||
return this.directiveToIndexMap[key]; | ||
} | ||
return false; | ||
} | ||
_removeThingsOfType({ | ||
@@ -1183,9 +1025,6 @@ kind, | ||
}); | ||
for (const path of map[key] || []) { | ||
(0, _lodash2.default)(this.schema, path); | ||
} | ||
delete map[key]; | ||
if (cleanup) { | ||
@@ -1195,3 +1034,2 @@ this.cleanSchema(); | ||
} | ||
_removeFieldsOfType({ | ||
@@ -1210,3 +1048,2 @@ kind, | ||
} | ||
_removeInputFieldsOfType({ | ||
@@ -1224,5 +1061,5 @@ kind, | ||
}); | ||
} // AKA Unions | ||
} | ||
// AKA Unions | ||
_removePossibleTypesOfType({ | ||
@@ -1241,3 +1078,2 @@ kind, | ||
} | ||
_removeArgumentsOfType({ | ||
@@ -1256,3 +1092,2 @@ kind, | ||
} | ||
_removeDirectiveArgumentsOfType({ | ||
@@ -1271,7 +1106,5 @@ // kind, | ||
} | ||
_cloneSchema() { | ||
return JSON.parse(JSON.stringify(this.schema)); | ||
} | ||
_hasType({ | ||
@@ -1287,8 +1120,6 @@ kind, | ||
} | ||
} | ||
} // A function that digs through any Non-Null and List nesting and returns the underlying Type. | ||
// A function that digs through any Non-Null and List nesting and returns the underlying Type. | ||
exports.Microfiber = Microfiber; | ||
function digUnderlyingType(type) { | ||
@@ -1298,11 +1129,9 @@ while ([_etc.KINDS.NON_NULL, _etc.KINDS.LIST].includes(type.kind)) { | ||
} | ||
return type; | ||
} // A function that returns a Boolean indicating whether a Type is special GraphQL reserved Type. | ||
} | ||
// A function that returns a Boolean indicating whether a Type is special GraphQL reserved Type. | ||
function isReservedType(type) { | ||
return type.name.startsWith('__'); | ||
} | ||
function buildKey({ | ||
@@ -1314,7 +1143,5 @@ kind, | ||
} | ||
function isUndef(item) { | ||
return typeof item === 'undefined'; | ||
} | ||
function mapProps({ | ||
@@ -1328,5 +1155,4 @@ props, | ||
} | ||
return acc; | ||
}, {}); | ||
} |
{ | ||
"name": "microfiber", | ||
"version": "1.3.2", | ||
"version": "2.0.0", | ||
"description": "A library to query and manipulate GraphQL Introspection Query results in some useful ways.", | ||
@@ -35,3 +35,3 @@ "author": "Chris Newhouse", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=14" | ||
}, | ||
@@ -44,5 +44,5 @@ "scripts": { | ||
"pub:dry-run": "yarn prepare && npm publish --dry-run", | ||
"test": "yarn mocha --config ./test/mocha-config.js", | ||
"test:watch": "nodemon -x 'yarn test' --ignore dist/", | ||
"test:debug:watch": "nodemon -x 'yarn test:debug' --ignore dist/", | ||
"test": "yarn prepare && yarn mocha --config ./test/mocha-config.js", | ||
"test:debug": "yarn test --node-option inspect=0.0.0.0:9223" | ||
@@ -66,3 +66,3 @@ }, | ||
"lodash.isequal": "^4.5.0", | ||
"mocha": "^9.2.2", | ||
"mocha": "^10.2.0", | ||
"nodemon": "^2.0.20", | ||
@@ -69,0 +69,0 @@ "rimraf": "^3.0.2" |
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
8
1089
46985