feat: Add ts-morph for query resolution of fragments#32
Conversation
This introduces using ts-morph to parse and resolve extracted queries
from defineQuery() calls and groq`` template literals.
This allows for resolving queries which uses string interpolation
to embed fragments in the query.
Example:
const subQuery = defineQuery(`{ name }`)
const query = defineQuery(`{
_id,
author->${subQuery}
}`)
will resolve to:
{
_id,
author->{ name }
}
Since we leverage ts-morph to parse the extracted code, this will also
work if `subQuery` is defined in a separate file.
To use ts-morph we need to create a Project - this is stored in a cache
based on the ts-config file to ensure that we don't need to do all the
parsing on each iteration. The extractAllDefineQuery() will get the
source and path of the current file, and ensure that it is always up to
date in the Project.
We implement some logic to resolve the nearest ts config file from the
current file. This should hopefully ensure everything resolves correctly
in a mono repo.
I had to update Prettier to get it working on my machine.
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
|
Warning Review the following alerts detected in dependencies. According to your organization's Security Policy, it is recommended to resolve "Warn" alerts. Learn more about Socket for GitHub.
|
Description
This change allows putting query fragments into queries, and have them be resolved before the query is executed.
Given this code:
previously, the executed query would then be:
which is invalid GROQ. The changes in this PR will ensure the interpolated string is resolved before executed, so the full GROQ query which is executed is:
The query will also resolve if the
authoris imported from a different file.What to review
This PR changes to using ts-morph to extract, parse and resolve the queries. Most of the logic is the
resolve-queries.tsfile.Testing
There didn't seem to be much of a testing facility set up for this project. But happy to try to set something up if desirable.