Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use TheCodingMachine\GraphQLite\Annotations\SourceFieldInterface;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeExceptionInterface;
use TheCodingMachine\GraphQLite\Mappers\DuplicateMappingException;
use TheCodingMachine\GraphQLite\Mappers\Parameters\ParameterMiddlewareInterface;
use TheCodingMachine\GraphQLite\Mappers\Parameters\TypeHandler;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
Expand All @@ -31,6 +32,7 @@
use Webmozart\Assert\Assert;
use function array_merge;
use function array_shift;
use function array_values;
use function get_parent_class;
use function ucfirst;

Expand Down Expand Up @@ -317,7 +319,10 @@ public function handle(QueryFieldDescriptor $fieldDescriptor): ?FieldDefinition
});

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

/*if ($unauthorized) {
Expand All @@ -332,7 +337,7 @@ public function handle(QueryFieldDescriptor $fieldDescriptor): ?FieldDefinition
}*/
}

return $queryList;
return array_values($queryList);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Mappers/DuplicateMappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,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
{
throw new self(sprintf("The query '%s' is duplicate in class '%s'", $queryName, $sourceClass));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace TheCodingMachine\GraphQLite\Fixtures\DuplicateQueries;

use ArrayObject;
use TheCodingMachine\GraphQLite\Annotations\Query;

class TestControllerWithDuplicateQuery
{
/**
* @Query(name="duplicateQuery")
*/
public function testDuplicateQuery1(): string
{
return 'string1';
}

/**
* @Query(name="duplicateQuery")
*/
public function testDuplicateQuery2(): string
{
return 'string2';
}
}
31 changes: 29 additions & 2 deletions tests/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\Psr16Adapter;
use Symfony\Component\Cache\Psr16Cache;
use Symfony\Component\Cache\Simple\ArrayCache;
use Symfony\Component\Cache\Simple\PhpFilesCache;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use TheCodingMachine\GraphQLite\Containers\BasicAutoWiringContainer;
use TheCodingMachine\GraphQLite\Containers\EmptyContainer;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
use TheCodingMachine\GraphQLite\Mappers\CompositeTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\DuplicateMappingException;
use TheCodingMachine\GraphQLite\Mappers\Parameters\ContainerParameterHandler;
use TheCodingMachine\GraphQLite\Mappers\Parameters\TypeHandler;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
Expand All @@ -32,6 +31,7 @@
use TheCodingMachine\GraphQLite\Security\VoidAuthorizationService;
use TheCodingMachine\GraphQLite\Fixtures\TestSelfType;


class SchemaFactoryTest extends TestCase
{

Expand Down Expand Up @@ -179,4 +179,31 @@ private function doTestSchema(Schema $schema): void
]
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
}

public function testDuplicateQueryException(): void
{
$factory = new SchemaFactory(
new Psr16Cache(new ArrayAdapter()),
new BasicAutoWiringContainer(
new EmptyContainer()
)
);
$factory->setAuthenticationService(new VoidAuthenticationService())
->setAuthorizationService(new VoidAuthorizationService())
->addControllerNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\DuplicateQueries')
->addTypeNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\Integration');

$this->expectException(DuplicateMappingException::class);
$schema = $factory->createSchema();
$queryString = '
query {
duplicateQuery
}
';
GraphQL::executeQuery(
$schema,
$queryString
);
}

}