Skip to content

Commit f79ade6

Browse files
authored
Merge pull request #205 from moufmouf/intputoutputconflictexception
Improving error message if an output and an input type name are in conflict
2 parents 4ce7e9d + 4d92fc3 commit f79ade6

File tree

7 files changed

+91
-13
lines changed

7 files changed

+91
-13
lines changed

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ New features:
1818
- Unauthorized access to fields can now generate GraphQL errors (rather that schema errors in GraphQLite v3)
1919
- Added fine-grained security using the `@Security` annotation. A field can now be [marked accessible or not depending on the context](fine-grained-security.md).
2020
For instance, you can restrict access to the field "viewsCount" of the type `BlogPost` only for post that the current user wrote.
21+
- You can now inject the current logged user in any query / mutation / field using the `@InjectUser` annotation
2122
- Performance:
2223
- You can inject the [Webonyx query plan in a parameter from a resolver](query_plan.md)
2324
- You can use the [dataloader pattern to improve performance drastically via the "prefetchMethod" attribute](prefetch_method.md)

src/Mappers/CannotMapTypeException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static function mustBeOutputType(string $subTypeName): self
8383

8484
public static function mustBeInputType(string $subTypeName): self
8585
{
86-
return new self('type "' . $subTypeName . '" must be an input type.');
86+
return new self('type "' . $subTypeName . '" must be an input type (if you declared an input type with the name "' . $subTypeName . '", make sure that there are no output type with the same name as this is forbidden by the GraphQL spec).');
8787
}
8888

8989
/**

src/TypeRegistry.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class TypeRegistry
2323
{
2424
/** @var array<string,NamedType&Type&(MutableObjectType|InterfaceType|UnionType|(InputObjectType&ResolvableMutableInputInterface))> */
25-
private $outputTypes = [];
25+
private $types = [];
2626

