Skip to content

Commit d0b3002

Browse files
authored
Merge pull request #24 from moufmouf/failwith
Adding failWith method to Field
2 parents 029c6e7 + 2cf2487 commit d0b3002

File tree

3 files changed

+91
-20
lines changed

3 files changed

+91
-20
lines changed

src/Field.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class Field implements SourceFieldInterface
2020
* @var bool
2121
*/
2222
private $id;
23+
/**
24+
* @var mixed
25+
*/
26+
private $failWithValue;
27+
/**
28+
* @var bool
29+
*/
30+
private $hasFailWith = false;
2331

2432

2533
public function __construct(string $name, bool $isId = false)
@@ -52,9 +60,17 @@ public function isHidden(): bool
5260
return $this->hide;
5361
}
5462

55-
public function requiresRight(string $right)
63+
public function requiresRight(string $right): self
5664
{
5765
$this->right = $right;
66+
return $this;
67+
}
68+
69+
public function failWith($defaultValue): self
70+
{
71+
$this->failWithValue = $defaultValue;
72+
$this->hasFailWith = true;
73+
return $this;
5874
}
5975

6076
/**
@@ -92,11 +108,11 @@ public function isLogged(): bool
92108

93109
/**
94110
* Returns the GraphQL return type of the request (as a string).
95-
* The string can represent the FQCN of the type or an entry in the container resolving to the GraphQL type.
111+
* The string is the GraphQL output type name.
96112
*
97113
* @return string|null
98114
*/
99-
public function getReturnType(): ?string
115+
public function getOutputType(): ?string
100116
{
101117
return null;
102118
}
@@ -110,4 +126,24 @@ public function isId(): bool
110126
{
111127
return $this->id;
112128
}
129+
130+
/**
131+
* Returns the default value to use if the right is not enforced.
132+
*
133+
* @return mixed
134+
*/
135+
public function getFailWith()
136+
{
137+
return $this->failWithValue;
138+
}
139+
140+
/**
141+
* True if a default value is available if a right is not enforced.
142+
*
143+
* @return bool
144+
*/
145+
public function canFailWith()
146+
{
147+
return $this->hasFailWith;
148+
}
113149
}

tests/FieldTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,13 @@ public function testIsHidden()
2323

2424
$field->requiresRight('nope');
2525
}
26+
27+
public function testFailWith()
28+
{
29+
$field = new Field('test');
30+
$this->assertFalse($field->canFailWith());
31+
$field->failWith('foo');
32+
$this->assertTrue($field->canFailWith());
33+
$this->assertSame('foo', $field->getFailWith());
34+
}
2635
}

