Skip to content

Commit

Permalink
fix(cli-utils): Fix turbo command reusing its own prior cache output (
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten authored Apr 15, 2024
1 parent c9c3d0f commit 251b9b0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
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

0 comments on commit 251b9b0

Please sign in to comment.