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
5 changes: 4 additions & 1 deletion src/Mappers/PorpaginasTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ private function getObjectType(OutputType $subType): ObjectType
if (!isset($args['limit']) && isset($args['offset'])) {
throw PorpaginasMissingParameterException::missingLimit();
}
return $root->take($args['offset'], $args['limit']);
if (isset($args['limit'])) {
return $root->take($args['offset'] ?? 0, $args['limit']);
}
return $root;
}
],
'count' => [
Expand Down
15 changes: 15 additions & 0 deletions src/Types/InvalidTypesInUnionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php


namespace TheCodingMachine\GraphQL\Controllers\Types;


use TheCodingMachine\GraphQL\Controllers\Mappers\CannotMapTypeExceptionInterface;

class InvalidTypesInUnionException extends \InvalidArgumentException implements CannotMapTypeExceptionInterface
{
public static function notObjectType(): self
{
throw new self('A Union type can only contain objects. Scalars, lists, etc... are not allowed.');
}
}
2 changes: 1 addition & 1 deletion src/Types/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(array $types, RecursiveTypeMapperInterface $typeMapp
foreach ($types as $type) {
$name .= $type->name;
if (!$type instanceof ObjectType) {
throw new \InvalidArgumentException('A Union type can only contain objects. Scalars, lists, etc... are not allowed.');
throw InvalidTypesInUnionException::notObjectType();
}
}
parent::__construct([
Expand Down
37 changes: 36 additions & 1 deletion tests/Integration/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,5 +330,40 @@ public function testEndToEndPorpaginas()
);

$this->assertSame('In the items field of a result set, you cannot add a "offset" without also adding a "limit"', $result->toArray(Debug::RETHROW_UNSAFE_EXCEPTIONS)['errors'][0]['message']);
}


// Let's run a query with no limit offset
$invalidQueryString = '
query {
getContactsIterator {
items {
name
... on User {
email
}
}
count
}
}
';

$result = GraphQL::executeQuery(
$schema,
$invalidQueryString
);

$this->assertSame([
'getContactsIterator' => [
'items' => [
[
'name' => 'Joe'
],
[
'name' => 'Bill',
'email' => 'bill@example.com'
]
],
'count' => 2
]
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']); }
}
7 changes: 7 additions & 0 deletions tests/Mappers/StaticTypeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TheCodingMachine\GraphQL\Controllers\Mappers;

use GraphQL\Type\Definition\StringType;
use GraphQL\Type\Definition\Type;
use PHPUnit\Framework\TestCase;
use TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest;
Expand Down Expand Up @@ -81,4 +82,10 @@ public function testException3(): void
$this->expectException(CannotMapTypeException::class);
$this->typeMapper->mapNameToType('notExists', $this->getTypeMapper());
}

public function testUnsupportedSubtypes(): void
{
$this->expectException(CannotMapTypeException::class);
$this->typeMapper->mapClassToType(TestObject::class, new StringType(), $this->getTypeMapper());
}
}