-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reload schema/documents cache (only for **current project**) in…
… VSCode (#1222) * feat: reload schema/documents cache (only for **current project**) in VSCode * Update packages/plugin/src/documents.ts * Update packages/plugin/src/schema.ts
- Loading branch information
1 parent
8568313
commit cf59b0a
Showing
13 changed files
with
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-eslint/eslint-plugin': minor | ||
--- | ||
|
||
feat: reload schema/documents cache (only for **current project**) in VSCode |
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,5 +1,10 @@ | ||
{ | ||
"$schema": "https://docs.renovatebot.com/renovate-schema.json", | ||
"extends": ["github>the-guild-org/shared-config:renovate"], | ||
"automerge": true | ||
"lockFileMaintenance": { | ||
"enabled": true, | ||
"automerge": true, | ||
"automergeType": "pr", | ||
"platformAutomerge": true | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Based on the `eslint-plugin-import`'s cache | ||
// https://github.com/import-js/eslint-plugin-import/blob/main/utils/ModuleCache.js | ||
import debugFactory from 'debug'; | ||
|
||
const log = debugFactory('graphql-eslint:ModuleCache'); | ||
|
||
export class ModuleCache<T, K = any> { | ||
map = new Map<K, { lastSeen: [number, number]; result: T }>(); | ||
|
||
set(cacheKey: K, result: T): void { | ||
this.map.set(cacheKey, { lastSeen: process.hrtime(), result }); | ||
log('setting entry for', cacheKey); | ||
} | ||
|
||
get(cacheKey, settings = { lifetime: 10 /* seconds */ }): void | T { | ||
if (!this.map.has(cacheKey)) { | ||
log('cache miss for', cacheKey); | ||
return; | ||
} | ||
const { lastSeen, result } = this.map.get(cacheKey); | ||
// check freshness | ||
if (process.hrtime(lastSeen)[0] < settings.lifetime) { | ||
return result; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
import { GraphQLSchema } from 'graphql'; | ||
import { GraphQLProjectConfig } from 'graphql-config'; | ||
import { asArray } from '@graphql-tools/utils'; | ||
import debugFactory from 'debug'; | ||
import fastGlob from 'fast-glob'; | ||
import fg from 'fast-glob'; | ||
import chalk from 'chalk'; | ||
import type { ParserOptions, Schema, Pointer } from './types'; | ||
import { ParserOptions, Schema, Pointer } from './types'; | ||
import { ModuleCache } from './cache'; | ||
|
||
const schemaCache = new Map<string, GraphQLSchema | Error>(); | ||
const schemaCache = new ModuleCache<GraphQLSchema | Error>(); | ||
const debug = debugFactory('graphql-eslint:schema'); | ||
|
||
export function getSchema( | ||
project: GraphQLProjectConfig, | ||
options: Omit<ParserOptions, 'filePath'> = {}, | ||
schemaOptions?: ParserOptions['schemaOptions'], | ||
): Schema { | ||
const schemaKey = asArray(project.schema).sort().join(','); | ||
const schemaKey = project.schema; | ||
|
||
if (!schemaKey) { | ||
return null; | ||
} | ||
|
||
if (schemaCache.has(schemaKey)) { | ||
return schemaCache.get(schemaKey); | ||
const cache = schemaCache.get(schemaKey); | ||
|
||
if (cache) { | ||
return cache; | ||
} | ||
|
||
let schema: Schema; | ||
|
||
try { | ||
debug('Loading schema from %o', project.schema); | ||
schema = project.loadSchemaSync(project.schema, 'GraphQLSchema', { | ||
...options.schemaOptions, | ||
...schemaOptions, | ||
pluckConfig: project.extensions.pluckConfig, | ||
}); | ||
if (debug.enabled) { | ||
debug('Schema loaded: %o', schema instanceof GraphQLSchema); | ||
const schemaPaths = fastGlob.sync(project.schema as Pointer, { | ||
absolute: true, | ||
}); | ||
const schemaPaths = fg.sync(project.schema as Pointer, { absolute: true }); | ||
debug('Schema pointers %O', schemaPaths); | ||
} | ||
// Do not set error to cache, since cache reload will be done after some `lifetime` seconds | ||
schemaCache.set(schemaKey, schema); | ||
} catch (error) { | ||
error.message = chalk.red(`Error while loading schema: ${error.message}`); | ||
schema = error as Error; | ||
} | ||
|
||
schemaCache.set(schemaKey, schema); | ||
return schema; | ||
} |
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