@@ -131,6 +131,17 @@ func (v *VariablesSchemaBuilder) EnterVariableDefinition(ref int) {
131131 v .schema .Required = append (v .schema .Required , varName )
132132 }
133133
134+ // Set variable description: use the variable's own description if present (AC2),
135+ // otherwise fall back to the field argument description from the schema (AC3)
136+ if v .operationDocument .VariableDefinitions [ref ].Description .IsDefined {
137+ varSchema .Description = v .operationDocument .VariableDefinitionDescriptionString (ref )
138+ } else {
139+ // Fall back to argument description from schema definition
140+ if desc := v .findArgumentDescriptionForVariable (varName ); desc != "" {
141+ varSchema .Description = desc
142+ }
143+ }
144+
134145 // Set default value if exists
135146 if v .operationDocument .VariableDefinitionHasDefaultValue (ref ) {
136147 defaultValue := v .operationDocument .VariableDefinitionDefaultValue (ref )
@@ -150,6 +161,85 @@ func (v *VariablesSchemaBuilder) EnterVariableDefinition(ref int) {
150161 v .schema .Properties [varName ] = varSchema
151162}
152163
164+ // findArgumentDescriptionForVariable looks up the argument description from the schema definition
165+ // for a variable by matching it to field arguments in the operation's root selection set.
166+ func (v * VariablesSchemaBuilder ) findArgumentDescriptionForVariable (varName string ) string {
167+ if len (v .operationDocument .OperationDefinitions ) == 0 {
168+ return ""
169+ }
170+
171+ operationDef := v .operationDocument .OperationDefinitions [0 ]
172+ if ! operationDef .HasSelections {
173+ return ""
174+ }
175+
176+ // Determine root type name
177+ var rootTypeName string
178+ switch operationDef .OperationType {
179+ case ast .OperationTypeQuery :
180+ rootTypeName = "Query"
181+ case ast .OperationTypeMutation :
182+ rootTypeName = "Mutation"
183+ case ast .OperationTypeSubscription :
184+ rootTypeName = "Subscription"
185+ default :
186+ return ""
187+ }
188+
189+ rootType , exists := v .definitionDocument .Index .FirstNodeByNameStr (rootTypeName )
190+ if ! exists || rootType .Kind != ast .NodeKindObjectTypeDefinition {
191+ return ""
192+ }
193+
194+ // Iterate through root fields in the operation's selection set
195+ selectionSetRef := operationDef .SelectionSet
196+ for _ , selectionRef := range v .operationDocument .SelectionSets [selectionSetRef ].SelectionRefs {
197+ selection := v .operationDocument .Selections [selectionRef ]
198+ if selection .Kind != ast .SelectionKindField {
199+ continue
200+ }
201+
202+ fieldRef := selection .Ref
203+ // Check if this field has arguments referencing our variable
204+ if ! v .operationDocument .FieldHasArguments (fieldRef ) {
205+ continue
206+ }
207+
208+ for _ , argRef := range v .operationDocument .Fields [fieldRef ].Arguments .Refs {
209+ argValue := v .operationDocument .ArgumentValue (argRef )
210+ if argValue .Kind != ast .ValueKindVariable {
211+ continue
212+ }
213+ argVarName := v .operationDocument .VariableValueNameString (argValue .Ref )
214+ if argVarName != varName {
215+ continue
216+ }
217+
218+ // Found the argument that references this variable.
219+ // Look up the corresponding argument definition in the schema.
220+ argName := v .operationDocument .ArgumentNameString (argRef )
221+ fieldName := v .operationDocument .FieldNameString (fieldRef )
222+
223+ // Find this field in the root type definition
224+ for _ , fieldDefRef := range v .definitionDocument .ObjectTypeDefinitions [rootType .Ref ].FieldsDefinition .Refs {
225+ if v .definitionDocument .FieldDefinitionNameString (fieldDefRef ) != fieldName {
226+ continue
227+ }
228+ // Find the argument definition
229+ for _ , argDefRef := range v .definitionDocument .FieldDefinitionArgumentsDefinitions (fieldDefRef ) {
230+ if v .definitionDocument .InputValueDefinitionNameString (argDefRef ) == argName {
231+ if v .definitionDocument .InputValueDefinitions [argDefRef ].Description .IsDefined {
232+ return v .definitionDocument .InputValueDefinitionDescriptionString (argDefRef )
233+ }
234+ }
235+ }
236+ }
237+ }
238+ }
239+
240+ return ""
241+ }
242+
153243// GetSchema returns the built schema
154244func (v * VariablesSchemaBuilder ) GetSchema () * JsonSchema {
155245 // If we have required fields, the root schema cannot be nullable
0 commit comments