@@ -68,7 +68,7 @@ public GraphQLDocument(DocumentNode ast, IEnumerable<LocatedNamedSource> queryPa
6868 this . settings = settings ;
6969 List < object > items = ast . Definitions . Select ( this . Visit ) . ToList ( ) ;
7070 this . Operations = items . OfType < Operation > ( ) . ToList ( ) ;
71- this . types = items . OfType < IGraphQLType > ( ) . GroupBy ( x=> x . Name ) . Select ( x=> x . First ( ) ) . ToList ( ) ;
71+ this . types = items . OfType < IGraphQLType > ( ) . GroupBy ( x => x . Name ) . Select ( x => x . First ( ) ) . ToList ( ) ;
7272 this . astPrinter = new AstPrinter ( settings . TypeNameDirective ) ;
7373 foreach ( IGraphQLInitter i in items . OfType < IGraphQLInitter > ( ) . Where ( x => ! ( x is Operation ) ) )
7474 {
@@ -80,9 +80,45 @@ public GraphQLDocument(DocumentNode ast, IEnumerable<LocatedNamedSource> queryPa
8080 }
8181 }
8282
83- internal IValueNode < string > ResolveSpecifiedTypeName ( IEnumerable < DirectiveNode > directives )
83+ internal IValueNode < string > ResolveSpecifiedTypeName ( FieldNode field )
8484 {
85- var directive = directives . SingleOrDefault ( x => x . Name . Value == this . settings . TypeNameDirective ) ;
85+ ( LocatedNamedSource part , int offsetStart , int length ) = ResolveNode ( field . Location ) ;
86+
87+ var allTextBeforeError = part . Body . Substring ( 0 , offsetStart ) ;
88+ var lines = allTextBeforeError . Split ( '\n ' ) ;
89+ var line = lines . Count ( ) ;
90+
91+ for ( var i = line - 1 ; i > 0 ; i -- )
92+ {
93+ // walk back looking to comment lines with a type name flag on it
94+ var lineText = part . Line ( i ) ;
95+ if ( lineText == null )
96+ {
97+ break ;
98+ }
99+
100+ lineText = lineText . Trim ( ) ;
101+ if ( lineText . StartsWith ( "#!" ) )
102+ {
103+ lineText = lineText . Substring ( 2 ) ;
104+ var p = lineText . Trim ( ) . Split ( ':' ) . Select ( x => x . Trim ( ) ) . ToList ( ) ;
105+ if ( p [ 0 ] . Equals ( "typename" , StringComparison . OrdinalIgnoreCase ) && p . Count > 1 )
106+ {
107+ return new StringValueNode ( p [ 1 ] ) ;
108+ }
109+ }
110+ else if ( lineText . StartsWith ( "\" " ) )
111+ {
112+ // skip as this is a description line
113+ }
114+ else if ( lineText . Length != 0 )
115+ {
116+ // other lines stop looking as we have reached something else
117+ break ;
118+ }
119+ }
120+
121+ var directive = field . Directives . SingleOrDefault ( x => x . Name . Value == this . settings . TypeNameDirective ) ;
86122 var value = directive ? . Arguments . SingleOrDefault ( x => x . Name . Value == "type" ) ? . Value ;
87123 var scalar = value as IValueNode < string > ;
88124 return scalar ;
@@ -244,12 +280,12 @@ private void UnPackType(ITypeNode type, ValueTypeReference target)
244280 throw ;
245281 }
246282 }
247-
283+
248284 private object Visit ( ISyntaxNode node )
249285 {
250286 switch ( node )
251287 {
252-
288+
253289 case OperationDefinitionNode op :
254290 return new Operation ( op ) ;
255291 case InterfaceTypeDefinitionNode op :
0 commit comments