1+ using System . Collections . Generic ;
2+ using GraphQL . Builders ;
3+ using GraphQL . Types ;
4+ using GraphQLParser . AST ;
5+
6+ namespace Micro . Auth . Api . GraphQL . Federation
7+ {
8+ public static class Extensions
9+ {
10+ private static FieldBuilder < TSourceType , TReturnType > BuildAstMeta < TSourceType , TReturnType > ( FieldBuilder < TSourceType , TReturnType > fieldBuilder , string name , string value = null )
11+ {
12+ fieldBuilder . FieldType . BuildAstMeta ( name , value ) ;
13+ return fieldBuilder ;
14+ }
15+ public static FieldBuilder < TSourceType , TReturnType > Requires < TSourceType , TReturnType > ( this FieldBuilder < TSourceType , TReturnType > fieldBuilder , string fields ) => BuildAstMeta ( fieldBuilder , "requires" , fields ) ;
16+ public static FieldBuilder < TSourceType , TReturnType > Provides < TSourceType , TReturnType > ( this FieldBuilder < TSourceType , TReturnType > fieldBuilder , string fields ) => BuildAstMeta ( fieldBuilder , "provides" , fields ) ;
17+ public static FieldBuilder < TSourceType , TReturnType > External < TSourceType , TReturnType > ( this FieldBuilder < TSourceType , TReturnType > fieldBuilder ) => BuildAstMeta ( fieldBuilder , "external" ) ;
18+ public static void BuildAstMeta ( this IProvideMetadata type , string name , string value = null )
19+ {
20+ var definition = ( GraphQLObjectTypeDefinition ) type . GetMetadata < ASTNode > ( "__AST_MetaField__" , ( ) => BuildGraphQLObjectTypeDefinition ( ) ) ;
21+ var directive = BuildGraphQLDirective ( name , value ) ;
22+ AddDirective ( definition , directive ) ;
23+ //type.SetAstType(definition);
24+ type . Metadata [ "__AST_MetaField__" ] = definition ;
25+ }
26+ private static void AddDirective ( GraphQLObjectTypeDefinition definition , GraphQLDirective directive ) => ( ( List < GraphQLDirective > ) definition . Directives ) . Add ( directive ) ;
27+ private static GraphQLObjectTypeDefinition BuildGraphQLObjectTypeDefinition ( ) => new GraphQLObjectTypeDefinition
28+ {
29+ Directives = new List < GraphQLDirective > ( ) ,
30+ Location = new GraphQLLocation ( ) ,
31+ Fields = new List < GraphQLFieldDefinition > ( )
32+ } ;
33+ private static GraphQLDirective BuildGraphQLDirective ( string name , string value = null , ASTNodeKind kind = ASTNodeKind . StringValue ) => new GraphQLDirective
34+ {
35+ Name = new GraphQLName
36+ {
37+ Value = name ,
38+ Location = new GraphQLLocation ( )
39+ } ,
40+ Arguments = string . IsNullOrEmpty ( value ) ? new List < GraphQLArgument > ( ) : new List < GraphQLArgument > ( ) {
41+ new GraphQLArgument
42+ {
43+ Name = new GraphQLName {
44+ Value = "fields" ,
45+ Location = new GraphQLLocation ( )
46+ } ,
47+ Value = new GraphQLScalarValue ( kind ) {
48+ Value = value ,
49+ Location = new GraphQLLocation ( )
50+ } ,
51+ Location = new GraphQLLocation ( )
52+ }
53+ } ,
54+ Location = new GraphQLLocation ( )
55+ } ;
56+ }
57+ }
0 commit comments