|
1 |
| -import { GraphQLResolveInfo } from 'graphql' |
2 |
| - |
3 |
| -import { getNavigationMaps, INavigationMap } from '../dynamic-navigations' |
4 |
| -import { |
5 |
| - extractGraphQLSelectionPath, |
6 |
| - extractGraphQLSelections, |
7 |
| - toCamelCase, |
8 |
| - updatePathInfoFromSchema, |
9 |
| -} from '../helpers' |
10 |
| - |
| 1 | +import type { GraphQLResolveInfo } from 'graphql' |
| 2 | +import type { INavigationMap } from '../dynamic-navigations' |
11 | 3 | import type { Dictionary } from '../types'
|
12 | 4 |
|
13 | 5 | export abstract class DynamicResolverBase {
|
14 | 6 | constructor(protected readonly prisma: any) {}
|
15 | 7 |
|
16 |
| - protected getSelectionObject(info: GraphQLResolveInfo, selectionMap?: Dictionary<string>) { |
17 |
| - const select = extractGraphQLSelections({ |
18 |
| - node: info.fieldNodes[ 0 ], |
19 |
| - selectionMap, |
20 |
| - }) |
21 |
| - |
22 |
| - return select |
23 |
| - } |
24 |
| - |
25 |
| - protected getSelectionPath(info: GraphQLResolveInfo, selectionMap?: Dictionary<string>) { |
26 |
| - const selectionPath = extractGraphQLSelectionPath({ |
27 |
| - path: info.path, |
28 |
| - }) |
29 |
| - |
30 |
| - const pathInfoList = selectionPath |
31 |
| - pathInfoList.forEach(pathInfo => { |
32 |
| - if (pathInfo.table) return |
33 |
| - |
34 |
| - const tableName = toCamelCase(pathInfo.tableName) |
35 |
| - const maps = getNavigationMaps() |
36 |
| - .filter(it => it.sourceTableName === tableName) |
37 |
| - |
38 |
| - pathInfo.table = maps[ 0 ]?.source |
39 |
| - pathInfo.navigationMaps = maps |
40 |
| - |
41 |
| - updatePathInfoFromSchema(pathInfo, info.schema) |
42 |
| - }) |
43 |
| - |
44 |
| - const query = pathInfoList[ 0 ] |
45 |
| - |
46 |
| - if (pathInfoList.length < 3) { |
47 |
| - throw new Error(`The navigation source could not be extracted in (${query.propertyName})`) |
48 |
| - } |
49 |
| - |
50 |
| - const firstLink = pathInfoList[ 1 ] |
51 |
| - |
52 |
| - if (!firstLink.table) { |
53 |
| - throw new Error(`The navigation target could not be extracted in (${query.propertyName}): '${firstLink.propertyName}' → ?`) |
54 |
| - } |
55 |
| - |
56 |
| - const secondLink = pathInfoList[ 2 ] |
57 |
| - |
58 |
| - if (!secondLink.table) { |
59 |
| - throw new Error(`The navigation target could not be extracted in (${query.propertyName}): ${firstLink.propertyName} → ${secondLink.propertyName}' → ?`) |
60 |
| - } |
61 |
| - |
62 |
| - const sourceTableName |
63 |
| - = selectionMap?.[ firstLink.propertyName ] |
64 |
| - ?? toCamelCase(firstLink.table.name) |
65 |
| - |
66 |
| - const source = this.prisma[ sourceTableName ] |
67 |
| - if (!source) { |
68 |
| - throw new Error(`Unrecognized Prisma model: "${sourceTableName}". Failed to build resolver for navigation: '${sourceTableName} → ${secondLink.propertyName}'`) |
69 |
| - } |
70 |
| - |
71 |
| - return { |
72 |
| - to: secondLink, |
73 |
| - root: query, |
74 |
| - from: firstLink, |
75 |
| - source, |
76 |
| - selectionPath, |
77 |
| - } |
78 |
| - } |
79 |
| - |
80 | 8 | protected resolve(parent: any, _primaryKeyName: string, _info: GraphQLResolveInfo, navigationMap: INavigationMap, _selectionMap?: Dictionary<string>) {
|
81 | 9 | const {
|
82 | 10 | relation,
|
|
0 commit comments