DENVER — Jon Gray was closing in on his first complete game when everything unraveled with eight outs to go.

Instead of a breakthrough win, he wound up with a tough loss.

Socrates Brito snapped a seventh-inning tie with a three-run homer off the Rockies right-hander, and the Arizona Diamondbacks held off a late rally to beat Colorado 8-5 on Sunday.

The home run spoiled a great start for Gray (9-7), who was cruising along until he ran into trouble in the seventh.

“My fastball was working, I was getting ahead of guys and getting quick outs,” he said. “That is what kept it efficient. I wanted to go deep into the game. I felt like I could have gone 20 innings.”

Gray had allowed a run in the first inning on a sacrifice fly but retired 17 of 19 batters before Arizona roughed him up in the seventh.

Trailing 2-1, the Diamondbacks tied it with three straight one-out singles. Brito made it 5-2 when he hit Gray’s first-pitch fastball into the second deck in right-center field.

“He’s got a good fastball and I wasn’t trying to do too much,” Brito said. “He’s a guy that throws a lot of fastballs, so I was always looking fastball.”

It was his third of the season and the first home run off Gray in 22 1/3 innings.

“He was in complete control the entire game,” Rockies manager Walt Weiss said. “Then he had baserunners there in the seventh.”

Pinch-hitter Tom Murphy belted a solo homer off Patrick Corbin in the bottom of the inning, but Brandon Drury’s two-run single in the eighth gave Arizona a four-run lead.

Murphy’s home run carried an estimated 482 feet.

“He put the whole barrel on it, so when you get it airborne it goes a long way,” Weiss said.

The Diamondbacks added a run in the ninth and then had to withstand Colorado’s comeback attempt. The Rockies scored twice on a single and a wild pitch, but Daniel Hudson struck out DJ LeMahieu and Carlos Gonzalez with two runners on to end it.

Archie Bradley (6-8) scattered six hits over six innings and Jean Segura had three hits for Arizona.

TRAINER’S ROOM

Rockies: OF Charlie Blackmon was out of the lineup for the second straight game with back tightness. Weiss said Blackmon has improved and was getting treatment. He could return Monday.

STREAKS

Paul Goldschmidt failed to reach base for the first time in 51 games against the Rockies. He was 0 for 4 with a sacrifice fly. He struck out looking in the ninth with a chance to prolong the streak. … LeMahieu extended his streak of reaching base safely to 21 games with a single in the first.

FIRST IMPRESSIONS

Rockies rookie Raimel Tapia was 3 for 5 in his second start since being recalled Friday. He also got his first stolen base.

UP NEXT

Rockies RHP Chad Bettis (11-7, 5.17 ERA) opens a three-game series against San Francisco on Monday.