From b8d63b012515dd6ac3f8d92821700c7bb0447d1c Mon Sep 17 00:00:00 2001 From: Colton Schlosser <1498529+cltnschlosser@users.noreply.github.com> Date: Sat, 15 Jun 2024 19:30:35 -0500 Subject: [PATCH] Creates a unit test to reproduce bug caused by conflict between entity key and field alias --- .../src/__tests__/executeQueryPlan.test.ts | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/gateway-js/src/__tests__/executeQueryPlan.test.ts b/gateway-js/src/__tests__/executeQueryPlan.test.ts index 160f4cf0e..0d2b676e6 100644 --- a/gateway-js/src/__tests__/executeQueryPlan.test.ts +++ b/gateway-js/src/__tests__/executeQueryPlan.test.ts @@ -6868,6 +6868,124 @@ describe('executeQueryPlan', () => { `); }); + it('handles @key leading to a conflict with a requested alias', async () => { + const s1 = { + name: 'S1', + typeDefs: gql` + type Query { + object: O + } + + type O @key(fields: "id") { + id: ID! + legacyId: ID + a: String + } + `, + resolvers: { + Query: { + object() { + return { __typename: 'O', id: 'newId', legacyId: 'legacyId', a: 'a' }; + }, + }, + }, + }; + + const s2 = { + name: 'S2', + typeDefs: gql` + extend type O @key(fields: "id") { + id: ID! + a: String @external + b: String @requires(fields: "a") + } + `, + resolvers: { + O: { + __resolveReference(ref: { id: number }) { + return ref; + }, + b(o: { a: string }) { + return `B: ${o.a}`; + }, + }, + }, + }; + + const { serviceMap, schema, queryPlanner } = getFederatedTestingSchema([ + s1, + s2, + ]); + + const operationWithoutJoin = parseOp( + ` + query TestQuery { + object { + id: legacyId + legacyId + a + } + } + `, + schema, + ); + + const queryPlanWithoutJoin = buildPlan(operationWithoutJoin, queryPlanner); + const requestContext = buildRequestContext(); + + const responseWithoutJoin = await executePlan( + queryPlanWithoutJoin, + operationWithoutJoin, + requestContext, + schema, + serviceMap, + ); + expect(responseWithoutJoin.data).toMatchInlineSnapshot(` + Object { + "object": Object { + "a": "a", + "id": "legacyId", + "legacyId": "legacyId", + }, + } + `); + + const operationWithJoin = parseOp( + ` + query TestQuery { + object { + id: legacyId + legacyId + a + b + } + } + `, + schema, + ); + + const queryPlanWithJoin = buildPlan(operationWithJoin, queryPlanner); + + const responseWithJoin = await executePlan( + queryPlanWithJoin, + operationWithJoin, + requestContext, + schema, + serviceMap, + ); + // BUG: id is returned as 'newId' instead of being the legacyId + expect(responseWithJoin.data).toMatchInlineSnapshot(` + Object { + "object": Object { + "a": "a", + "b": "B: a", + "id": "legacyId", + "legacyId": "legacyId", + }, + } + `); + }) + it('handles field conflicting when type-exploding', async () => { const s1 = { name: 'S1',