Skip to content

Commit f6bde2f

Browse files
committed
Add the ability to get resource by an EdgeDB FQN
1 parent 1d3185b commit f6bde2f

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/common/resource.dto.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ export class EnhancedResource<T extends ResourceShape<any>> {
247247
return type;
248248
}
249249

250+
get dbFQN(): (T['DB'] & {})['__element__']['__name__'] {
251+
return this.db.__element__.__name__;
252+
}
253+
250254
@Once()
251255
get dbLabels() {
252256
return getDbClassLabels(this.type);

src/core/resources/resources.host.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@nestjs/common';
22
import { GraphQLSchemaHost } from '@nestjs/graphql';
3-
import { CachedByArg } from '@seedcompany/common';
3+
import { CachedByArg, mapKeys } from '@seedcompany/common';
44
import { isObjectType } from 'graphql';
55
import { mapValues } from 'lodash';
66
import { LiteralUnion, ValueOf } from 'type-fest';
@@ -42,6 +42,7 @@ export class ResourcesHost {
4242
return Object.keys(map) as Array<keyof ResourceMap>;
4343
}
4444

45+
@CachedByArg()
4546
async getEnhancedMap(): Promise<EnhancedResourceMap> {
4647
const map = await this.getMap();
4748
return mapValues(map, EnhancedResource.of) as any;
@@ -66,6 +67,36 @@ export class ResourcesHost {
6667
return await this.getByName(name as any);
6768
}
6869

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+
69100
async verifyImplements(resource: ResourceLike, theInterface: ResourceLike) {
70101
const iface = await this.enhance(theInterface);
71102
if (!(await this.doesImplement(resource, iface))) {

0 commit comments

Comments
 (0)