Skip to content

Commit

Permalink
chore: cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-0acf4 committed Feb 5, 2025
1 parent c847764 commit 68542fd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
20 changes: 10 additions & 10 deletions src/typegate/src/runtimes/typegraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export type DeprecatedArg = { includeDeprecated?: boolean };
export class TypeGraphRuntime extends Runtime {
tg: TypeGraphDS;
#typeGen: IntrospectionTypeEmitter | null = null;
#visibility: TypeVisibility | null = null;

private constructor(tg: TypeGraphDS) {
super(TypeGraph.formatName(tg));
Expand Down Expand Up @@ -63,18 +62,22 @@ export class TypeGraphRuntime extends Runtime {
];
}

initFormatter(
initTypeGenerator(
config: TypegateConfigBase,
denoRuntime: DenoRuntime,
) {
this.#visibility = new TypeVisibility(this.tg, denoRuntime, config);
this.#typeGen = new IntrospectionTypeEmitter(
this.tg,
this.#visibility,
new TypeVisibility(this.tg, denoRuntime, config),
);
}

get #types() {
if (!this.#typeGen) {
// Unreachable
throw new Error("Invalid state: type generator not initialized");
}

Check warning on line 79 in src/typegate/src/runtimes/typegraph.ts

View check run for this annotation

Codecov / codecov/patch

src/typegate/src/runtimes/typegraph.ts#L77-L79

Added lines #L77 - L79 were not covered by tests

return this.#typeGen!;
}

Expand Down Expand Up @@ -110,8 +113,8 @@ export class TypeGraphRuntime extends Runtime {
// https://github.com/graphql/graphql-js/blob/main/src/type/introspection.ts#L36
description: () => `${root.type} typegraph`,

Check warning on line 114 in src/typegate/src/runtimes/typegraph.ts

View check run for this annotation

Codecov / codecov/patch

src/typegate/src/runtimes/typegraph.ts#L114

Added line #L114 was not covered by tests
types: () => this.#typesResolver(args),
queryType: () => this.#types?.getRootSchema("Query"),
mutationType: this.#types?.getRootSchema("Mutation"),
queryType: () => this.#types.getRootSchema("Query"),
mutationType: this.#types.getRootSchema("Mutation"),
subscriptionType: () => null,
directives: () => [],
};
Expand All @@ -127,10 +130,7 @@ export class TypeGraphRuntime extends Runtime {

#withPrecomputeVisibility(resolver: Resolver): Resolver {
return async (args) => {
this.#visibility!.reset();
await this.#visibility!.preComputeAllPolicies(args ?? {});

this.#types.reset();
await this.#types.resetComputations(args);
this.#types.emitRoot();

return resolver(args);
Expand Down
17 changes: 11 additions & 6 deletions src/typegate/src/runtimes/typegraph/type_emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
typeGenericCustomScalar,
} from "./helpers.ts";
import { TypeKind } from "graphql";
import { Resolver } from "../../types.ts";
import { Resolver, ResolverArgs } from "../../types.ts";
import { getLogger } from "../../log.ts";
import { FunctionNode } from "../../typegraph/type_node.ts";
import { ensure } from "../../utils.ts";
Expand Down Expand Up @@ -55,7 +55,12 @@ export class IntrospectionTypeEmitter {
this.#typesDefined = new Set();
}

reset() {
async resetComputations(args: ResolverArgs) {
if (this.visibility) {
this.visibility!.reset();
await this.visibility!.preComputeAllPolicies(args ?? {});
}

this.#types = [];
this.#typesDefined = new Set();
}
Expand Down Expand Up @@ -185,6 +190,8 @@ export class IntrospectionTypeEmitter {
return {
// Note: this is arbitrary, injections and policies will reduce the field
// thus making a completely different type
// Alternatively, we can return null and make the parent itself invisible (we don't emit an empty object scalar)
// but in a way the parent field is accessible
adhocId: originalEntries.length == entries.length
? ""
: `_f${entries.length == 0 ? "empty" : entries.length}`,
Expand Down Expand Up @@ -278,17 +285,15 @@ export class IntrospectionTypeEmitter {

gctx.path.push(type.title);

let ret = null;
if (isScalar(type)) {
ret = this.#emitScalar(type, gctx);
this.#emitScalar(type, gctx);
} else if (isObject(type)) {
ret = this.#emitObject(type, gctx);
this.#emitObject(type, gctx);
} else {
throw new Error(`Unhandled "${type.type}" of title ${type.title}`);

Check warning on line 293 in src/typegate/src/runtimes/typegraph/type_emitter.ts

View check run for this annotation

Codecov / codecov/patch

src/typegate/src/runtimes/typegraph/type_emitter.ts#L293

Added line #L293 was not covered by tests
}

gctx.path.pop();
return ret;
}

#emitWrapperAndReturnSchema(
Expand Down
2 changes: 1 addition & 1 deletion src/typegate/src/typegate/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ export class Typegate implements AsyncDisposable {
typegraph: typegraphRuntime,
},
);
(typegraphRuntime as TypeGraphRuntime).initFormatter(
(typegraphRuntime as TypeGraphRuntime).initTypeGenerator(
this.config.base,
introspectionRuntimeRefData.denoRuntime as DenoRuntime,
);
Expand Down

0 comments on commit 68542fd

Please sign in to comment.