@@ -910,10 +910,13 @@ function parseScalarTypeDefinition()
910910 $ name = $ this ->parseName ();
911911 $ directives = $ this ->parseDirectives ();
912912
913+ $ description = $ this ->getDescriptionFromAdjacentCommentTokens ($ start );
914+
913915 return new ScalarTypeDefinitionNode ([
914916 'name ' => $ name ,
915917 'directives ' => $ directives ,
916- 'loc ' => $ this ->loc ($ start )
918+ 'loc ' => $ this ->loc ($ start ),
919+ 'description ' => $ description
917920 ]);
918921 }
919922
@@ -935,12 +938,15 @@ function parseObjectTypeDefinition()
935938 Token::BRACE_R
936939 );
937940
941+ $ description = $ this ->getDescriptionFromAdjacentCommentTokens ($ start );
942+
938943 return new ObjectTypeDefinitionNode ([
939944 'name ' => $ name ,
940945 'interfaces ' => $ interfaces ,
941946 'directives ' => $ directives ,
942947 'fields ' => $ fields ,
943- 'loc ' => $ this ->loc ($ start )
948+ 'loc ' => $ this ->loc ($ start ),
949+ 'description ' => $ description
944950 ]);
945951 }
946952
@@ -972,12 +978,15 @@ function parseFieldDefinition()
972978 $ type = $ this ->parseTypeReference ();
973979 $ directives = $ this ->parseDirectives ();
974980
981+ $ description = $ this ->getDescriptionFromAdjacentCommentTokens ($ start );
982+
975983 return new FieldDefinitionNode ([
976984 'name ' => $ name ,
977985 'arguments ' => $ args ,
978986 'type ' => $ type ,
979987 'directives ' => $ directives ,
980- 'loc ' => $ this ->loc ($ start )
988+ 'loc ' => $ this ->loc ($ start ),
989+ 'description ' => $ description
981990 ]);
982991 }
983992
@@ -1007,12 +1016,14 @@ function parseInputValueDef()
10071016 $ defaultValue = $ this ->parseConstValue ();
10081017 }
10091018 $ directives = $ this ->parseDirectives ();
1019+ $ description = $ this ->getDescriptionFromAdjacentCommentTokens ($ start );
10101020 return new InputValueDefinitionNode ([
10111021 'name ' => $ name ,
10121022 'type ' => $ type ,
10131023 'defaultValue ' => $ defaultValue ,
10141024 'directives ' => $ directives ,
1015- 'loc ' => $ this ->loc ($ start )
1025+ 'loc ' => $ this ->loc ($ start ),
1026+ 'description ' => $ description
10161027 ]);
10171028 }
10181029
@@ -1032,11 +1043,14 @@ function parseInterfaceTypeDefinition()
10321043 Token::BRACE_R
10331044 );
10341045
1046+ $ description = $ this ->getDescriptionFromAdjacentCommentTokens ($ start );
1047+
10351048 return new InterfaceTypeDefinitionNode ([
10361049 'name ' => $ name ,
10371050 'directives ' => $ directives ,
10381051 'fields ' => $ fields ,
1039- 'loc ' => $ this ->loc ($ start )
1052+ 'loc ' => $ this ->loc ($ start ),
1053+ 'description ' => $ description
10401054 ]);
10411055 }
10421056
@@ -1053,11 +1067,14 @@ function parseUnionTypeDefinition()
10531067 $ this ->expect (Token::EQUALS );
10541068 $ types = $ this ->parseUnionMembers ();
10551069
1070+ $ description = $ this ->getDescriptionFromAdjacentCommentTokens ($ start );
1071+
10561072 return new UnionTypeDefinitionNode ([
10571073 'name ' => $ name ,
10581074 'directives ' => $ directives ,
10591075 'types ' => $ types ,
1060- 'loc ' => $ this ->loc ($ start )
1076+ 'loc ' => $ this ->loc ($ start ),
1077+ 'description ' => $ description
10611078 ]);
10621079 }
10631080
@@ -1093,11 +1110,14 @@ function parseEnumTypeDefinition()
10931110 Token::BRACE_R
10941111 );
10951112
1113+ $ description = $ this ->getDescriptionFromAdjacentCommentTokens ($ start );
1114+
10961115 return new EnumTypeDefinitionNode ([
10971116 'name ' => $ name ,
10981117 'directives ' => $ directives ,
10991118 'values ' => $ values ,
1100- 'loc ' => $ this ->loc ($ start )
1119+ 'loc ' => $ this ->loc ($ start ),
1120+ 'description ' => $ description
11011121 ]);
11021122 }
11031123
@@ -1110,10 +1130,13 @@ function parseEnumValueDefinition()
11101130 $ name = $ this ->parseName ();
11111131 $ directives = $ this ->parseDirectives ();
11121132
1133+ $ description = $ this ->getDescriptionFromAdjacentCommentTokens ($ start );
1134+
11131135 return new EnumValueDefinitionNode ([
11141136 'name ' => $ name ,
11151137 'directives ' => $ directives ,
1116- 'loc ' => $ this ->loc ($ start )
1138+ 'loc ' => $ this ->loc ($ start ),
1139+ 'description ' => $ description
11171140 ]);
11181141 }
11191142
@@ -1133,11 +1156,14 @@ function parseInputObjectTypeDefinition()
11331156 Token::BRACE_R
11341157 );
11351158
1159+ $ description = $ this ->getDescriptionFromAdjacentCommentTokens ($ start );
1160+
11361161 return new InputObjectTypeDefinitionNode ([
11371162 'name ' => $ name ,
11381163 'directives ' => $ directives ,
11391164 'fields ' => $ fields ,
1140- 'loc ' => $ this ->loc ($ start )
1165+ 'loc ' => $ this ->loc ($ start ),
1166+ 'description ' => $ description
11411167 ]);
11421168 }
11431169
@@ -1193,4 +1219,28 @@ function parseDirectiveLocations()
11931219 } while ($ this ->skip (Token::PIPE ));
11941220 return $ locations ;
11951221 }
1222+
1223+ /**
1224+ * @param Token $nameToken
1225+ * @return null|string
1226+ */
1227+ private function getDescriptionFromAdjacentCommentTokens (Token $ nameToken )
1228+ {
1229+ $ description = null ;
1230+
1231+ $ currentToken = $ nameToken ;
1232+ $ previousToken = $ currentToken ->prev ;
1233+
1234+ while ($ previousToken ->kind == Token::COMMENT
1235+ && ($ previousToken ->line + 1 ) == $ currentToken ->line
1236+ ) {
1237+ $ description = $ previousToken ->value . $ description ;
1238+
1239+ // walk the tokens backwards until no longer adjacent comments
1240+ $ currentToken = $ previousToken ;
1241+ $ previousToken = $ currentToken ->prev ;
1242+ }
1243+
1244+ return $ description ;
1245+ }
11961246}
0 commit comments