Democrat Jim Chapman narrowly won a special congressional election today against a Republican who had waged a $1 million campaign to break the century-old Democratic hammerlock on the rural northeast Texas 1st District.

With all of the votes in, Chapman defeated Republican Edd Hargett by 52,670 to 50,737 -- about 51 percent to 49 percent -- in a race that the GOP had touted as a measure of their emerging strength in populist, "yellow-dog Democrat" regions of the South.

"So much for realignment," chortled George Shipley, Chapman's pollster and strategist, savoring the win in a showdown that the beleaguered Texas Democratic Party treated as an Armageddon.

Although they were heavily outspent, the Democrats threw all their county courthouse and labor muscle into the fray. State Democratic Chairman Robert Slagle took the contest so intently that he spent the past three weeks organizing the precincts in the city of Marshall, population 24,500).

"This is for our self-respect," he said on the closing day of the campaign.

Chapman, 40, a former district attorney, and Hargett, 38, a farmer, engineer and political novice, were vying for a seat that became vacant earlier this year when Rep. Sam Hall (D-Tex.) resigned to become a federal judge.

The term "yellow dog" is used through the Deep South to describe Democrats so partisan they would vote for such a creature before they would vote Republican. For a century, it aptly described the electorate of the cattle ranches, dairy farms and oil fields of this piney woods region, where the population is the oldest and third poorest of any district in the state.

Although not a single local elected official in the 1st District is a Republican, the political winds here appeared to be shifting. Last year the district gave President Reagan a lopsided margin, and for the first time in modern history it supported a Republican in a Senate race, Sen. Phil Gramm, a Democrat-turned-Republican.

It was Gramm who engineered Hall's appointment, theorizing that with the tug of Democratic loyalty loosening, the Republicans might have a chance to sneak off with an open seat race and add to their record contingent (10 of 26) in the Texas House delegation.

"It was historic to have come so close, but making history and losing is no fun," Gramm said as he glumly pored over returns at Hargett headquarters here.

"Four years ago it would have been impossible for a Republican to have come so close," added former representative Kent R. Hance, a Democratic "switcher" who is preparing to run for governor next year as a Republican. Not all Republicans were so bent on putting the bravest face on defeat. "Let's face it, there are still a lot of behavioral Democrats down here," said GOP consultant Lee Atwater. "Coming into this district, I felt a little like Custer at Little Big Horn.

"I don't think we made any strategic mistakes," he added, "except maybe thinking we could win in the first place."

Democrats in Washington did not contain their glee. "Score one for the Texas Democrats over the national Republicans," said Rep. Tony Coelho (D-Calif.), chairman of the Democratic Congressional Campaign Committee.

"So much for the A-team of Lee Atwater and company," added committee communications director Mark Johnson.

Until recent years, the GOP strongholds in congressional races in Texas and through the South have been in metropolitan areas, such as Dallas, Houston and Charlotte. Republicans had hoped a victory here today would serve notice that some dozen Democratic representatives from hitherto safe "yellow-dog districts" might be vulnerable.

Before the early returns started to buoy their confidence, Democratic leaders had played down the meaning of the contest. "There's no message in a special election," said Slagle, who noted that the party has always had trouble turning out votes on nontraditional election days.

He also said Hargett had spent the entire campaign "hiding the fact that he's a Republican, so how can you call it realignment if he wins?"

The turnout was unusally heavy -- in excess of 30 percent -- for a midsummer special election. In the first round of the election five weeks ago, just 23 percent of the voters turned out.