@@ -24,9 +24,10 @@ import {
2424 GraphQLScalarType ,
2525 GraphQLUnionType ,
2626 isInputType ,
27+ isNamedType ,
2728 isOutputType ,
2829} from '../type/definition' ;
29- import { GraphQLDirective } from '../type/directives' ;
30+ import { GraphQLDirective , specifiedDirectives } from '../type/directives' ;
3031import { introspectionTypes , TypeKind } from '../type/introspection' ;
3132import { specifiedScalarTypes } from '../type/scalars' ;
3233import type { GraphQLSchemaValidationOptions } from '../type/schema' ;
@@ -381,6 +382,66 @@ export function buildClientSchema(
381382 } ;
382383 }
383384
385+ function getSpecifiedDirectiveFromIntrospection (
386+ directiveIntrospection : IntrospectionDirective ,
387+ ) : GraphQLDirective | undefined {
388+ const possibleSpecifiedDirective = specifiedDirectives . find (
389+ ( dir ) => dir . name === directiveIntrospection . name ,
390+ ) ;
391+ if ( possibleSpecifiedDirective == null ) {
392+ return ;
393+ }
394+
395+ for ( const location of directiveIntrospection . locations ) {
396+ if ( ! possibleSpecifiedDirective . locations . includes ( location ) ) {
397+ return ;
398+ }
399+ }
400+
401+ for ( const arg of directiveIntrospection . args ) {
402+ const possibleArg = possibleSpecifiedDirective . args . find (
403+ ( a ) => a . name === arg . name ,
404+ ) ;
405+ if ( possibleArg == null ) {
406+ return ;
407+ }
408+ const argType = getType ( arg . type ) ;
409+ // Is same type
410+ let currentType = argType ;
411+ let expectedType = possibleArg . type ;
412+ // eslint-disable-next-line no-constant-condition
413+ while ( true ) {
414+ if ( currentType instanceof GraphQLNonNull ) {
415+ if ( expectedType instanceof GraphQLNonNull ) {
416+ currentType = currentType . ofType ;
417+ expectedType = expectedType . ofType ;
418+ continue ;
419+ } else {
420+ return ;
421+ }
422+ }
423+ if ( currentType instanceof GraphQLList ) {
424+ if ( expectedType instanceof GraphQLList ) {
425+ currentType = currentType . ofType ;
426+ expectedType = expectedType . ofType ;
427+ continue ;
428+ } else {
429+ return ;
430+ }
431+ }
432+ if ( ! isNamedType ( currentType ) || ! isNamedType ( expectedType ) ) {
433+ return ;
434+ }
435+ if ( currentType !== expectedType ) {
436+ return ;
437+ }
438+ break ;
439+ }
440+ }
441+
442+ return possibleSpecifiedDirective ;
443+ }
444+
384445 function buildDirective (
385446 directiveIntrospection : IntrospectionDirective ,
386447 ) : GraphQLDirective {
@@ -396,6 +457,12 @@ export function buildClientSchema(
396457 `Introspection result missing directive locations: ${ directiveIntrospectionStr } .` ,
397458 ) ;
398459 }
460+ const specifiedDirective = getSpecifiedDirectiveFromIntrospection (
461+ directiveIntrospection ,
462+ ) ;
463+ if ( specifiedDirective != null ) {
464+ return specifiedDirective ;
465+ }
399466 return new GraphQLDirective ( {
400467 name : directiveIntrospection . name ,
401468 description : directiveIntrospection . description ,
0 commit comments