cli icon indicating copy to clipboard operation
cli copied to clipboard

Add query cache

Open gonzaloriestra opened this issue 1 year ago β€’ 4 comments

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

gonzaloriestra avatar Aug 14 '24 11:08 gonzaloriestra

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

github-actions[bot] avatar Aug 14 '24 11:08 github-actions[bot]

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

github-actions[bot] avatar Aug 14 '24 11:08 github-actions[bot]

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.

github-actions[bot] avatar Aug 21 '24 14:08 github-actions[bot]

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 main you might see odd diffs, rebase main into 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).

github-actions[bot] avatar Aug 29 '24 14:08 github-actions[bot]

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.

github-actions[bot] avatar Sep 30 '24 03:09 github-actions[bot]

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.

gonzaloriestra avatar Sep 30 '24 11:09 gonzaloriestra