amplify-disable-unused-ops
v0.2.0
Published
A CLI tool that automatically detects and disables unused GraphQL operations in AWS Amplify Gen2 projects.
Readme
amplify-disable-unused-ops
A CLI tool that automatically detects and disables unused GraphQL operations in AWS Amplify Gen2 projects.
Overview
This tool scans TypeScript projects to identify actually used Amplify DataClient operations (query/mutation/subscription) and disables unused operations using disableOperations(), preventing the generation of unnecessary API endpoints.
Key Features
- Usage Scanning: Analyzes TypeScript files in your project to detect which models and operations are actually being used
- Automatic Disabling: Automatically disables unused operations with
disableOperations() - Backup: Automatically creates backups of resource files before making changes (optional)
- Dry Run: Preview changes before actually applying them
Installation
npm add -D amplify-disable-unused-opsUsage
1. Scan Usage
Scan TypeScript files in your project to detect which Amplify operations are being used.
amplify-disable-unused-ops scan --project tsconfig.json --out usage.jsonOptions:
--project: Path to the TypeScript project configuration file (tsconfig.json)--out: Path to the JSON file where usage information will be written
Example output (usage.json):
{
"Todo": ["create", "list"],
"User": ["get", "update"]
}2. Disable Unused Operations
Update the Amplify resource file to disable unused operations based on the scan results.
amplify-disable-unused-ops apply --resource amplify/data/resource.ts --usage usage.jsonOptions:
--resource: Path to the Amplify resource definition file (resource.ts)--usage: Path to the usage JSON file generated by the scan command--dry-run: Preview changes without actually modifying files--no-backup: Skip creating a backup file (backup is created by default)
Dry run example:
amplify-disable-unused-ops apply --resource amplify/data/resource.ts --usage usage.json --dry-runOperation Types
This tool detects and manages the following operation types:
Query Operations
get: Retrieve a single recordlist: Retrieve multiple records
Mutation Operations
create: Create a recordupdate: Update a recorddelete: Delete a record
Subscription Operations
onCreate: Real-time notification on record creationonUpdate: Real-time notification on record updateonDelete: Real-time notification on record deletionobserveQuery: Real-time monitoring of query results
How It Works
- Scan Phase: Uses
ts-morphto parse the TypeScript AST and track client instances generated bygenerateClient() - Analysis Phase: Detects operations called in the form
client.models.ModelName.operation() - Apply Phase: Disables unused operation groups (queries/mutations/subscriptions) using
disableOperations()based on detected usage
Tech Stack
- TypeScript: Type-safe development
- ts-morph: TypeScript AST parsing and manipulation
- Node.js: Runtime environment
Development
Build
npm run buildRun
npm startLicense
MIT
