Skip to content

Commit 750a2d0

Browse files
committed
Declare typename for Actors to help distinguish against other resources
1 parent 869a3e3 commit 750a2d0

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

src/components/user/dto/actor.dto.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { e } from '~/core/gel';
1919
resolveType: resolveByTypename(Actor.name),
2020
})
2121
export class Actor extends DataObject {
22+
declare readonly __typename: 'User' | 'SystemAgent';
23+
2224
@IdField()
2325
readonly id: ID;
2426
}
@@ -31,7 +33,7 @@ export class Actor extends DataObject {
3133
implements: [Actor],
3234
})
3335
export abstract class SystemAgent extends Actor {
34-
__typename?: 'SystemAgent';
36+
declare readonly __typename: 'SystemAgent';
3537

3638
@NameField()
3739
readonly name: string;

src/components/user/dto/user.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export class User extends Interfaces {
4545
...Commentable.Relations,
4646
} satisfies ResourceRelationsShape);
4747

48+
declare readonly __typename: 'User';
49+
4850
@Field()
4951
@DbUnique('EmailAddress')
5052
email: SecuredStringNullable;
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@nestjs/common';
22
import { type Role } from '~/common';
3-
import { disableAccessPolicies, edgeql, Gel } from '~/core/gel';
3+
import { disableAccessPolicies, e, Gel } from '~/core/gel';
44
import { SystemAgentRepository } from './system-agent.repository';
55

66
@Injectable()
@@ -12,15 +12,15 @@ export class SystemAgentGelRepository extends SystemAgentRepository {
1212
}
1313

1414
protected async upsertAgent(name: string, roles?: readonly Role[]) {
15-
const query = edgeql(`
16-
select (
17-
(select SystemAgent filter .name = <str>$name) ??
18-
(insert SystemAgent {
19-
name := <str>$name,
20-
roles := array_unpack(<optional array<Role>>$roles)
21-
})
22-
) {*}
23-
`);
24-
return await this.db.run(query, { name, roles });
15+
const upserted = e.op(
16+
e.select(e.SystemAgent, () => ({ filter_single: { name } })),
17+
'??',
18+
e.insert(e.SystemAgent, { name, roles }),
19+
);
20+
const query = e.select(upserted, (agent) => ({
21+
__typename: e.str('SystemAgent' as const),
22+
...agent['*'],
23+
}));
24+
return await this.db.run(query);
2525
}
2626
}

src/components/user/system-agent.neo4j.repository.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Injectable } from '@nestjs/common';
22
import { node } from 'cypher-query-builder';
3-
import { type ID, type Role } from '~/common';
3+
import { type Role } from '~/common';
44
import { DatabaseService } from '~/core/database';
5+
import { merge } from '~/core/database/query';
6+
import { type SystemAgent } from './dto';
57
import { SystemAgentRepository } from './system-agent.repository';
68

79
@Injectable()
@@ -22,8 +24,8 @@ export class SystemAgentNeo4jRepository extends SystemAgentRepository {
2224
'agent.roles': roles ?? [],
2325
},
2426
})
25-
.return<{ agent: { id: ID; name: string; roles: readonly Role[] } }>(
26-
'apoc.convert.toMap(agent) AS agent',
27+
.return<{ agent: SystemAgent }>(
28+
merge('agent', { __typename: '"SystemAgent"' }).as('agent'),
2729
)
2830
.first();
2931
return res!.agent;

0 commit comments

Comments
 (0)