WASHINGTON — It was just after President Trump had finished railing in the Oval Office against African immigrants he said came from “shithole countries” when a senior Republican senator, Lindsey Graham of South Carolina, who was there to negotiate a deal on immigration, spoke up.

“America is an idea, not a race,” Mr. Graham said, according to three people familiar with the exchange on Thursday. Diversity was a strength, he said, not a weakness. And by the way, the senator added, he himself was a descendant of immigrants who came to the United States from “shithole countries with no skills.”

Mr. Trump’s racially charged comments in front of more than half a dozen lawmakers, which also extended to immigrants from Haiti — followed by a day in which members of Congress denounced the president, defended him or stayed silent — now threaten what had been an emerging agreement to protect undocumented immigrants brought to the United States as children.

Several people with knowledge of the conversation said the president had also demanded to know whether Haitian immigrants could be left out of any deal. The White House has not disputed the account of the exchange.