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)

Changed files
+13 -3
.changeset
packages
graphqlsp
+5
.changeset/lemon-snakes-bow.md
···
+
---
+
'@0no-co/graphqlsp': minor
+
---
+
+
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
···
unused fields within the same file. (only works with call-expressions, default: true)
- `tadaOutputLocation` when using `gql.tada` this can be convenient as it automatically generates
an `introspection.ts` file for you, just give it the directory to output to and you're done
-
+
- `reservedKeys` this setting will affect `trackFieldUsage`, you can enter keys here that will be ignored
+
from usage tracking, so when they are unused in the component but used in i.e. the normalised cache you
+
won't get annoying warnings. (default `id`, `_id` and `__typename`, example: ['slug'])
## Tracking unused fields
+5 -2
packages/graphqlsp/src/fieldUsage.ts
···
const shouldTrackFieldUsage = info.config.trackFieldUsage ?? true;
if (!shouldTrackFieldUsage) return diagnostics;
+
const defaultReservedKeys = ['id', '_id', '__typename'];
+
const additionalKeys = info.config.reservedKeys ?? [];
+
const reservedKeys = new Set([...defaultReservedKeys, ...additionalKeys]);
+
try {
nodes.forEach(node => {
const nodeText = node.getText();
···
const allAccess: string[] = [];
const inProgress: string[] = [];
const allPaths: string[] = [];
-
const reserved = ['id', '__typename'];
const fieldToLoc = new Map<string, { start: number; length: number }>();
// This visitor gets all the leaf-paths in the document
// as well as all fields that are part of the document
···
visit(parse(node.getText().slice(1, -1)), {
Field: {
enter: node => {
-
if (!node.selectionSet && !reserved.includes(node.name.value)) {
+
if (!node.selectionSet && !reservedKeys.has(node.name.value)) {
let p;
if (inProgress.length) {
p = inProgress.join('.') + '.' + node.name.value;