github-pages-deploy-action/node_modules/graphql/validation/rules/ProvidedRequiredArgumentsRule.mjs

121 lines
5.3 KiB
JavaScript
Raw Normal View History

2020-03-07 11:45:40 +08:00
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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; }
import inspect from "../../jsutils/inspect.mjs";
import keyMap from "../../jsutils/keyMap.mjs";
import { GraphQLError } from "../../error/GraphQLError.mjs";
import { Kind } from "../../language/kinds.mjs";
import { print } from "../../language/printer.mjs";
import { specifiedDirectives } from "../../type/directives.mjs";
import { isType, isRequiredArgument } from "../../type/definition.mjs";
/**
* Provided required arguments
*
* A field or directive is only valid if all required (non-null without a
* default value) field arguments have been provided.
*/
export function ProvidedRequiredArgumentsRule(context) {
return _objectSpread({}, ProvidedRequiredArgumentsOnDirectivesRule(context), {
Field: {
// Validate on leave to allow for deeper errors to appear first.
leave: function leave(fieldNode) {
var _fieldNode$arguments;
var fieldDef = context.getFieldDef();
if (!fieldDef) {
return false;
}
/* istanbul ignore next (See https://github.com/graphql/graphql-js/issues/2203) */
var argNodes = (_fieldNode$arguments = fieldNode.arguments) !== null && _fieldNode$arguments !== void 0 ? _fieldNode$arguments : [];
var argNodeMap = keyMap(argNodes, function (arg) {
return arg.name.value;
});
for (var _i2 = 0, _fieldDef$args2 = fieldDef.args; _i2 < _fieldDef$args2.length; _i2++) {
var argDef = _fieldDef$args2[_i2];
var argNode = argNodeMap[argDef.name];
if (!argNode && isRequiredArgument(argDef)) {
var argTypeStr = inspect(argDef.type);
context.reportError(new GraphQLError("Field \"".concat(fieldDef.name, "\" argument \"").concat(argDef.name, "\" of type \"").concat(argTypeStr, "\" is required, but it was not provided."), fieldNode));
}
}
}
}
});
}
/**
* @internal
*/
export function ProvidedRequiredArgumentsOnDirectivesRule(context) {
var requiredArgsMap = Object.create(null);
var schema = context.getSchema();
var definedDirectives = schema ? schema.getDirectives() : specifiedDirectives;
for (var _i4 = 0; _i4 < definedDirectives.length; _i4++) {
var directive = definedDirectives[_i4];
requiredArgsMap[directive.name] = keyMap(directive.args.filter(isRequiredArgument), function (arg) {
return arg.name;
});
}
var astDefinitions = context.getDocument().definitions;
for (var _i6 = 0; _i6 < astDefinitions.length; _i6++) {
var def = astDefinitions[_i6];
if (def.kind === Kind.DIRECTIVE_DEFINITION) {
var _def$arguments;
/* istanbul ignore next (See https://github.com/graphql/graphql-js/issues/2203) */
var argNodes = (_def$arguments = def.arguments) !== null && _def$arguments !== void 0 ? _def$arguments : [];
requiredArgsMap[def.name.value] = keyMap(argNodes.filter(isRequiredArgumentNode), function (arg) {
return arg.name.value;
});
}
}
return {
Directive: {
// Validate on leave to allow for deeper errors to appear first.
leave: function leave(directiveNode) {
var directiveName = directiveNode.name.value;
var requiredArgs = requiredArgsMap[directiveName];
if (requiredArgs) {
var _directiveNode$argume;
/* istanbul ignore next (See https://github.com/graphql/graphql-js/issues/2203) */
var _argNodes = (_directiveNode$argume = directiveNode.arguments) !== null && _directiveNode$argume !== void 0 ? _directiveNode$argume : [];
var argNodeMap = keyMap(_argNodes, function (arg) {
return arg.name.value;
});
for (var _i8 = 0, _Object$keys2 = Object.keys(requiredArgs); _i8 < _Object$keys2.length; _i8++) {
var argName = _Object$keys2[_i8];
if (!argNodeMap[argName]) {
var argType = requiredArgs[argName].type;
var argTypeStr = isType(argType) ? inspect(argType) : print(argType);
context.reportError(new GraphQLError("Directive \"@".concat(directiveName, "\" argument \"").concat(argName, "\" of type \"").concat(argTypeStr, "\" is required, but it was not provided."), directiveNode));
}
}
}
}
}
};
}
function isRequiredArgumentNode(arg) {
return arg.type.kind === Kind.NON_NULL_TYPE && arg.defaultValue == null;
}