private DocumentNode CreateDelegationQuery( OperationType operation, IImmutableStack <SelectionPathComponent> path, FieldNode requestedField) { if (!path.Any()) { path = path.Push(new SelectionPathComponent( requestedField.Name, Array.Empty <ArgumentNode>())); } FieldNode current = CreateRequestedField(requestedField, ref path); while (path.Any()) { path = path.Pop(out SelectionPathComponent component); current = CreateSelection(current, component); } var usedVariables = new HashSet <string>(); _usedVariables.CollectUsedVariables(current, usedVariables); _usedVariables.CollectUsedVariables(_fragments, usedVariables); var definitions = new List <IDefinitionNode>(); definitions.Add(CreateOperation( _operationName, operation, new List <FieldNode> { current }, _variables.Where(t => usedVariables.Contains(t.Variable.Name.Value)) .ToList())); definitions.AddRange(_fragments); return(new DocumentNode(null, definitions)); }