tests/GraphQLTypeGeneratorTest.php

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,25 @@
1717
use TheCodingMachine\GraphQL\Controllers\AnnotationReader;
1818
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
1919
use TheCodingMachine\GraphQL\Controllers\Containers\BasicAutoWiringContainer;
20-
use TheCodingMachine\GraphQL\Controllers\ControllerQueryProviderFactory;
21-
use TheCodingMachine\GraphQL\Controllers\HydratorInterface;
20+
use TheCodingMachine\GraphQL\Controllers\FieldsBuilderFactory;
21+
use TheCodingMachine\GraphQL\Controllers\Hydrators\FactoryHydrator;
22+
use TheCodingMachine\GraphQL\Controllers\Hydrators\HydratorInterface;
23+
use TheCodingMachine\GraphQL\Controllers\InputTypeGenerator;
24+
use TheCodingMachine\GraphQL\Controllers\InputTypeUtils;
2225
use TheCodingMachine\GraphQL\Controllers\Mappers\GlobTypeMapper;
2326
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapper;
2427
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface;
2528
use TheCodingMachine\GraphQL\Controllers\Mappers\TypeMapperInterface;
29+
use TheCodingMachine\GraphQL\Controllers\NamingStrategy;
30+
use TheCodingMachine\GraphQL\Controllers\Reflection\CachedDocBlockFactory;
2631
use TheCodingMachine\GraphQL\Controllers\Security\AuthenticationServiceInterface;
2732
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface;
2833
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService;
2934
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService;
3035
use TheCodingMachine\GraphQL\Controllers\TypeGenerator;
36+
use TheCodingMachine\GraphQL\Controllers\Types\TypeResolver;
3137
use TheCodingMachine\TDBM\Configuration;
3238
use TheCodingMachine\Tdbm\GraphQL\Registry\EmptyContainer;
33-
use TheCodingMachine\Tdbm\GraphQL\Registry\Registry;
3439
use TheCodingMachine\Tdbm\GraphQL\Tests\Beans\Country;
3540
use TheCodingMachine\Tdbm\GraphQL\Tests\Beans\User;
3641
use TheCodingMachine\Tdbm\GraphQL\Tests\DAOs\CountryDao;
@@ -59,13 +64,14 @@ class GraphQLTypeGeneratorTest extends TestCase
5964
public function setUp()
6065
{
6166
$this->mainContainer = new Picotainer([
62-
ControllerQueryProviderFactory::class => function (ContainerInterface $container) {
63-
return new ControllerQueryProviderFactory(
67+
FieldsBuilderFactory::class => function (ContainerInterface $container) {
68+
return new FieldsBuilderFactory(
6469
$container->get(AnnotationReader::class),
6570
$container->get(HydratorInterface::class),
6671
$container->get(AuthenticationServiceInterface::class),
6772
$container->get(AuthorizationServiceInterface::class),
68-
$container->get(BasicAutoWiringContainer::class)
73+
$container->get(TypeResolver::class),
74+
$container->get(CachedDocBlockFactory::class)
6975
);
7076
},
7177
BasicAutoWiringContainer::class => function (ContainerInterface $container) {
@@ -78,35 +84,55 @@ public function setUp()
7884
return new VoidAuthenticationService();
7985
},
8086
RecursiveTypeMapperInterface::class => function (ContainerInterface $container) {
81-
return new RecursiveTypeMapper($container->get(TypeMapperInterface::class));
87+
return new RecursiveTypeMapper($container->get(TypeMapperInterface::class), $container->get(NamingStrategyInterface::class), new \Symfony\Component\Cache\Simple\ArrayCache());
8288
},
8389
TypeMapperInterface::class => function (ContainerInterface $container) {
8490
return new GlobTypeMapper(
8591
'TheCodingMachine\\Tdbm\\GraphQL\\Tests\\GraphQL',
8692
$container->get(TypeGenerator::class),
93+
$container->get(InputTypeGenerator::class),
94+
$container->get(InputTypeUtils::class),
8795
$container->get(BasicAutoWiringContainer::class),
8896
$container->get(AnnotationReader::class),
89-
new NullCache()
97+
$container->get(NamingStrategyInterface::class),
98+
new \Symfony\Component\Cache\Simple\ArrayCache()
9099
);
91100
},
92101
TypeGenerator::class => function (ContainerInterface $container) {
93102
return new TypeGenerator(
94103
$container->get(AnnotationReader::class),
95-
$container->get(ControllerQueryProviderFactory::class)
104+
$container->get(FieldsBuilderFactory::class),
105+
$container->get(NamingStrategyInterface::class)
96106
);
97107
},
98108
AnnotationReader::class => function (ContainerInterface $container) {
99109
return new AnnotationReader(new DoctrineAnnotationReader());
100110
},
101111
HydratorInterface::class => function (ContainerInterface $container) {
102-
return new class implements HydratorInterface {
103-
public function hydrate(array $data, InputType $type)
104-
{
105-
throw new \RuntimeException('Not implemented');
106-
//return new Contact($data['name']);
107-
}
108-
};
109-
}
112+
return new FactoryHydrator();
113+
},
114+
InputTypeGenerator::class => function (ContainerInterface $container) {
115+
return new InputTypeGenerator(
116+
$container->get(InputTypeUtils::class),
117+
$container->get(FieldsBuilderFactory::class),
118+
$container->get(HydratorInterface::class)
119+
);
120+
},
121+
InputTypeUtils::class => function (ContainerInterface $container) {
122+
return new InputTypeUtils(
123+
$container->get(AnnotationReader::class),
124+
$container->get(NamingStrategyInterface::class)
125+
);
126+
},
127+
TypeResolver::class => function (ContainerInterface $container) {
128+
return new TypeResolver();
129+
},
130+
CachedDocBlockFactory::class => function () {
131+
return new CachedDocBlockFactory(new \Symfony\Component\Cache\Simple\ArrayCache());
132+
},
133+
NamingStrategyInterface::class => function () {
134+
return new NamingStrategy();
135+
},
110136
]);
111137
}
112138

0 commit comments

Comments
 (0)