Skip to content

Commit

Permalink
Improve exception description in case of duplicate field
Browse files Browse the repository at this point in the history
  • Loading branch information
gulien committed Jan 15, 2020
1 parent 4fe8fcb commit 1e29c0a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ private function getFieldsByAnnotations($controller, string $annotationName, boo
$refClass = new ReflectionClass($controller);

$queryList = [];
/** @var array<string, ReflectionMethod> $refMethodByFields */
$refMethodByFields = [];

$oldDeclaringClass = null;
$context = null;
Expand Down Expand Up @@ -316,10 +318,11 @@ public function handle(QueryFieldDescriptor $fieldDescriptor): ?FieldDefinition
});

if ($field !== null) {
if (isset($queryList[$fieldDescriptor->getName()])) {
throw DuplicateMappingException::createForQuery($refClass->getName(), $fieldDescriptor->getName());
if (isset($refMethodByFields[$name])) {
throw DuplicateMappingException::createForQuery($refClass->getName(), $name, $refMethodByFields[$name], $refMethod);
}
$queryList[$fieldDescriptor->getName()] = $field;
$queryList[$name] = $field;
$refMethodByFields[$name] = $refMethod;
}

/*if ($unauthorized) {
Expand Down
4 changes: 2 additions & 2 deletions src/Mappers/DuplicateMappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public static function createForTypeName(string $type, string $sourceClass1, str
throw new self(sprintf("The type '%s' is created by 2 different classes: '%s' and '%s'", $type, $sourceClass1, $sourceClass2));
}

public static function createForQuery(string $sourceClass, string $queryName): self
public static function createForQuery(string $sourceClass, string $queryName, \ReflectionMethod $method1, \ReflectionMethod $method2): self
{
throw new self(sprintf("The query/mutation '%s' is declared twice in class '%s'", $queryName, $sourceClass));
throw new self(sprintf("The query/mutation/field '%s' is declared twice in class '%s'. First in '%s::%s()', second in '%s::%s()'", $queryName, $sourceClass, $method1->getDeclaringClass()->getName(), $method1->getName(), $method2->getDeclaringClass()->getName(), $method2->getName()));
}

public static function createForQueryInTwoControllers(string $sourceClass1, string $sourceClass2, string $queryName): self
Expand Down

0 comments on commit 1e29c0a

Please sign in to comment.