Sen. Lindsey O. Graham (R-S.C.) speaks during a Senate Judiciary Committee confirmation hearing for Judge Brett M. Kavanaugh on Tuesday. (Andrew Harrer/Bloomberg News)

Sen. Lindsey O. Graham’s longtime ally John McCain was widely mourned last week as a vital counterweight to President Trump, one of the few Republicans willing to take the president to task for cozying up to dictators, launching personal attacks and undermining the rule of law.

But Graham, McCain’s loyal sidekick for years, is now charting a markedly different path in the wake of his friend’s death — emerging as one of the strongest voices in defense of a president he once derided as a “kook” and “unfit for office.”

In a matter of days, Graham (S.C.) brokered invitations to McCain’s funeral for the president’s daughter and son-in-law with a family that didn’t want Trump himself there. He appeared on “Fox & Friends,” one of Trump’s favorite television shows, to echo the president’s attacks on the ongoing special counsel investigation of possible coordination between Russia and Trump’s campaign in the 2016 election.

And on Wednesday, Graham took to Twitter to minimize the impact of Bob Woodward’s blistering new book on the president while lavishing praise on Trump’s achievements, including his Supreme Court pick of U.S. Appeals Court Judge Brett M. Kavanaugh.

Graham’s tweets included a recap of his appearance the night before on another Fox News Channel show, where he predicted Kavanaugh would win Senate confirmation and provide a boost to Trump in his 2020 reelection bid.

“Well done, Mr. President,” Graham said.

Graham’s embrace of Trump — which began months ago but seems to have tightened in the past week — has prompted questions about his motives, with some suggesting he’s angling for a job in a Cabinet in which vacancies have become commonplace.

Late last month, shortly before McCain’s death, Graham raised eyebrows when he seemed to give Trump his blessing to fire Attorney General Jeff Sessions as long as he waited until after the midterms. Graham, who once said there would “holy hell to pay” for such a move, voiced sympathy for Trump’s frustration with an attorney general who recused himself from the Russia probe.

Asked Wednesday by a McClatchy reporter if he were angling for an administration job, Graham emphatically denied that was his aim.

“How do you say ‘no’ in forty languages?” he said.

Graham may also be acting out of self-preservation ahead of his 2020 reelection bid, seeking to avoid a repeat of his 2014 campaign.

During that race, he faced a half-dozen primary challengers who sought to capi­tal­ize on unhappiness among the conservative base with his more-moderate positions on climate change and immigration as well as his votes to confirm both of President Barack Obama’s Supreme Court nominees.

For years, Graham has come under fire from conservative commentators such as Rush Limbaugh, who dubbed the senator “Lindsey Grahamnesty,” and Glenn Beck, who nicknamed him “Obama Lite.”

Graham ultimately won his 2014 primary with 56 percent of the vote. But he could face a tougher road in 2020, given the Republican Party’s rightward tilt and its full-on embrace of Trump.

McCain’s death, and the unmistakable critique of Trump that was part of the week of services celebrating his life, drew attention to the question of whether Graham would adopt the critical approach to Trump that McCain embraced and his supporters heralded.

In an emotional floor speech last week, Graham addressed the issue of who would fill McCain’s shoes broadly, urging all his colleagues to look to his friend and mentor for inspiration.

“The void to be filled by John’s passing is more than I can fill. Don’t look to me to replace this man,” Graham said last week. “Look to me to remember what he was all about and try to follow in his footsteps. If you want to help me, join the march. ”

That march, so far, has not included embracing McCain’s role as a vocal Republican counterweight to Trump.

Some of Graham’s colleagues said they’ve noticed a change since the 2016 election in Graham’s posture toward the president, with whom he now plays golf periodically.

“Lindsey’s developed a relationship with the president, he’s changed his opinion,” said Sen. Ron Johnson (R-Wis.). “You get to know somebody and you realize, no one’s perfect, everyone has pluses and minuses. . . . They enjoy each other’s company, but they continue to agree and disagree on a host of issues.”

Asked about Graham’s changing stance toward the president, Sen. Bob Corker (R-Tenn.), who has been a vocal critic of Trump and his policies, said: “Sometimes you get to know someone and you view them differently, you feel like you can learn to make a difference in other ways.”

