From 63340f393e8fd9a9b551aa182244cf79ffe91bd6 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Fri, 19 Jun 2020 17:36:21 +0100 Subject: [PATCH] Add parentType to path to avoid path ambiguity --- src/execution/__tests__/executor-test.js | 2 +- src/execution/execute.js | 6 +++--- src/jsutils/Path.d.ts | 7 ++++++- src/jsutils/Path.js | 3 ++- src/subscription/subscribe.js | 2 +- src/utilities/coerceInputValue.js | 4 ++-- 6 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/execution/__tests__/executor-test.js b/src/execution/__tests__/executor-test.js index c6684372e7b..a01a5b7eca1 100644 --- a/src/execution/__tests__/executor-test.js +++ b/src/execution/__tests__/executor-test.js @@ -306,7 +306,7 @@ describe('Execute: Handles basic execution tasks', () => { const field = operation.selectionSet.selections[0]; expect(resolvedInfo).to.deep.include({ fieldNodes: [field], - path: { prev: undefined, key: 'result' }, + path: { prev: undefined, key: 'result', parentType: undefined }, variableValues: { var: 'abc' }, }); }); diff --git a/src/execution/execute.js b/src/execution/execute.js index 550dfeab4a1..9a6f69aba96 100644 --- a/src/execution/execute.js +++ b/src/execution/execute.js @@ -400,7 +400,7 @@ function executeFieldsSerially( Object.keys(fields), (results, responseName) => { const fieldNodes = fields[responseName]; - const fieldPath = addPath(path, responseName); + const fieldPath = addPath(path, responseName, parentType.name); const result = resolveField( exeContext, parentType, @@ -440,7 +440,7 @@ function executeFields( for (const responseName of Object.keys(fields)) { const fieldNodes = fields[responseName]; - const fieldPath = addPath(path, responseName); + const fieldPath = addPath(path, responseName, parentType.name); const result = resolveField( exeContext, parentType, @@ -918,7 +918,7 @@ function completeListValue( const completedResults = arrayFrom(result, (item, index) => { // No need to modify the info object containing the path, // since from here on it is not ever accessed by resolver functions. - const fieldPath = addPath(path, index); + const fieldPath = addPath(path, index, undefined); const completedItem = completeValueCatchingError( exeContext, itemType, diff --git a/src/jsutils/Path.d.ts b/src/jsutils/Path.d.ts index 28bba41712b..bebca6c3d89 100644 --- a/src/jsutils/Path.d.ts +++ b/src/jsutils/Path.d.ts @@ -1,12 +1,17 @@ export interface Path { prev: Path | undefined; key: string | number; + parentType: string | undefined; } /** * Given a Path and a key, return a new Path containing the new key. */ -export function addPath(prev: Path | undefined, key: string | number): Path; +export function addPath( + prev: Path | undefined, + key: string | number, + parentType: string | undefined, +): Path; /** * Given a Path, return an Array of the path keys. diff --git a/src/jsutils/Path.js b/src/jsutils/Path.js index f477354e076..d56af4eb265 100644 --- a/src/jsutils/Path.js +++ b/src/jsutils/Path.js @@ -11,8 +11,9 @@ export type Path = {| export function addPath( prev: $ReadOnly | void, key: string | number, + parentTypeName: string | undefined, ): Path { - return { prev, key }; + return { prev, key, parentTypeName }; } /** diff --git a/src/subscription/subscribe.js b/src/subscription/subscribe.js index 0651e84dfc7..a225a2653a3 100644 --- a/src/subscription/subscribe.js +++ b/src/subscription/subscribe.js @@ -253,7 +253,7 @@ export function createSourceEventStream( // AsyncIterable yielding raw payloads. const resolveFn = fieldDef.subscribe ?? exeContext.fieldResolver; - const path = addPath(undefined, responseName); + const path = addPath(undefined, responseName, type); const info = buildResolveInfo(exeContext, fieldDef, fieldNodes, type, path); diff --git a/src/utilities/coerceInputValue.js b/src/utilities/coerceInputValue.js index 36e4eae0b22..69bd20710da 100644 --- a/src/utilities/coerceInputValue.js +++ b/src/utilities/coerceInputValue.js @@ -82,7 +82,7 @@ function coerceInputValueImpl( const itemType = type.ofType; if (isCollection(inputValue)) { return arrayFrom(inputValue, (itemValue, index) => { - const itemPath = addPath(path, index); + const itemPath = addPath(path, index, undefined); return coerceInputValueImpl(itemValue, itemType, onError, itemPath); }); } @@ -126,7 +126,7 @@ function coerceInputValueImpl( fieldValue, field.type, onError, - addPath(path, field.name), + addPath(path, field.name, undefined), ); }