Mirror: TypeScript LSP plugin that finds GraphQL documents in your code and provides diagnostics, auto-complete and hover-information.

allow adding more reserved keys for field usage tracking (#195)

authored by

Jovi De Croock and committed by
GitHub
d6c8d628 e98d1be1

+13 -3
+5
.changeset/lemon-snakes-bow.md
··· 1 + --- 2 + '@0no-co/graphqlsp': minor 3 + --- 4 + 5 + Add way to provide additional reserved keys for field-usage tracking by means of the `reservedKeys` config property which accepts an array of strings
+3 -1
packages/graphqlsp/README.md
··· 69 69 unused fields within the same file. (only works with call-expressions, default: true) 70 70 - `tadaOutputLocation` when using `gql.tada` this can be convenient as it automatically generates 71 71 an `introspection.ts` file for you, just give it the directory to output to and you're done 72 - 72 + - `reservedKeys` this setting will affect `trackFieldUsage`, you can enter keys here that will be ignored 73 + from usage tracking, so when they are unused in the component but used in i.e. the normalised cache you 74 + won't get annoying warnings. (default `id`, `_id` and `__typename`, example: ['slug']) 73 75 74 76 ## Tracking unused fields 75 77
+5 -2
packages/graphqlsp/src/fieldUsage.ts
··· 255 255 const shouldTrackFieldUsage = info.config.trackFieldUsage ?? true; 256 256 if (!shouldTrackFieldUsage) return diagnostics; 257 257 258 + const defaultReservedKeys = ['id', '_id', '__typename']; 259 + const additionalKeys = info.config.reservedKeys ?? []; 260 + const reservedKeys = new Set([...defaultReservedKeys, ...additionalKeys]); 261 + 258 262 try { 259 263 nodes.forEach(node => { 260 264 const nodeText = node.getText(); ··· 275 279 const allAccess: string[] = []; 276 280 const inProgress: string[] = []; 277 281 const allPaths: string[] = []; 278 - const reserved = ['id', '__typename']; 279 282 const fieldToLoc = new Map<string, { start: number; length: number }>(); 280 283 // This visitor gets all the leaf-paths in the document 281 284 // as well as all fields that are part of the document ··· 285 288 visit(parse(node.getText().slice(1, -1)), { 286 289 Field: { 287 290 enter: node => { 288 - if (!node.selectionSet && !reserved.includes(node.name.value)) { 291 + if (!node.selectionSet && !reservedKeys.has(node.name.value)) { 289 292 let p; 290 293 if (inProgress.length) { 291 294 p = inProgress.join('.') + '.' + node.name.value;