-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: load and cache matches on server start
- Loading branch information
1 parent
43ed87b
commit 69c1d97
Showing
10 changed files
with
96 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,8 @@ | ||
import { normalizePath } from 'vite'; | ||
import type { CodegenContext } from '@graphql-codegen/cli'; | ||
import { getDocumentPaths, getSchemaPaths } from '../utils/configPaths'; | ||
|
||
export type FileMatcher = ( | ||
filePath: string, | ||
context: CodegenContext, | ||
) => Promise<boolean>; | ||
|
||
export const isCodegenConfig: FileMatcher = async (filePath, context) => { | ||
export function isCodegenConfig(filePath: string, context: CodegenContext) { | ||
if (!context.filepath) return false; | ||
|
||
return normalizePath(filePath) === normalizePath(context.filepath); | ||
}; | ||
|
||
export const isGraphQLDocument: FileMatcher = async (filePath, context) => { | ||
const documentPaths = await getDocumentPaths(context); | ||
|
||
if (!documentPaths.length) return false; | ||
|
||
const normalizedFilePath = normalizePath(filePath); | ||
|
||
return documentPaths.some((path) => normalizedFilePath.includes(path)); | ||
}; | ||
|
||
export const isGraphQLSchema: FileMatcher = async (filePath, context) => { | ||
const schemaPaths = await getSchemaPaths(context); | ||
|
||
if (!schemaPaths.length) return false; | ||
|
||
const normalizedFilePath = normalizePath(filePath); | ||
|
||
return schemaPaths.some((path) => normalizedFilePath.includes(path)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { CodegenContext } from '@graphql-codegen/cli'; | ||
import { getDocumentPaths, getSchemaPaths } from './configPaths'; | ||
import { normalizePath } from 'vite'; | ||
import type { Options } from '..'; | ||
|
||
export function createMatchCache( | ||
context: CodegenContext, | ||
options: Pick<Required<Options>, 'matchOnDocuments' | 'matchOnSchemas'>, | ||
) { | ||
const cache = new Set<string>(); | ||
|
||
const refresh = async () => { | ||
const matchers = [] as Promise<string[]>[]; | ||
if (options.matchOnDocuments) matchers.push(getDocumentPaths(context)); | ||
if (options.matchOnSchemas) matchers.push(getSchemaPaths(context)); | ||
|
||
const results = await Promise.all(matchers); | ||
|
||
const entries = results.flat().map(normalizePath); | ||
|
||
cache.clear(); | ||
|
||
for (const entry of entries) { | ||
cache.add(entry); | ||
} | ||
}; | ||
|
||
return { | ||
init: refresh, | ||
refresh, | ||
has: (filePath: string) => cache.has(normalizePath(filePath)), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters