Add query cache
WHY are these changes introduced?
Fixes https://github.com/Shopify/develop-app-inner-loop/issues/2021
WHAT is this pull request doing?
- Adds a cache mechanism during the life of the command to run any function
- Enables cache for some App Management API queries: fetchApp and specifications
- Re-uses the logic for the existing TOML config caching
The idea is to enable the cache for more queries later, including Partners, once we ensure it works as expected. The two enabled right now are safe, as we don't expect them to change during a command run.
Results
First deploy: 6 activeAppRelease and 2 fetchSpecifications queries, network time: 17959ms First deploy with cache: 1 activeAppRelease and 1 fetchSpecifications queries, network time: 9910ms
Next deploy: 4 activeAppRelease queries, network time: 5683ms Next deploy with cache: 1 activeAppRelease query, network time: 3526ms
How to test your changes?
USE_APP_MANAGEMENT_API=1 p shopify app deploy
Measuring impact
How do we know this change was effective? Please choose one:
- [x] n/a - this doesn't need measurement, e.g. a linting rule or a bug-fix
- [ ] Existing analytics will cater for this addition
- [ ] PR includes analytics changes to measure impact
Checklist
- [x] I've considered possible cross-platform impacts (Mac, Linux, Windows)
- [x] I've considered possible documentation changes
Thanks for your contribution!
Depending on what you are working on, you may want to request a review from a Shopify team:
- Themes: @shopify/advanced-edits
- UI extensions: @shopify/ui-extensions-cli
- Checkout UI extensions: @shopify/checkout-ui-extensions-api-stewardship
- Hydrogen: @shopify/hydrogen
- Other: @shopify/app-inner-loop
Coverage report
St.:grey_question: |
Category | Percentage | Covered / Total |
|---|---|---|---|
| π‘ | Statements | 72.94% (+0.21% πΌ) |
8271/11340 |
| π‘ | Branches | 69.38% (-0.03% π») |
4036/5817 |
| π‘ | Functions | 71.65% (+0.17% πΌ) |
2156/3009 |
| π‘ | Lines | 73.31% (+0.25% πΌ) |
7828/10678 |
Show new covered files π£
St.:grey_question: |
File | Statements | Branches | Functions | Lines |
|---|---|---|---|---|---|
| π’ | ... / command-cache.ts |
81.48% | 43.48% | 100% | 88% |
Show files with reduced coverage π»
St.:grey_question: |
File | Statements | Branches | Functions | Lines |
|---|---|---|---|---|---|
| π’ | ... / app-management.ts |
100% | 62.5% (-12.5% π») |
100% | 100% |
Test suite run success
1868 tests passing in 851 suites.
Report generated by π§ͺjest coverage report action from 20275c096960b05664ddb866ff700e9a55fd0634
We detected some changes at either packages/*/src or packages/cli-kit/assets/cli-ruby/** and there are no updates in the .changeset. If the changes are user-facing, run "pnpm changeset add" to track your changes and include them in the next release CHANGELOG.
Differences in type declarations
We detected differences in the type declarations generated by Typescript for this branch compared to the baseline ('main' branch). Please, review them to ensure they are backward-compatible. Here are some important things to keep in mind:
- Some seemingly private modules might be re-exported through public modules.
- If the branch is behind
mainyou might see odd diffs, rebasemaininto this branch.
New type declarations
packages/cli-kit/dist/public/node/command-cache.d.ts
export interface CommandCacheOptions {
/**
* ID of the command to read data from. Defaults to the SHOPIFY_CLI_COMMAND_RUN_ID environment variable.
*/
commandId?: string;
/**
* Folder to store the cache in. Defaults to the Conf folder: https://github.com/sindresorhus/env-paths#pathsconfig.
*/
cwd?: string;
}
/**
* Saves data to the command cache.
*
* @param data - Key-value pairs to save.
* @param options - Options for saving the data.
*/
export declare function setCachedCommandInfo(data: {
[key: string]: unknown;
}, options?: CommandCacheOptions): void;
/**
* Reads data from the command cache.
*
* @param options - Options for reading the data.
* @returns The data if exists or undefined.
*/
export declare function getCachedCommandInfo(options?: CommandCacheOptions): {
[key: string]: unknown;
} | undefined;
/**
* Clears the command cache.
*
* @param cwd - Current working directory where the cache is stored.
*/
export declare function clearCachedCommandInfo(cwd?: string | undefined): void;
/**
* Runs a function or returns the cached result if it was already run during the current command.
*
* @param cacheKey - String to use as a key for caching.
* @param fn - Function to run if the cache key is not found, and cache the result of.
* @param options - Options for the cache store.
* @returns The result of the function.
*/
export declare function runWithCommandCache<T>(cacheKey: string, fn: () => T, options?: CommandCacheOptions): Promise<T>;
Existing type declarations
packages/cli-kit/dist/public/node/api/app-management.d.ts
@@ -1,14 +1,20 @@
import { GraphQLVariables, GraphQLResponse } from './graphql.js';
+export interface AppManagementRequestOptions {
+ variables?: GraphQLVariables;
+ cacheEnabled?: boolean;
+}
/**
* Executes an org-scoped GraphQL query against the App Management API.
*
* @param orgId - The organization ID.
* @param query - GraphQL query to execute.
* @param token - Partners token.
- * @param variables - GraphQL variables to pass to the query.
+ * @param options - Additional options for the request:
+ * - variables - GraphQL variables to pass to the query.
+ * - cacheEnabled: Whether to cache the result of the query during the command execution. Disabled by default.
* @returns The response of the query of generic type <T>.
*/
-export declare function appManagementRequest<T>(orgId: string, query: string, token: string, variables?: GraphQLVariables): Promise<T>;
+export declare function appManagementRequest<T>(orgId: string, query: string, token: string, options?: AppManagementRequestOptions): Promise<T>;
/**
* Sets the next deprecation date from [GraphQL response extensions](https://www.apollographql.com/docs/resources/graphql-glossary/#extensions)
* if objects contain a (ISO 8601-formatted string).
This PR seems inactive. If it's still relevant, please add a comment saying so. Otherwise, take no action. β If there's no activity within a week, then a bot will automatically close this. Thanks for helping to improve Shopify's dev tooling and experience.
The refactor initiated with https://github.com/Shopify/cli/pull/4526 should help avoiding some duplicated queries, so I'm closing here, as we are not sure about this approach.