44
55namespace Worksome \PestGraphqlCoverage ;
66
7+ use GraphQL \Language \AST \DirectiveNode ;
78use GraphQL \Language \AST \FieldDefinitionNode ;
89use GraphQL \Language \AST \InterfaceTypeDefinitionNode ;
910use 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