Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/client/src/runtime/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export class RequestHandler {
}
const operation = Object.keys(data)[0]
const response = Object.values(data)[0]
const pathForGet = dataPath.filter((key) => key !== 'select' && key !== 'include')
const pathForGet = dataPathToGetPath(dataPath)
const extractedResponse = deepGet(response, pathForGet)
const deserializedResponse =
operation === 'queryRaw'
Expand Down Expand Up @@ -367,3 +367,19 @@ function convertValidationError(error: EngineValidationError): EngineValidationE

return error
}

/**
* Converts a fluent-API dataPath into the path used to read the result out of
* the response. dataPath is a sequence of [selector, relationField] pairs where
* the selector is always 'select' or 'include'. The relation field names (the
* odd positions) form the path. Filtering by the literal values 'select' or
* 'include' would also drop a relation field that happens to be named that way,
* so the path is derived positionally instead.
*/
export function dataPathToGetPath(dataPath: string[]): string[] {
const getPath: string[] = []
for (let index = 1; index < dataPath.length; index += 2) {
getPath.push(dataPath[index])
}
return getPath
}
18 changes: 18 additions & 0 deletions packages/client/src/runtime/dataPathToGetPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { dataPathToGetPath } from './RequestHandler'

describe('dataPathToGetPath', () => {
test('returns the relation field names from a fluent dataPath', () => {
expect(dataPathToGetPath(['select', 'posts'])).toEqual(['posts'])
expect(dataPathToGetPath(['select', 'posts', 'include', 'comments'])).toEqual(['posts', 'comments'])
})

test('keeps a relation field named "select" or "include"', () => {
expect(dataPathToGetPath(['select', 'select'])).toEqual(['select'])
expect(dataPathToGetPath(['select', 'include'])).toEqual(['include'])
expect(dataPathToGetPath(['select', 'posts', 'select', 'select'])).toEqual(['posts', 'select'])
})

test('returns an empty path for the root result', () => {
expect(dataPathToGetPath([])).toEqual([])
})
})
Loading