From 6f39add0a9ebd9cfa7f4b7f5e29b816441c10b20 Mon Sep 17 00:00:00 2001 From: dugenkui Date: Thu, 30 Jul 2020 00:48:31 +0800 Subject: [PATCH] check duplicate argument define in directive and type --- src/type/validate.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/type/validate.js b/src/type/validate.js index 90ef8a3670..4e5ed1f89e 100644 --- a/src/type/validate.js +++ b/src/type/validate.js @@ -171,18 +171,28 @@ function validateDirectives(context: SchemaValidationContext): void { // TODO: Ensure proper locations. // Ensure the arguments are valid. + const knownArgNames = Object.create(null); for (const arg of directive.args) { // Ensure they are named correctly. validateName(context, arg); - + const argName = arg.name; // Ensure the type is an input type. if (!isInputType(arg.type)) { context.reportError( - `The type of @${directive.name}(${arg.name}:) must be Input Type ` + - `but got: ${inspect(arg.type)}.`, + `The type of @${directive.name}(${argName}:) must be Input Type ` + + `but got: ${inspect(arg.type)}.`, arg.astNode, ); } + // Ensure the argument must have a unique name. + if (knownArgNames[argName]) { + context.reportError( + `There can be only one argument named "${argName}".`, + directive.astNode, + ); + } else { + knownArgNames[argName] = argName; + } } } } @@ -274,6 +284,7 @@ function validateFields( } // Ensure the arguments are valid + const knownArgNames = Object.create(null); for (const arg of field.args) { const argName = arg.name; @@ -288,6 +299,16 @@ function validateFields( arg.astNode?.type, ); } + + // Ensure the argument must have a unique name. + if (knownArgNames[argName]) { + context.reportError( + `There can be only one argument named ${argName}.`, + field.astNode?.type, + ); + } else { + knownArgNames[argName] = argName; + } } } }