We have the Waeco (branded Mobicool) compressor portable fridge thing (i.e. this:
https://www.marinesuperstore.com/refrigeration/cool-boxes/waeco-fr35-portable-fridge-freezer which is really good, by the way) and I
think I'm right in saying that it automatically favouurs the mains input over the 12V input so in theory, you can leave both connected and it'll sort its self out in the best possible way - could be wrong though.
Alternatively, you need a relay - the coil of the relay needs to be connected to mains, and the 12V positive (or negative) needs to be connected to the normally closed contacts of the relay. That way, when the mains becomes present, it will disconnect the 12V. When the mains disappears, the 12V comes back in. It's obviously important to pick a suitably rated relay, and to somehow make it all safe! Important things to look at when picking the relay are coil voltage, contact current, and contact breaking current at 12V DC (which will likely be lower than the steady contact current rating of the relay. This looks like it's be a good option
http://www.rapidonline.com/finder-66-82-8-230-0000-230v-relay-dpdt-dc-30a-flange-mounting-66-82-60-4387 and has fast-on type spade connections, so will be easy to connect to. Annoyingly, the datasheet doesn't specify the DC load breaking current capacity of the normally closed (NC) contacts, as this is normally lower than the normally open contacts, but I'm expecting that your cooler draws around 4A, and I'd be fairly sure the contacts will handle that for the few hundred cycles it'll most likely see throughout it's life (and many more, most likely), as it'll only be switching 12V DC (voltage is a big factor when it comes to breaking current in any form of switch.
Simply mount this all in a plastic box project box with a couple of cable gland, and bob's your mother's brother. From a safety point of view, make sure that all cables are well restrained, and if there are any metal parts penetrating the plastic box, then you need to earth them.