apollo-progressive-fragment-matcher
Version:
A smart alternative to the introspection fragment matcher.
124 lines (105 loc) • 3.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.addTypeIntrospections = void 0;
var _language = require("graphql/language");
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, 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; }
var defaultConfig = {
possibleTypes: {},
generateAliasName: function generateAliasName(type) {
return "__".concat(type, "__");
}
};
var addTypeIntrospections = function addTypeIntrospections(originalQuery, config) {
var _defaultConfig$config = _objectSpread({}, defaultConfig, config),
possibleTypes = _defaultConfig$config.possibleTypes,
generateAliasName = _defaultConfig$config.generateAliasName;
var types = [];
var extractFragmentName = function extractFragmentName(node) {
var type = node.typeCondition.name.value;
if (!types.includes(type) && !possibleTypes[type]) {
types.push(type);
}
}; // iterate query AST and extract fragment types.
var query = (0, _language.visit)(originalQuery, {
InlineFragment: {
enter: extractFragmentName
},
FragmentDefinition: {
enter: extractFragmentName
}
});
var operationDefinition = query.definitions.find(function (_ref) {
var kind = _ref.kind;
return kind === 'OperationDefinition';
});
for (var _i = 0; _i < types.length; _i++) {
var type = types[_i];
operationDefinition.selectionSet.selections = [].concat(_toConsumableArray(operationDefinition.selectionSet.selections), [createTypeIntrospectionSelection({
type: type,
alias: generateAliasName(type)
})]);
}
return {
types: types,
query: query
};
};
exports.addTypeIntrospections = addTypeIntrospections;
var createTypeIntrospectionSelection = function createTypeIntrospectionSelection(_ref2) {
var type = _ref2.type,
alias = _ref2.alias;
return {
kind: 'Field',
alias: {
kind: 'Name',
value: alias
},
name: {
kind: 'Name',
value: '__type'
},
arguments: [{
kind: 'Argument',
name: {
kind: 'Name',
value: 'name'
},
value: {
kind: 'StringValue',
value: type,
block: false
}
}],
selectionSet: {
kind: 'SelectionSet',
selections: [{
kind: 'Field',
name: {
kind: 'Name',
value: 'possibleTypes'
},
arguments: [],
directives: [],
selectionSet: {
kind: 'SelectionSet',
selections: [{
kind: 'Field',
name: {
kind: 'Name',
value: 'name'
},
arguments: [],
directives: []
}]
}
}]
}
};
};