1
1
import { Injectable } from '@nestjs/common' ;
2
2
import { GraphQLSchemaHost } from '@nestjs/graphql' ;
3
- import { CachedByArg } from '@seedcompany/common' ;
3
+ import { CachedByArg , mapKeys } from '@seedcompany/common' ;
4
4
import { isObjectType } from 'graphql' ;
5
5
import { mapValues } from 'lodash' ;
6
6
import { LiteralUnion , ValueOf } from 'type-fest' ;
@@ -42,6 +42,7 @@ export class ResourcesHost {
42
42
return Object . keys ( map ) as Array < keyof ResourceMap > ;
43
43
}
44
44
45
+ @CachedByArg ( )
45
46
async getEnhancedMap ( ) : Promise < EnhancedResourceMap > {
46
47
const map = await this . getMap ( ) ;
47
48
return mapValues ( map , EnhancedResource . of ) as any ;
@@ -66,6 +67,36 @@ export class ResourcesHost {
66
67
return await this . getByName ( name as any ) ;
67
68
}
68
69
70
+ async getByEdgeDB (
71
+ fqn : string ,
72
+ ) : Promise < EnhancedResource < ValueOf < ResourceMap > > > {
73
+ fqn = fqn . includes ( '::' ) ? fqn : `default::${ fqn } ` ;
74
+ const map = await this . edgeDBFQNMap ( ) ;
75
+ const resource = map . get ( fqn ) ;
76
+ if ( ! resource ) {
77
+ throw new ServerException (
78
+ `Unable to determine resource from ResourceMap for EdgeDB FQN: ${ fqn } ` ,
79
+ ) ;
80
+ }
81
+ return resource ;
82
+ }
83
+
84
+ @CachedByArg ( )
85
+ private async edgeDBFQNMap ( ) {
86
+ const map = await this . getEnhancedMap ( ) ;
87
+ const fqnMap = mapKeys (
88
+ map as Record < string , EnhancedResource < any > > ,
89
+ ( _ , r , { SKIP } ) => {
90
+ try {
91
+ return r . dbFQN ;
92
+ } catch ( e ) {
93
+ return SKIP ;
94
+ }
95
+ } ,
96
+ ) . asMap ;
97
+ return fqnMap ;
98
+ }
99
+
69
100
async verifyImplements ( resource : ResourceLike , theInterface : ResourceLike ) {
70
101
const iface = await this . enhance ( theInterface ) ;
71
102
if ( ! ( await this . doesImplement ( resource , iface ) ) ) {
0 commit comments