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
14 changes: 6 additions & 8 deletions src/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,15 @@ class FieldsBuilder
* @var TypeResolver
*/
private $typeResolver;

/**
* @param AnnotationReader $annotationReader
* @param RecursiveTypeMapperInterface $typeMapper
* @param HydratorInterface $hydrator
* @param AuthenticationServiceInterface $authenticationService
* @param AuthorizationServiceInterface $authorizationService
* @var NamingStrategyInterface
*/
private $namingStrategy;

public function __construct(AnnotationReader $annotationReader, RecursiveTypeMapperInterface $typeMapper,
HydratorInterface $hydrator, AuthenticationServiceInterface $authenticationService,
AuthorizationServiceInterface $authorizationService, TypeResolver $typeResolver,
CachedDocBlockFactory $cachedDocBlockFactory)
CachedDocBlockFactory $cachedDocBlockFactory, NamingStrategyInterface $namingStrategy)
{
$this->annotationReader = $annotationReader;
$this->typeMapper = $typeMapper;
Expand All @@ -102,6 +99,7 @@ public function __construct(AnnotationReader $annotationReader, RecursiveTypeMap
$this->authorizationService = $authorizationService;
$this->typeResolver = $typeResolver;
$this->cachedDocBlockFactory = $cachedDocBlockFactory;
$this->namingStrategy = $namingStrategy;
}

// TODO: Add RecursiveTypeMapper in the list of parameters for getQueries and REMOVE the ControllerQueryProviderFactory.
Expand Down Expand Up @@ -217,7 +215,7 @@ private function getFieldsByAnnotations($controller, string $annotationName, boo
$docBlockComment = $docBlockObj->getSummary()."\n".$docBlockObj->getDescription()->render();

$methodName = $refMethod->getName();
$name = $queryAnnotation->getName() ?: $methodName;
$name = $queryAnnotation->getName() ?: $this->namingStrategy->getFieldNameFromMethodName($methodName);

$parameters = $refMethod->getParameters();
if ($injectSource === true) {
Expand Down
10 changes: 8 additions & 2 deletions src/FieldsBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,23 @@ class FieldsBuilderFactory
* @var TypeResolver
*/
private $typeResolver;
/**
* @var NamingStrategyInterface
*/
private $namingStrategy;

public function __construct(AnnotationReader $annotationReader,
HydratorInterface $hydrator, AuthenticationServiceInterface $authenticationService,
AuthorizationServiceInterface $authorizationService, TypeResolver $typeResolver,
CachedDocBlockFactory $cachedDocBlockFactory)
CachedDocBlockFactory $cachedDocBlockFactory, NamingStrategyInterface $namingStrategy)
{
$this->annotationReader = $annotationReader;
$this->hydrator = $hydrator;
$this->authenticationService = $authenticationService;
$this->authorizationService = $authorizationService;
$this->typeResolver = $typeResolver;
$this->cachedDocBlockFactory = $cachedDocBlockFactory;
$this->namingStrategy = $namingStrategy;
}

/**
Expand All @@ -64,7 +69,8 @@ public function buildFieldsBuilder(RecursiveTypeMapperInterface $typeMapper): Fi
$this->authenticationService,
$this->authorizationService,
$this->typeResolver,
$this->cachedDocBlockFactory
$this->cachedDocBlockFactory,
$this->namingStrategy
);
}
}
19 changes: 19 additions & 0 deletions src/NamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
namespace TheCodingMachine\GraphQL\Controllers;


use function lcfirst;
use function strlen;
use function strpos;
use function substr;
use TheCodingMachine\GraphQL\Controllers\Annotations\Factory;
use TheCodingMachine\GraphQL\Controllers\Annotations\Type;

Expand Down Expand Up @@ -49,4 +53,19 @@ public function getInputTypeName(string $className, Factory $factory): string
}
return $className.'Input';
}

/**
* Returns the name of a GraphQL field from the name of the annotated method.
*/
public function getFieldNameFromMethodName(string $methodName): string
{
// Let's remove any "get" or "is".
if (strpos($methodName, 'get') === 0 && strlen($methodName) > 3) {
return lcfirst(substr($methodName, 3));
}
if (strpos($methodName, 'is') === 0 && strlen($methodName) > 2) {
return lcfirst(substr($methodName, 2));
}
return $methodName;
}
}
5 changes: 5 additions & 0 deletions src/NamingStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public function getInterfaceNameFromConcreteName(string $concreteType): string;
public function getOutputTypeName(string $typeClassName, Type $type): string;

public function getInputTypeName(string $className, Factory $factory): string;

/**
* Returns the name of a GraphQL field from the name of the annotated method.
*/
public function getFieldNameFromMethodName(string $methodName): string;
}
6 changes: 4 additions & 2 deletions tests/AbstractQueryProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ protected function buildFieldsBuilder(): FieldsBuilder
new VoidAuthenticationService(),
new VoidAuthorizationService(),
$this->getTypeResolver(),
new CachedDocBlockFactory(new ArrayCache())
new CachedDocBlockFactory(new ArrayCache()),
new NamingStrategy()
);
}

Expand Down Expand Up @@ -304,7 +305,8 @@ protected function getControllerQueryProviderFactory(): FieldsBuilderFactory
new VoidAuthenticationService(),
new VoidAuthorizationService(),
$this->getTypeResolver(),
new CachedDocBlockFactory(new ArrayCache()));
new CachedDocBlockFactory(new ArrayCache()),
new NamingStrategy());
}
return $this->controllerQueryProviderFactory;
}
Expand Down
9 changes: 6 additions & 3 deletions tests/FieldsBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ public function isLogged(): bool
},
new VoidAuthorizationService(),
$this->getTypeResolver(),
new CachedDocBlockFactory(new ArrayCache())
new CachedDocBlockFactory(new ArrayCache()),
new NamingStrategy()
);

