Most farm groups said yesterday they oppose cutting off U.S. grain shipments to the Soviet Union, but union leaders said longshoremen might stop loading cargo bound for Soviet ports in retaliation for the Soviet invasion of Afghanistan.

A grain cutoff is one of several steps the administration is reported to have been considering, although it is thought to be unlikely.

John Bowers, executive vice president of the 116,000-member International Longshoremen's Association, said in a telephone interview from New York that members of the union in Gulf Coast and Midwest ports are "strongly anticommunist and have been infuriated by the Soviet intervention in Afghanistan.

He said that for 25 years until President Nixon asked them to resume in 1973, they refused to handle Soviet-bound cargo, and many members wanted to resume that stance and stop handling the millions of tons of corn and wheat that U.S. farmers are shipping to the Soviet Union.

For the time being, Bowers said, "we are telling them to work," but when union chief Teddy Gleason returns today after a brief illness, he is to consider the matter and may call an executive board meeting to see what should be done.

Bowers said widespread refusal of longshoremen to handle grain cargoes is a strong possibility, regardless of whether President Carter decides to impose an official embargo.

The leaders of U.S. farm organizations, meanwhile, as well as several presidential candidates such as Sen. Bob Dole (R-Kan.) and George Bush, another Republican hopeful, either opposed a grain-trade cutoff outright or backed away from it.

Bob Lewis of the National Farmers Union said his group whose members are heavily concentrated in graingrowing areas of the Midwest and West, is "unalterably opposed to any embargo just on agricultural products." He said that would assign to farmers the entire burden of putting economic pressure on the Soviets, and would mean economic disaster for many farmers.

"Russian purchases equal 10 percent of U.S. production of [corn] and wheat," Lewis said. If these purchases, expected to amount to 25 million metric tons (three quarters of it corn) in the 1979-80 marketing year, were cut off, surpluses would pile up in the United States and drive down market prices sharply, he said. There would then be considerable pressure to have the government raise U.S. price supports and pay crop subsidies to farmers to make up the losses.

John Datt, spokesman for the American Farm Bureau Federation, the largest farm group, said it opposes a grain cutoff because "historically we have opposed using food as a weapon in diplomacy." He added that while a cutoff probably would hurt the Soviet Union, it would also hurt U.S. farmers by creating price-depressing surpluses.

Carl Schwensen of the National Association of Wheat Growers said, "We don't see it as any short-term solution to either Iran or Afghanistan . . . it would reduce prices, the farmers would suffer the heaviest."

The National Grange said it would go along with a cutoff of shipments if the president made it national policy, but "we're not really for an embargo now."

The National Farmers Organization chief, DeVon Woodland said, "We're very jealous of our export markets . . . but we're wise enough to know you don't feed the enemy. If the president chooses to do that as a tool or weapon, we'll support that," but it would have a "devastating effect" on U.S. prices that would necessitate an increase in price supports and crop subsidies. "We're certainly not one to advocate it as the first tool."

Dole and Bush spokesmen both said the two men oppose an embargo at this time. A spokesman for Rep. John B. Anderson (R-Ill.), another GOP presidential candidate, said Anderson hasn't endorsed a cutoff but believes an international conference of grain producers should meet to consider it.

Agricultural experts made these points: of all wheat sold in the world market -- 80 million metric tons -- the United States sells about 38 million, less than half. If the United States acted alone to cut off the 6 million tons it expects to send the Soviet Union, Russia probably could replace much of that by buying from other exporters like Canada, Australia, Argentina and some European countries.

On corn, the picture is different. Of 100 million metric tons of feed sold in the world market, the United States exports nearly three-quarters. If the United States cut off the 18-million-plus tons it expects to ship to the Soviet Union this year, Russia probably couldn't replace it elsewhere. This would mean less Soviet production of the beef, pork and other meats the Soviets want to upgrade their national diet. A feed cutoff would have a definite impact.

Today, U.S. wheat and corn prices on the market are higher than government "target" support prices, but experts predicted that they would sink sharply if exports to the Soviet Union were stopped.

Meanwhile, the Agriculture Department reported that in recent weeks the Soviets have ordered at least 3.8 million tons of corn and wheat. Officials seemed doubtful that the Soviets were hastening their purchase agreements to get them in before a possible embargo, but said it is possible.