diff --git a/public/app/features/query/state/runRequest.test.ts b/public/app/features/query/state/runRequest.test.ts index 6742d37d0bf..8a40ef7fd08 100644 --- a/public/app/features/query/state/runRequest.test.ts +++ b/public/app/features/query/state/runRequest.test.ts @@ -156,7 +156,7 @@ describe('runRequest', () => { }); }); - runRequestScenario('After tree responses, 2 with different keys', (ctx) => { + runRequestScenario('After three responses, 2 with different keys', (ctx) => { ctx.setup(() => { ctx.start(); ctx.emitPacket({ @@ -186,6 +186,40 @@ describe('runRequest', () => { }); }); + runRequestScenario('When the key is defined in refId', (ctx) => { + ctx.setup(() => { + ctx.start(); + ctx.emitPacket({ + data: [{ name: 'DataX-1', refId: 'X' } as DataFrame], + }); + ctx.emitPacket({ + data: [{ name: 'DataY-1', refId: 'Y' } as DataFrame], + }); + ctx.emitPacket({ + data: [{ name: 'DataY-2', refId: 'Y' } as DataFrame], + }); + }); + + it('should emit 3 separate results', () => { + expect(ctx.results.length).toBe(3); + }); + + it('should keep data for X and Y', () => { + expect(ctx.results[2].series).toMatchInlineSnapshot(` + Array [ + Object { + "name": "DataX-1", + "refId": "X", + }, + Object { + "name": "DataY-2", + "refId": "Y", + }, + ] + `); + }); + }); + runRequestScenario('After response with state Streaming', (ctx) => { ctx.setup(() => { ctx.start(); diff --git a/public/app/features/query/state/runRequest.ts b/public/app/features/query/state/runRequest.ts index 370df008e9b..76a7442ab3f 100644 --- a/public/app/features/query/state/runRequest.ts +++ b/public/app/features/query/state/runRequest.ts @@ -43,7 +43,9 @@ export function processResponsePacket(packet: DataQueryResponse, state: RunningQ ...state.packets, }; - packets[packet.key || 'A'] = packet; + // updates to the same key will replace previous values + const key = packet.key ?? packet.data?.[0]?.refId ?? 'A'; + packets[key] = packet; let loadingState = packet.state || LoadingState.Done; let error: DataQueryError | undefined = undefined;