Skip to content

Commit de19274

Browse files
committed
Create Identity.readyForCli for data loaders
This should be replaced with something else later. Other entry points (migrations, etc) should declare something explicit.
1 parent cb5fce8 commit de19274

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/core/authentication/identity.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,12 @@ export class Identity {
3636
asRole<R>(role: Role, fn: () => R): R {
3737
return this.auth.asRole(role, fn);
3838
}
39+
40+
/**
41+
* @deprecated probably replace this with something more explicit.
42+
*/
43+
async readyForCli() {
44+
// Ensure the default root session is ready to go for data loaders
45+
await this.auth.lazySessionForRootUser();
46+
}
3947
}

src/core/resources/resource.loader.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Inject, Injectable, type Type } from '@nestjs/common';
1+
import { Injectable, type Type } from '@nestjs/common';
22
import {
33
DataLoaderContext,
44
type DataLoaderStrategy,
55
} from '@seedcompany/data-loader';
66
import type { ConditionalKeys, Merge, ValueOf } from 'type-fest';
77
import { type ID, type Many, type ObjectView, ServerException } from '~/common';
8-
import type { AuthenticationService } from '../../components/authentication';
8+
import { Identity } from '../authentication';
99
import { ConfigService } from '../config/config.service';
1010
import { type BaseNode } from '../database/results';
1111
import { GqlContextHost } from '../graphql';
@@ -44,7 +44,7 @@ export class ResourceLoader {
4444
private readonly loaderRegistry: ResourceLoaderRegistry,
4545
private readonly contextHost: GqlContextHost,
4646
private readonly config: ConfigService,
47-
@Inject('AUTHENTICATION') private readonly auth: AuthenticationService & {},
47+
private readonly identity: Identity,
4848
private readonly loaderContext: DataLoaderContext,
4949
private readonly resourceResolver: ResourceResolver,
5050
) {}
@@ -111,13 +111,12 @@ export class ResourceLoader {
111111
type: Type<DataLoaderStrategy<T, Key, CachedKey>>,
112112
) {
113113
if (this.config.isCli) {
114-
// Ensure the default root session is ready to go for data loaders
115-
await this.auth.lazySessionForRootUser();
114+
await this.identity.readyForCli();
116115
}
117-
return await this.loaderContext.getLoader<T, Key, CachedKey>(
118-
type,
119-
this.config.isCli ? CLI_CONTEXT_ID : this.contextHost.context,
120-
);
116+
const context = this.config.isCli
117+
? CLI_CONTEXT_ID
118+
: this.contextHost.context;
119+
return await this.loaderContext.getLoader<T, Key, CachedKey>(type, context);
121120
}
122121

123122
private findLoaderFactory(type: Many<keyof ResourceMap | SomeResourceType>) {

0 commit comments

Comments
 (0)