Skip to content

Commit 28c3d59

Browse files
committed
fix: resolve incorrect deprecation location
1 parent 8805102 commit 28c3d59

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

src/Collector.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Worksome\PestGraphqlCoverage;
66

7-
use GraphQL\Language\AST\DirectiveNode;
87
use Nuwave\Lighthouse\Schema\Values\FieldValue;
98

109
class Collector
@@ -35,20 +34,13 @@ public static function parseResult(): array
3534

3635
public static function addResult(FieldValue $fieldValue): void
3736
{
38-
if (Config::shouldIgnoreDeprecatedFields() && self::isFieldDeprecated($fieldValue)) {
39-
return;
40-
}
37+
$fieldName = sprintf('%s.%s', $fieldValue->getParentName(), $fieldValue->getFieldName());
4138

4239
$filePath = self::filePath();
4340
$stream = fopen($filePath, 'a');
4441
assert(is_resource($stream));
4542

46-
fwrite($stream, sprintf(
47-
'%s.%s%s',
48-
$fieldValue->getParentName(),
49-
$fieldValue->getFieldName(),
50-
PHP_EOL
51-
));
43+
fwrite($stream, $fieldName . PHP_EOL);
5244

5345
fclose($stream);
5446
}
@@ -61,16 +53,4 @@ public static function filePath(): string
6153
'gql-coverage.php',
6254
]);
6355
}
64-
65-
private static function isFieldDeprecated(FieldValue $fieldValue): bool
66-
{
67-
foreach ($fieldValue->getField()->directives as $directive) {
68-
/** @var DirectiveNode $directive */
69-
if ($directive->name->value === 'deprecated') {
70-
return true;
71-
}
72-
}
73-
74-
return false;
75-
}
7656
}

src/Config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ final class Config
99
/** @var list<string> */
1010
private static array $ignoredNodes = [];
1111

12+
/** @var list<string> */
13+
private static array $deprecatedFields = [];
14+
1215
private static bool $ignorePaginatorInfo = false;
1316

1417
private static bool $ignoreDeprecatedFields = false;
@@ -52,6 +55,7 @@ public static function ignoredNodes(): array
5255
{
5356
return [
5457
...self::$ignoredNodes,
58+
...self::$deprecatedFields,
5559
...self::getPaginatorInfoNodes(),
5660
];
5761
}
@@ -62,6 +66,12 @@ public static function shouldIgnoreDeprecatedFields(): bool
6266
return self::$ignoreDeprecatedFields;
6367
}
6468

69+
/** @internal */
70+
public static function addDeprecatedField(string $deprecatedField): void
71+
{
72+
self::$deprecatedFields[] = $deprecatedField;
73+
}
74+
6575
/** @return list<string> */
6676
private static function getPaginatorInfoNodes(): array
6777
{

src/Plugin.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Worksome\PestGraphqlCoverage;
66

7+
use GraphQL\Language\AST\DirectiveNode;
78
use GraphQL\Language\AST\FieldDefinitionNode;
89
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
910
use GraphQL\Language\AST\NodeKind;
@@ -25,6 +26,8 @@ class Plugin implements AddsOutput, HandlesArguments
2526

2627
private const SERVER_VARIABLE_NAME = 'GQL_COVERAGE_ENABLED';
2728

29+
private int $numberOfDeprecatedFields = 0;
30+
2831
public int $maxUntestedFieldsCount = 10;
2932

3033
public string $schemaCommand = 'php artisan lighthouse:print-schema';
@@ -173,6 +176,10 @@ public function addOutput(int $exitCode): int
173176

174177
$style->writeln("GraphQL coverage: $percentage% ($totalTestedNodes/$totalNodes fields)");
175178

179+
if (Config::shouldIgnoreDeprecatedFields() && $this->numberOfDeprecatedFields > 0) {
180+
$style->writeln($this->numberOfDeprecatedFields . ' deprecated fields were ignored.');
181+
}
182+
176183
if ($untested !== []) {
177184
$style->newLine();
178185
$style->writeln("Untested fields (max. {$this->maxUntestedFieldsCount}): ");
@@ -215,10 +222,32 @@ private function collectAllNodesFromSchema(): array
215222
return;
216223
}
217224

225+
if (Config::shouldIgnoreDeprecatedFields() && self::isFieldDeprecated($node)) {
226+
Config::addDeprecatedField(
227+
sprintf('%s.%s', $parentType->name->value, $node->name->value)
228+
);
229+
230+
$this->numberOfDeprecatedFields++;
231+
232+
return;
233+
}
234+
218235
$nodes[$parentType->name->value][$node->name->value] = true;
219236
},
220237
]);
221238

222239
return $nodes;
223240
}
241+
242+
private static function isFieldDeprecated(FieldDefinitionNode $node): bool
243+
{
244+
foreach ($node->directives as $directive) {
245+
/** @var DirectiveNode $directive */
246+
if ($directive->name->value === 'deprecated') {
247+
return true;
248+
}
249+
}
250+
251+
return false;
252+
}
224253
}

0 commit comments

Comments
 (0)