Skip to content

Commit 391b65a

Browse files
authored
Merge pull request #12 from moufmouf/query_descriptions
Adding descriptions for queries and mutations
2 parents 065c690 + 8493c2d commit 391b65a

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

src/ControllerQueryProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use TheCodingMachine\GraphQL\Controllers\Annotations\Mutation;
1919
use TheCodingMachine\GraphQL\Controllers\Annotations\Query;
2020
use TheCodingMachine\GraphQL\Controllers\Annotations\Right;
21+
use TheCodingMachine\GraphQL\Controllers\Reflection\CommentParser;
2122
use TheCodingMachine\GraphQL\Controllers\Security\AuthenticationServiceInterface;
2223
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface;
2324
use Youshido\GraphQL\Field\Field;
@@ -107,6 +108,7 @@ private function getFieldsByAnnotations(string $annotationName): array
107108
$queryAnnotation = $this->annotationReader->getMethodAnnotation($standardPhpMethod, $annotationName);
108109

109110
if ($queryAnnotation !== null) {
111+
$docBlock = new CommentParser($refMethod->getDocComment());
110112
if (!$this->isAuthorized($standardPhpMethod)) {
111113
continue;
112114
}
@@ -122,7 +124,7 @@ private function getFieldsByAnnotations(string $annotationName): array
122124
} catch (TypeMappingException $e) {
123125
throw TypeMappingException::wrapWithReturnInfo($e, $refMethod);
124126
}
125-
$queryList[] = new QueryField($methodName, $type, $args, [$this->controller, $methodName], $this->hydrator);
127+
$queryList[] = new QueryField($methodName, $type, $args, [$this->controller, $methodName], $this->hydrator, $docBlock->getComment());
126128
}
127129
}
128130

src/QueryField.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ class QueryField extends AbstractField
2929
* @param callable $resolve
3030
* @param array $additionalConfig
3131
*/
32-
public function __construct(string $name, TypeInterface $type, array $arguments, callable $resolve, HydratorInterface $hydrator, array $additionalConfig = [])
32+
public function __construct(string $name, TypeInterface $type, array $arguments, callable $resolve, HydratorInterface $hydrator, ?string $comment, array $additionalConfig = [])
3333
{
3434
$this->hydrator = $hydrator;
3535
$config = [
3636
'name' => $name,
3737
'type' => $type,
3838
'args' => array_map(function(array $item) { return $item['type']; }, $arguments)
3939
];
40+
if ($comment) {
41+
$config['description'] = $comment;
42+
}
4043

4144
$config['resolve'] = function ($source, array $args, ResolveInfo $info) use ($resolve, $arguments) {
4245
$toPassArgs = [];

src/Reflection/CommentParser.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\GraphQL\Controllers\Reflection;
5+
6+
7+
class CommentParser
8+
{
9+
private $comment;
10+
11+
public function __construct(string $docBlock)
12+
{
13+
$lines = $this->getAllLinesWithoutStars($docBlock);
14+
15+
// First, let's go to the first Annotation.
16+
// Anything before is the pure comment.
17+
18+
$oneAnnotationFound = false;
19+
// Is the line an annotation? Let's test this with a regexp.
20+
foreach ($lines as $line) {
21+
if ($oneAnnotationFound === false && preg_match("/^[@][a-zA-Z]/", $line) === 0) {
22+
$this->comment .= $line."\n";
23+
} else {
24+
//$this->parseAnnotation($line);
25+
$oneAnnotationFound = true;
26+
}
27+
}
28+
29+
$this->comment = rtrim($this->comment, "\n");
30+
}
31+
32+
/**
33+
* @param string $docComment
34+
* @return string[]
35+
*/
36+
private function getAllLinesWithoutStars(string $docComment): array
37+
{
38+
if (strpos($docComment, '/**') === 0) {
39+
$docComment = substr($docComment, 3);
40+
}
41+
42+
// Let's remove all the \r...
43+
$docComment = str_replace("\r", '', $docComment);
44+
45+
$commentLines = explode("\n", $docComment);
46+
$commentLinesWithoutStars = array_map(function(string $line) {
47+
return ltrim($line, " \t*");
48+
}, $commentLines);
49+
50+
// Let's remove the trailing /:
51+
$lastComment = $commentLinesWithoutStars[count($commentLinesWithoutStars)-1];
52+
$commentLinesWithoutStars[count($commentLinesWithoutStars)-1] = substr($lastComment, 0, strlen($lastComment)-1);
53+
54+
if ($commentLinesWithoutStars[count($commentLinesWithoutStars)-1] == "") {
55+
array_pop($commentLinesWithoutStars);
56+
}
57+
58+
if (isset($commentLinesWithoutStars[0]) && $commentLinesWithoutStars[0] === '') {
59+
$commentLinesWithoutStars = array_slice($commentLinesWithoutStars, 1);
60+
}
61+
return $commentLinesWithoutStars;
62+
}
63+
64+
/**
65+
* Returns the comment out of a Docblock, ignoring all annotations
66+
*
67+
* @return string
68+
*/
69+
public function getComment(): string
70+
{
71+
return $this->comment;
72+
}
73+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace TheCodingMachine\GraphQL\Controllers\Reflection;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class CommentParserTest extends TestCase
8+
{
9+
public function testParse()
10+
{
11+
$commentParser = new CommentParser(<<<EOF
12+
/**
13+
* Foo
14+
* Bar
15+
*
16+
* @param string \$id yop
17+
*/
18+
EOF
19+
);
20+
21+
$this->assertSame("Foo\nBar", $commentParser->getComment());
22+
}
23+
}

0 commit comments

Comments
 (0)