  We show that the membership problem in a finitely generated submonoid of a
graph group (also called a right-angled Artin group or a free partially
commutative group) is decidable if and only if the independence graph
(commutation graph) is a transitive forest. As a consequence we obtain the
first example of a finitely presented group with a decidable generalized word
problem that does not have a decidable membership problem for finitely
generated submonoids. We also show that the rational subset membership problem
is decidable for a graph group if and only if the independence graph is a
transitive forest, answering a question of Kambites, Silva, and the second
author. Finally we prove that for certain amalgamated free products and
HNN-extensions the rational subset and submonoid membership problems are
recursively equivalent. In particular, this applies to finitely generated
groups with two or more ends that are either torsion-free or residually finite.