2727
/**
2828
* Registers a type.
@@ -33,10 +33,10 @@ class TypeRegistry
3333
*/
3434
public function registerType(NamedType $type): void
3535
{
36-
if (isset($this->outputTypes[$type->name])) {
36+
if (isset($this->types[$type->name])) {
3737
throw new GraphQLRuntimeException('Type "' . $type->name . '" is already registered');
3838
}
39-
$this->outputTypes[$type->name] = $type;
39+
$this->types[$type->name] = $type;
4040
}
4141

4242
/**
@@ -50,29 +50,29 @@ public function registerType(NamedType $type): void
5050
*/
5151
public function getOrRegisterType(NamedType $type): NamedType
5252
{
53-
if (isset($this->outputTypes[$type->name])) {
54-
return $this->outputTypes[$type->name];
53+
if (isset($this->types[$type->name])) {
54+
return $this->types[$type->name];
5555
}
56-
$this->outputTypes[$type->name] = $type;
56+
$this->types[$type->name] = $type;
5757

5858
return $type;
5959
}
6060

6161
public function hasType(string $typeName): bool
6262
{
63-
return isset($this->outputTypes[$typeName]);
63+
return isset($this->types[$typeName]);
6464
}
6565

6666
/**
6767
* @return NamedType&Type&(ObjectType|InterfaceType|UnionType|(InputObjectType&ResolvableMutableInputInterface))
6868
*/
6969
public function getType(string $typeName): NamedType
7070
{
71-
if (! isset($this->outputTypes[$typeName])) {
71+
if (! isset($this->types[$typeName])) {
7272
throw new GraphQLRuntimeException('Could not find type "' . $typeName . '" in registry');
7373
}
7474

75-
return $this->outputTypes[$typeName];
75+
return $this->types[$typeName];
7676
}
7777

7878
public function getMutableObjectType(string $typeName): MutableObjectType
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\GraphQLite\Fixtures\InputOutputNameConflict\Controllers;
5+
6+
7+
use TheCodingMachine\GraphQLite\Annotations\Query;
8+
use TheCodingMachine\GraphQLite\Annotations\UseInputType;
9+
use TheCodingMachine\GraphQLite\Fixtures\InputOutputNameConflict\Types\InAndOut;
10+
11+
class InAndOutController
12+
{
13+
/**
14+
* @Query()
15+
* @UseInputType(for="$inAndOut", inputType="InAndOut")
16+
*/
17+
public function testInAndOut(InAndOut $inAndOut): InAndOut
18+
{
19+
return $inAndOut;
20+
}
21+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\GraphQLite\Fixtures\InputOutputNameConflict\Types;
5+
6+
use TheCodingMachine\GraphQLite\Annotations\Factory;
7+
use TheCodingMachine\GraphQLite\Annotations\Field;
8+
use TheCodingMachine\GraphQLite\Annotations\Type;
9+
10+
/**
11+
* @Type(name="InAndOut")
12+
*/
13+
class InAndOut
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $value;
19+
20+
public function __construct(string $value)
21+
{
22+
$this->value = $value;
23+
}
24+
25+
/**
26+
* @Field()
27+
*/
28+
public function getValue(): string
29+
{
30+
return $this->value;
31+
}
32+
33+
/**
34+
* @Factory(name="InAndOut")
35+
*/
36+
public static function create(string $value): self
37+
{
38+
return new self($value);
39+
}
40+
}

tests/Fixtures/Integration/Types/ContactFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ContactFactory
1515
{
1616
/**
1717
* @Factory()
18-
* @UseInputType(for="$relations", inputType="[ContactRefInput!]!")
18+
* @UseInputType(for="$relations", inputType="[ContactRef!]!")
1919
* @param string $name
2020
* @param Contact|null $manager
2121
* @param Contact[] $relations
@@ -34,7 +34,7 @@ public function createContact(string $name, DateTimeInterface $birthDate, ?Uploa
3434
}
3535

3636
/**
37-
* @Factory(name="ContactRefInput", default=false)
37+
* @Factory(name="ContactRef", default=false)
3838
* @return Contact
3939
*/
4040
public function getContact(string $name): Contact

tests/Integration/EndToEndTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use TheCodingMachine\GraphQLite\GraphQLRuntimeException;
2222
use TheCodingMachine\GraphQLite\InputTypeGenerator;
2323
use TheCodingMachine\GraphQLite\InputTypeUtils;
24+
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
2425
use TheCodingMachine\GraphQLite\Mappers\CompositeTypeMapper;
2526
use TheCodingMachine\GraphQLite\Mappers\GlobTypeMapper;
2627
use TheCodingMachine\GraphQLite\Mappers\Parameters\ContainerParameterHandler;
@@ -52,6 +53,7 @@
5253
use TheCodingMachine\GraphQLite\Containers\EmptyContainer;
5354
use TheCodingMachine\GraphQLite\Reflection\CachedDocBlockFactory;
5455
use TheCodingMachine\GraphQLite\Schema;
56+
use TheCodingMachine\GraphQLite\SchemaFactory;
5557
use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface;
5658
use TheCodingMachine\GraphQLite\Security\AuthorizationServiceInterface;
5759
use TheCodingMachine\GraphQLite\Security\SecurityExpressionLanguageProvider;
@@ -451,7 +453,7 @@ public function testEndToEndInputType()
451453
{
452454
name: "bar"
453455
}
454-
]
456+
]
455457
}
456458
) {
457459
name,
@@ -1364,4 +1366,18 @@ public function getUser(): ?object
13641366

13651367
$this->assertSame(42, $result->toArray(Debug::RETHROW_UNSAFE_EXCEPTIONS)['data']['injectedUser']);
13661368
}
1369+
1370+
public function testInputOutputNameConflict(): void
1371+
{
1372+
$schemaFactory = new SchemaFactory(new Psr16Cache(new ArrayAdapter()), new BasicAutoWiringContainer(new EmptyContainer()));
1373+
$schemaFactory->addControllerNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\InputOutputNameConflict\\Controllers');
1374+
$schemaFactory->addTypeNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\InputOutputNameConflict\\Types');
1375+
1376+
$schema = $schemaFactory->createSchema();
1377+
1378+
$this->expectException(CannotMapTypeException::class);
1379+
$this->expectExceptionMessage('For parameter $inAndOut, in TheCodingMachine\\GraphQLite\\Fixtures\\InputOutputNameConflict\\Controllers\\InAndOutController::testInAndOut, type "InAndOut" must be an input type (if you declared an input type with the name "InAndOut", make sure that there are no output type with the same name as this is forbidden by the GraphQL spec).');
1380+
1381+
$schema->validate();
1382+
}
13671383
}

0 commit comments

Comments
 (0)