We have $$W[J]\equiv\hbar i\log(Z[J])$$

How do we use it to compute the connected diagrams to some order in perturbative field theory?

I get that we need to take functional derivatives of it but I am not sure how this will give me the required diagrams (if at all it is expected to). I have gone through the verification of the claim that: it leaves only connected diagrams to first and second order in interaction parameter in $\phi^4$ theory and find it alright.

But suppose I want to extend it to some higher order and other similar theories like phi cubed theory. How to go ahead? Any references are appreciated which has some examples to start with using the above functional.