Refactor link expansion to avoid N+1 queries

This improved expansion of 100 items from c. 190ms to c. 30ms in
testing.