Skip to content

Commit 9234442

Browse files
committed
Resolve EdgeDB FQNs with Resource's resolveType
This is an UGLY hack to use ResourcesHost statically
1 parent d6f3110 commit 9234442

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/common/resource.dto.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import { LazyGetter as Once } from 'lazy-get-decorator';
44
import { DateTime } from 'luxon';
55
import { keys as keysOf } from 'ts-transformer-keys';
66
import { inspect } from 'util';
7-
import type { ResourceDBMap, ResourceName } from '~/core';
7+
import type {
8+
ResourceDBMap,
9+
ResourceLike,
10+
ResourceName,
11+
ResourcesHost,
12+
} from '~/core';
813
import { $, e } from '~/core/edgedb/reexports';
914
import { ScopedRole } from '../components/authorization/dto';
1015
import { CalculatedSymbol } from './calculated.decorator';
@@ -27,7 +32,7 @@ const hasTypename = (value: unknown): value is { __typename: string } =>
2732
export const resolveByTypename =
2833
(interfaceName: string) => (value: unknown) => {
2934
if (hasTypename(value)) {
30-
return value.__typename;
35+
return EnhancedResource.resolve(value.__typename).name;
3136
}
3237
throw new ServerException(`Cannot resolve ${interfaceName} type`);
3338
};
@@ -87,13 +92,22 @@ export class EnhancedResource<T extends ResourceShape<any>> {
8792
static readonly dbTypes = new WeakMap<ResourceShape<any>, $.$expr_PathNode>();
8893
/** @internal */
8994
static readonly dbSkipAccessPolicies = new Set<string>();
95+
/** @internal */
96+
static resourcesHost?: ResourcesHost;
9097

9198
private constructor(readonly type: T) {}
9299
private static readonly refs = new WeakMap<
93100
ResourceShape<any>,
94101
EnhancedResource<any>
95102
>();
96103

104+
static resolve(ref: ResourceLike) {
105+
if (!EnhancedResource.resourcesHost) {
106+
throw new ServerException('Cannot resolve without ResourcesHost');
107+
}
108+
return EnhancedResource.resourcesHost.enhance(ref);
109+
}
110+
97111
static of<T extends ResourceShape<any>>(
98112
resource: T | EnhancedResource<T>,
99113
): EnhancedResource<T> {

src/core/resources/resources.host.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ export type ResourceLike =
3030

3131
@Injectable()
3232
export class ResourcesHost {
33-
constructor(private readonly gqlSchema: GraphQLSchemaHost) {}
33+
constructor(private readonly gqlSchema: GraphQLSchemaHost) {
34+
EnhancedResource.resourcesHost = this;
35+
}
3436

3537
getMap() {
3638
// @ts-expect-error Yeah we are assuming each type has been correctly

0 commit comments

Comments
 (0)