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

feat: support array destructuring of result lists (#204)

Changed files
+36
.changeset
packages
example-tada
src
graphqlsp
+5
.changeset/flat-cherries-report.md
···
···
+
---
+
'@0no-co/graphqlsp': minor
+
---
+
+
Support array destructuring result lists
+4
packages/example-tada/src/index.tsx
···
maxCP
maxHP
types
}
}
`, [PokemonFields, Fields.Pokemon]);
···
variables: { id: '' }
});
const selected = result.data?.pokemons?.at(0)!
console.log(result.data?.pokemons?.at(0)?.maxCP)
console.log(selected.maxHP)
···
maxCP
maxHP
types
+
fleeRate
}
}
`, [PokemonFields, Fields.Pokemon]);
···
variables: { id: '' }
});
+
// @ts-expect-error
+
const [sel] = result.data?.pokemons;
+
console.log(sel.fleeRate)
const selected = result.data?.pokemons?.at(0)!
console.log(result.data?.pokemons?.at(0)?.maxCP)
console.log(selected.maxHP)
+27
packages/graphqlsp/src/fieldUsage.ts
···
return node;
};
const traverseDestructuring = (
node: ts.ObjectBindingPattern,
originalWip: Array<string>,
···
// Crawl down until we have either a leaf node or an object/array that can
// be recrawled
return traverseDestructuring(
foundRef.name,
pathParts,
allFields,
···
return node;
};
+
const traverseArrayDestructuring = (
+
node: ts.ArrayBindingPattern,
+
originalWip: Array<string>,
+
allFields: Array<string>,
+
source: ts.SourceFile,
+
info: ts.server.PluginCreateInfo
+
): Array<string> => {
+
return node.elements.flatMap(element => {
+
if (ts.isOmittedExpression(element)) return [];
+
+
const wip = [...originalWip];
+
return ts.isIdentifier(element.name)
+
? crawlScope(element.name, wip, allFields, source, info)
+
: ts.isObjectBindingPattern(element.name)
+
? traverseDestructuring(element.name, wip, allFields, source, info)
+
: traverseArrayDestructuring(element.name, wip, allFields, source, info);
+
});
+
};
+
const traverseDestructuring = (
node: ts.ObjectBindingPattern,
originalWip: Array<string>,
···
// Crawl down until we have either a leaf node or an object/array that can
// be recrawled
return traverseDestructuring(
+
foundRef.name,
+
pathParts,
+
allFields,
+
source,
+
info
+
);
+
} else if (ts.isArrayBindingPattern(foundRef.name)) {
+
return traverseArrayDestructuring(
foundRef.name,
pathParts,
allFields,