Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli-utils): Fix turbo command reusing its own prior cache output #208

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/cool-dolphins-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"gql.tada": patch
"@gql.tada/cli-utils": patch
---

Fix `turbo` command reusing previously cached turbo typings. Instead, we now set a flag to disable the cache temporarily inside the command.
27 changes: 24 additions & 3 deletions packages/cli-utils/src/commands/turbo/thread.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'node:path';
import { Project, TypeFormatFlags, TypeFlags, ts } from 'ts-morph';
import { Project, TypeFormatFlags, TypeFlags, ScriptKind, ts } from 'ts-morph';

import type { GraphQLSPConfig } from '@gql.tada/internal';
import { init } from '@0no-co/graphqlsp/api';
Expand All @@ -19,8 +19,19 @@ async function* _runTurbo(params: TurboParams): AsyncIterableIterator<TurboSigna
init({ typescript: ts as any });

const projectPath = path.dirname(params.configPath);
const project = new Project({ tsConfigFilePath: params.configPath });
const checker = project.getTypeChecker().compilerObject;
const project = new Project({
tsConfigFilePath: params.configPath,
skipAddingFilesFromTsConfig: true,
});

// NOTE: We add our override declaration here before loading all files
// This sets `__cacheDisabled` on the turbo cache, which disables the cache temporarily
// If we don't disable the cache then we couldn't regenerate it from inferred types
project.createSourceFile('__gql-tada-override__.d.ts', DECLARATION_OVERRIDE, {
overwrite: true,
scriptKind: ScriptKind.TS,
});
project.addSourceFilesFromTsConfig(params.configPath);

// Filter source files by whether they're under the relevant root path
const sourceFiles = project.getSourceFiles().filter((sourceFile) => {
Expand All @@ -34,6 +45,7 @@ async function* _runTurbo(params: TurboParams): AsyncIterableIterator<TurboSigna
fileCount: sourceFiles.length,
};

const checker = project.getTypeChecker().compilerObject;
for (const { compilerNode: sourceFile } of sourceFiles) {
const filePath = sourceFile.fileName;
const cache: Record<string, string> = {};
Expand Down Expand Up @@ -87,6 +99,15 @@ const BUILDER_FLAGS: TypeFormatFlags =
TypeFormatFlags.AllowUniqueESSymbolType |
TypeFormatFlags.WriteTypeArgumentsOfSignature;

const DECLARATION_OVERRIDE = `
import * as _gqlTada from 'gql.tada';
declare module 'gql.tada' {
interface setupCache {
readonly __cacheDisabled: true;
}
}
`.trim();

function findAllCallExpressions(
sourceFile: ts.SourceFile,
config: GraphQLSPConfig
Expand Down
10 changes: 9 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ interface setupSchema extends AbstractSetupSchema {
}

interface AbstractSetupCache {
readonly __cacheDisabled: unknown;
[key: string]: unknown;
}

Expand Down Expand Up @@ -134,7 +135,14 @@ interface GraphQLTadaAPI<Schema extends SchemaLike, Config extends AbstractConfi
input: In,
fragments?: Fragments
): setupCache[In] extends DocumentNodeLike
? setupCache[In]
? unknown extends setupCache['__cacheDisabled']
? setupCache[In]
: getDocumentNode<
parseDocument<In>,
Schema,
getFragmentsOfDocuments<Fragments>,
Config['isMaskingDisabled']
>
: getDocumentNode<
parseDocument<In>,
Schema,
Expand Down
Loading