$fields = $queryProvider->getFields(new TestType(), true);
Expand All @@ -219,7 +220,8 @@ public function isAllowed(string $right): bool
}
},
$this->getTypeResolver(),
new CachedDocBlockFactory(new ArrayCache())
new CachedDocBlockFactory(new ArrayCache()),
new NamingStrategy()
);

$fields = $queryProvider->getFields(new TestType(), true);
Expand Down Expand Up @@ -275,7 +277,8 @@ public function testFromSourceFieldsInterface()
new VoidAuthenticationService(),
new VoidAuthorizationService(),
$this->getTypeResolver(),
new CachedDocBlockFactory(new ArrayCache())
new CachedDocBlockFactory(new ArrayCache()),
new NamingStrategy()
);
$fields = $queryProvider->getFields(new TestTypeWithSourceFieldInterface(), true);
$this->assertCount(1, $fields);
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Integration/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getName(): string
}

/**
* @Field(name="price")
* @Field()
* @return float
*/
public function getPrice(): float
Expand Down
29 changes: 15 additions & 14 deletions tests/Integration/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public function setUp()
$container->get(AuthenticationServiceInterface::class),
$container->get(AuthorizationServiceInterface::class),
$container->get(TypeResolver::class),
$container->get(CachedDocBlockFactory::class)
$container->get(CachedDocBlockFactory::class),
$container->get(NamingStrategyInterface::class)
);
},
TypeResolver::class => function(ContainerInterface $container) {
Expand Down Expand Up @@ -179,7 +180,7 @@ public function testEndToEnd()

$queryString = '
query {
getContacts {
contacts {
name
uppercaseName
... on User {
Expand All @@ -195,7 +196,7 @@ public function testEndToEnd()
);

$this->assertSame([
'getContacts' => [
'contacts' => [
[
'name' => 'Joe',
'uppercaseName' => 'JOE'
Expand All @@ -216,7 +217,7 @@ public function testEndToEnd()
);

$this->assertSame([
'getContacts' => [
'contacts' => [
[
'name' => 'Joe',
'uppercaseName' => 'JOE'
Expand Down Expand Up @@ -289,7 +290,7 @@ public function testEndToEndPorpaginas()

$queryString = '
query {
getContactsIterator {
contactsIterator {
items(limit: 1, offset: 1) {
name
uppercaseName
Expand All @@ -308,7 +309,7 @@ public function testEndToEndPorpaginas()
);

$this->assertSame([
'getContactsIterator' => [
'contactsIterator' => [
'items' => [
[
'name' => 'Bill',
Expand All @@ -327,7 +328,7 @@ public function testEndToEndPorpaginas()
);

$this->assertSame([
'getContactsIterator' => [
'contactsIterator' => [
'items' => [
[
'name' => 'Bill',
Expand All @@ -342,7 +343,7 @@ public function testEndToEndPorpaginas()
// Let's run a query with no limit but an offset
$invalidQueryString = '
query {
getContactsIterator {
contactsIterator {
items(offset: 1) {
name
... on User {
Expand All @@ -365,7 +366,7 @@ public function testEndToEndPorpaginas()
// Let's run a query with no limit offset
$invalidQueryString = '
query {
getContactsIterator {
contactsIterator {
items {
name
... on User {
Expand All @@ -383,7 +384,7 @@ public function testEndToEndPorpaginas()
);

$this->assertSame([
'getContactsIterator' => [
'contactsIterator' => [
'items' => [
[
'name' => 'Joe',
Expand All @@ -410,7 +411,7 @@ public function testEndToEnd2Iterators()

$queryString = '
query {
getContactsIterator {
contactsIterator {
items(limit: 1, offset: 1) {
name
uppercaseName
Expand All @@ -421,7 +422,7 @@ public function testEndToEnd2Iterators()
count
}

getProducts {
products {
items {
name
price
Expand All @@ -437,7 +438,7 @@ public function testEndToEnd2Iterators()
);

$this->assertSame([
'getContactsIterator' => [
'contactsIterator' => [
'items' => [
[
'name' => 'Bill',
Expand All @@ -447,7 +448,7 @@ public function testEndToEnd2Iterators()
],
'count' => 2
],
'getProducts' => [
'products' => [
'items' => [
[
'name' => 'Foo',
Expand Down
11 changes: 11 additions & 0 deletions tests/NamingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@ public function testGetInputTypeName(): void
$factory = new Factory(['name'=>'MyInputType']);
$this->assertSame('MyInputType', $namingStrategy->getInputTypeName('Bar\\FooClass', $factory));
}

public function testGetFieldNameFromMethodName(): void
{
$namingStrategy = new NamingStrategy();

$this->assertSame('name', $namingStrategy->getFieldNameFromMethodName('getName'));
$this->assertSame('get', $namingStrategy->getFieldNameFromMethodName('get'));
$this->assertSame('name', $namingStrategy->getFieldNameFromMethodName('isName'));
$this->assertSame('is', $namingStrategy->getFieldNameFromMethodName('is'));
$this->assertSame('foo', $namingStrategy->getFieldNameFromMethodName('foo'));
}
}