| 'use strict'; |
| |
| Object.defineProperty(exports, '__esModule', { |
| value: true, |
| }); |
| exports.ProvidedRequiredArgumentsOnDirectivesRule = |
| ProvidedRequiredArgumentsOnDirectivesRule; |
| exports.ProvidedRequiredArgumentsRule = ProvidedRequiredArgumentsRule; |
| |
| var _inspect = require('../../jsutils/inspect.js'); |
| |
| var _keyMap = require('../../jsutils/keyMap.js'); |
| |
| var _GraphQLError = require('../../error/GraphQLError.js'); |
| |
| var _kinds = require('../../language/kinds.js'); |
| |
| var _printer = require('../../language/printer.js'); |
| |
| var _definition = require('../../type/definition.js'); |
| |
| var _directives = require('../../type/directives.js'); |
| |
| /** @category Validation Rules */ |
| |
| /** |
| * Provided required arguments |
| * |
| * A field or directive is only valid if all required (non-null without a |
| * default value) field arguments have been provided. |
| * @param context - The validation context used while checking the document. |
| * @returns A visitor that reports validation errors for this rule. |
| * @example |
| * ```ts |
| * import { buildSchema, parse, validate } from 'graphql'; |
| * import { ProvidedRequiredArgumentsRule } from 'graphql/validation'; |
| * |
| * const schema = buildSchema(` |
| * type Query { |
| * field(required: String!): String |
| * } |
| * `); |
| * |
| * const invalidDocument = parse(` |
| * { field } |
| * `); |
| * const invalidErrors = validate(schema, invalidDocument, [ProvidedRequiredArgumentsRule]); |
| * |
| * invalidErrors.length; // => 1 |
| * |
| * const validDocument = parse(` |
| * { field(required: "x") } |
| * `); |
| * const validErrors = validate(schema, validDocument, [ProvidedRequiredArgumentsRule]); |
| * |
| * validErrors; // => [] |
| * ``` |
| */ |
| function ProvidedRequiredArgumentsRule(context) { |
| return { |
| // eslint-disable-next-line new-cap |
| ...ProvidedRequiredArgumentsOnDirectivesRule(context), |
| Field: { |
| // Validate on leave to allow for deeper errors to appear first. |
| leave(fieldNode) { |
| var _fieldNode$arguments; |
| |
| const fieldDef = context.getFieldDef(); |
| |
| if (!fieldDef) { |
| return false; |
| } |
| |
| const providedArgs = new Set( // FIXME: https://github.com/graphql/graphql-js/issues/2203 |
| /* c8 ignore next */ |
| (_fieldNode$arguments = fieldNode.arguments) === null || |
| _fieldNode$arguments === void 0 |
| ? void 0 |
| : _fieldNode$arguments.map((arg) => arg.name.value), |
| ); |
| |
| for (const argDef of fieldDef.args) { |
| if ( |
| !providedArgs.has(argDef.name) && |
| (0, _definition.isRequiredArgument)(argDef) |
| ) { |
| const argTypeStr = (0, _inspect.inspect)(argDef.type); |
| context.reportError( |
| new _GraphQLError.GraphQLError( |
| `Field "${fieldDef.name}" argument "${argDef.name}" of type "${argTypeStr}" is required, but it was not provided.`, |
| { |
| nodes: fieldNode, |
| }, |
| ), |
| ); |
| } |
| } |
| }, |
| }, |
| }; |
| } |
| /** @internal */ |
| |
| function ProvidedRequiredArgumentsOnDirectivesRule(context) { |
| var _schema$getDirectives; |
| |
| const requiredArgsMap = Object.create(null); |
| const schema = context.getSchema(); |
| const definedDirectives = |
| (_schema$getDirectives = |
| schema === null || schema === void 0 |
| ? void 0 |
| : schema.getDirectives()) !== null && _schema$getDirectives !== void 0 |
| ? _schema$getDirectives |
| : _directives.specifiedDirectives; |
| |
| for (const directive of definedDirectives) { |
| requiredArgsMap[directive.name] = (0, _keyMap.keyMap)( |
| directive.args.filter(_definition.isRequiredArgument), |
| (arg) => arg.name, |
| ); |
| } |
| |
| const astDefinitions = context.getDocument().definitions; |
| |
| for (const def of astDefinitions) { |
| if (def.kind === _kinds.Kind.DIRECTIVE_DEFINITION) { |
| var _def$arguments; |
| |
| // FIXME: https://github.com/graphql/graphql-js/issues/2203 |
| |
| /* c8 ignore next */ |
| const argNodes = |
| (_def$arguments = def.arguments) !== null && _def$arguments !== void 0 |
| ? _def$arguments |
| : []; |
| requiredArgsMap[def.name.value] = (0, _keyMap.keyMap)( |
| argNodes.filter(isRequiredArgumentNode), |
| (arg) => arg.name.value, |
| ); |
| } |
| } |
| |
| return { |
| Directive: { |
| // Validate on leave to allow for deeper errors to appear first. |
| leave(directiveNode) { |
| const directiveName = directiveNode.name.value; |
| const requiredArgs = requiredArgsMap[directiveName]; |
| |
| if (requiredArgs) { |
| var _directiveNode$argume; |
| |
| // FIXME: https://github.com/graphql/graphql-js/issues/2203 |
| |
| /* c8 ignore next */ |
| const argNodes = |
| (_directiveNode$argume = directiveNode.arguments) !== null && |
| _directiveNode$argume !== void 0 |
| ? _directiveNode$argume |
| : []; |
| const argNodeMap = new Set(argNodes.map((arg) => arg.name.value)); |
| |
| for (const [argName, argDef] of Object.entries(requiredArgs)) { |
| if (!argNodeMap.has(argName)) { |
| const argType = (0, _definition.isType)(argDef.type) |
| ? (0, _inspect.inspect)(argDef.type) |
| : (0, _printer.print)(argDef.type); |
| context.reportError( |
| new _GraphQLError.GraphQLError( |
| `Directive "@${directiveName}" argument "${argName}" of type "${argType}" is required, but it was not provided.`, |
| { |
| nodes: directiveNode, |
| }, |
| ), |
| ); |
| } |
| } |
| } |
| }, |
| }, |
| }; |
| } |
| |
| function isRequiredArgumentNode(arg) { |
| return ( |
| arg.type.kind === _kinds.Kind.NON_NULL_TYPE && arg.defaultValue == null |
| ); |
| } |