1- using System . Linq ;
1+ using System . Linq ;
2+ using GraphQL . Execution ;
23using GraphQL . Language . AST ;
34using GraphQL . Types ;
45using GraphQL . Validation ;
@@ -49,7 +50,7 @@ public INodeVisitor Validate(ValidationContext context)
4950 {
5051 var fieldDef = context . TypeInfo . GetFieldDef ( ) ;
5152
52- if ( fieldDef == null ) return ;
53+ if ( fieldDef == null || SkipAuthCheck ( fieldAst , context ) ) return ;
5354
5455 // check target field
5556 CheckAuth ( fieldAst , fieldDef , userContext , context , operationType ) ;
@@ -59,6 +60,37 @@ public INodeVisitor Validate(ValidationContext context)
5960 } ) ;
6061 }
6162
63+ private bool SkipAuthCheck ( Field fieldAst , ValidationContext context )
64+ {
65+ if ( fieldAst . Directives == null || ! fieldAst . Directives . Any ( ) ) return true ;
66+
67+ var includeField = GetDirectiveValue ( context , fieldAst . Directives , DirectiveGraphType . Include . Name ) ;
68+ if ( includeField . HasValue ) return ! includeField . Value ;
69+
70+ var skipField = GetDirectiveValue ( context , fieldAst . Directives , DirectiveGraphType . Skip . Name ) ;
71+ if ( skipField . HasValue ) return skipField . Value ;
72+
73+ return false ;
74+ }
75+
76+ private static bool ? GetDirectiveValue ( ValidationContext context , Directives directives , string directiveName )
77+ {
78+ var directive = directives . Find ( directiveName ) ;
79+ if ( directive == null ) return null ;
80+
81+ var operation = ! string . IsNullOrWhiteSpace ( context . OperationName )
82+ ? context . Document . Operations . WithName ( context . OperationName )
83+ : context . Document . Operations . FirstOrDefault ( ) ;
84+ var values = ExecutionHelper . GetArgumentValues (
85+ context . Schema ,
86+ DirectiveGraphType . Include . Arguments ,
87+ directive . Arguments ,
88+ ExecutionHelper . GetVariableValues ( context . Document , context . Schema , operation ? . Variables , context . Inputs ) ) ;
89+
90+ values . TryGetValue ( "if" , out object ifObj ) ;
91+ return bool . TryParse ( ifObj ? . ToString ( ) ?? string . Empty , out bool ifVal ) && ifVal ;
92+ }
93+
6294 private void CheckAuth (
6395 INode node ,
6496 IProvideMetadata type ,
0 commit comments