Corker, who is retiring after this term, said that his relationship with Trump has not changed, however.

“There are, I’m sure, very good reasons for every senator to be acting and speaking in the way that they are,” he said. “For me, I came here a person, and I’m leaving here as that same person.”

Graham has emerged as a conduit between Trump and people seeking access to him. In an audio recording Woodward made of an August phone call he had with Trump, the journalist mentions that he asked Graham to pass on word to Trump that he was seeking to interview him for his book.

Trump, who at first denied that any senators contacted him, is later heard on the call acknowledging that Graham had mentioned Woodward to him.

Richard J. Durbin (Ill.), the second-ranking Senate Democrat, said that Graham tells his colleagues that he speaks regularly with Trump, “at all hours of the night and day.”

“It’s an unusual relationship,” Durbin said. Graham, he added, is “the only one that can describe that relationship, but I think it embodies the entire spectrum of opportunities for him to be critical one day and supportive the next.”

Others suggested Graham’s about-face is reflective of a broader recalibration taking place within the Republican Party as lawmakers gradually have come to realize that Trump has achieved success on many of their agenda items despite his un­or­tho­dox style.

“A lot of people are beginning to wake up and realize that his agenda’s actually working, what he believes in actually works, and that’s what’s going on,” Sen. David Perdue (R-Ga.) said.

Graham fired off a string of tweets Wednesday morning defending Trump against the harrowing portrayal in Woodwood’s forthcoming book.

Trump’s “fate will be determined by the results he achieves for the American people, not by a book about the process,” Graham wrote. “By any reasonable measure, we have one of the strongest economies in modern history, President Trump has rebuilt a broken military, and we are pushing back hard against America’s enemies.”

Among other unflattering accounts, Woodward’s book describes “an administrative coup d’etat” and a “nervous breakdown” of the executive branch, with senior aides conspiring to remove official papers from the president’s desk so he couldn’t see or sign them.

In the book, White House Chief of Staff John F. Kelly is said to have told colleagues that he considers Trump “unhinged” and “an idiot,” and contends that it is “pointless” to advise the president. Kelly disputed Woodward’s account.

In his tweets Wednesday, Graham ridiculed the idea that Kelly should resign, writing: “Only an ‘idiot’ or ‘moron’ would call for John Kelly to resign from the Trump White House.”

Kelly and other staffers, Graham wrote, “have served the president with distinction and honor” and “even a 5th grader would understand that General Kelly has provided sound advice to and invaluable leadership for President Trump.” (Woodward’s book says that Defense Secretary Jim Mattis has described Trump as having the intellect of a “fifth- or sixth-grader” — a remark Mattis denied.)

The senator’s defense of Trump came as other Southern lawmakers — from both parties — were voicing concerns about reporting in Woodward’s book that the president had called Sessions a “dumb Southerner” and mocked his accent. Trump denied Woodward’s account in a tweet Tuesday night.

Graham on Wednesday also distributed by Twitter an account of his television appearance Tuesday night on Fox News, in which he called Trump the “big winner” at the Senate hearing the day before on Kavanaugh’s nomination.

During his appearance on “The Story,” Graham also told host Martha MacCallum that Democratic senators had performed poorly, adding: “If this is their approach to how to govern, they’re going to have a hard time in 2020.”

Graham squared off against Trump in spirited fashion during the Republican presidential primaries but became friendlier with the president after he took office, saying he wanted to work with him where he can.

In a CNN interview Wednesday evening, Graham pushed back against a tweet in which Trump wrote a single word — “TREASON?” — after the New York Times published a scathing op-ed from an anonymous senior administration official.

The op-ed may have been a “disloyal and cowardly act towards the president,” but it wasn’t treason, Graham said. He also sought to explain his support for Trump as dependent on the president’s actions rather than his words.

“He ran a campaign that I didn’t like at all,” Graham said. “I’m telling you, I lost, he won, and from the time he’s been sworn in until now, he says a lot of things I don't like, but he’s doing things I respect.”

Felicia Sonmez contributed to this report.