Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Added claude skills & some rules (#18871)
Lukasz Ostrowskifb59f151cf0f
diff --git a/.claude/skills/saleor-django-migration/SKILL.md b/.claude/skills/saleor-django-migration/SKILL.mdnew file mode 100644index 0000000000..f002eff3e0--- /dev/null+++ b/.claude/skills/saleor-django-migration/SKILL.md@@ -0,0 +1,84 @@+---+name: saleor-django-migration+description: Generate Django schema migrations for the Saleor codebase using manage.py makemigrations. Use when (1) a new Django model is created, (2) model fields are added/altered/removed, (3) user explicitly asks to create a migration. This skill covers ONLY synchronous schema migrations (CreateModel, AddField, AlterField, RemoveField, etc.) — NOT data migrations with RunPython or RunSQL. If unsure whether the migration is schema-only, ask the user.+---++# Saleor Django Schema Migration++## Scope++This skill handles **schema-only** migrations: CreateModel, AddField, AlterField, RemoveField, AddIndex, AddConstraint, etc. It does NOT cover:+- Data migrations (RunPython, RunSQL with data changes)+- Async/background migrations (celery tasks, post_migrate signals)++If the change requires data transformation, stop and ask the user.++## Critical Rules++1. **NEVER write migration files by hand.** Always let Django generate them.+2. **ALWAYS use `./manage.py makemigrations`** to generate migrations.+3. **Activate the virtual environment first** — run `. .venv/bin/activate` before any manage.py command.+4. **If database connection fails**, stop and ask the user to start the database.++## Workflow++### Step 1: Make model changes++Edit the Django model file(s) as needed. Do not touch any migration files.++### Step 2: Generate migration++```bash+. .venv/bin/activate && python manage.py makemigrations+```++If Django asks interactive questions (e.g. "Did you rename field X to Y?"), answer based on the model changes you made.++### Step 3: Read and analyze the generated migration++Read every generated migration file. Verify:+- It contains only schema operations (CreateModel, AddField, AlterField, RemoveField, AddIndex, etc.)+- Dependencies look correct+- No unexpected operations++### Step 4: Apply separation rules++Apply these rules to the generated migration(s):++**Rule A — One model per migration for new models:**+If the migration creates MORE THAN ONE new model (multiple `CreateModel` operations), split them. Generate each separately:+```bash+python manage.py makemigrations <app>+```+If Django generates them together, delete the combined migration, then create models one at a time (add one model to models.py, run makemigrations, repeat).++**Rule B — One field change per migration:**+If the migration alters MORE THAN ONE field within a single model (e.g. two `AddField` or two `AlterField` operations on the same model), split into separate migrations — one per field. Delete the combined migration, make one field change at a time, and run makemigrations after each.++**Exceptions (do NOT split):**+- A `CreateModel` with all its fields is one operation — do not split the fields of a new model+- Cross-model dependencies where Django requires them together (e.g. ForeignKey + related model creation)+- Field + its index/constraint in the same migration is acceptable when auto-generated together++### Step 5: Verify++Run the check command to confirm no pending migrations remain:+```bash+python manage.py makemigrations --check --dry-run+```++Expected output: `No changes detected` — meaning all model changes are captured in migrations.++## Error Handling++| Error | Action |+|-------|--------|+| `django.db.utils.OperationalError: could not connect` | Ask user to start the database |+| `Conflicting migrations detected` | Stop and let user decide |+| `ModuleNotFoundError` | Virtual environment not activated — run `. .venv/bin/activate` |++## Tips++- Rely on autogenerated migrations for correct timestamps, sequential numbers, dependencies, and field definitions. If you must hand-edit (e.g. to split), regenerate rather than edit — delete the migration, adjust models.py, and re-run makemigrations.+- When splitting, let Django figure out dependencies between the new smaller migrations.+- Check the newest existing migration in the app (`ls saleor/<app>/migrations/ | tail -5`) before generating, so you can confirm the new migration number is sequential.diff --git a/saleor/graphql/AGENTS.md b/saleor/graphql/AGENTS.mdnew file mode 100644index 0000000000..69ffef0ea5--- /dev/null+++ b/saleor/graphql/AGENTS.md@@ -0,0 +1,72 @@+This folder contains graphQL layer++**Each mutation must have its own error class.** Never reuse error classes between mutations.+Each mutation gets a dedicated error type so clients can handle errors specifically and new error codes can be added without affecting other mutations.++**Always tag new mutations and fields with `ADDED_IN_{VERSION}`.** Check the latest git tag to determine the current version.+Every new type, field, mutation, and input must include the version annotation in its description so the API changelog is generated correctly.++**Limit input list sizes to a maximum of 100 per call (unless specified otherwise).** Unbounded lists in mutation inputs can lead to OOM errors, timeouts, and expensive queries.+Apply the same limit pattern used in queries (max 100 items) and document the limit in the field description.++**Register query cost multipliers for limited/paginated fields.** When adding a new field with a `limit` argument, update `saleor/graphql/query_cost_map.py` so the `limit` value is used as a cost multiplier. This prevents expensive queries from bypassing rate limiting.+++**Use `Meta.permissions` rather than manual permission checks in `perform_mutation`.** The `BaseMutation.mutate` method already calls `check_permissions` using the permissions declared in `Meta`.+Adding manual permission checks in `perform_mutation` is redundant and can diverge from the declared permissions.++**Return the created or mutated object from mutations.** Callers may need the created object's ID or fields immediately after the mutation. Always include the result object in the mutation response type.++**Use dataloader pattern when resolving fields**++```python+@staticmethod+def resolve_parent(root: models.Category, info):+ if root.parent_id:+ return CategoryByIdLoader(info.context).load(root.parent_id)+ return None+```++**Regenerate graphql schema** when graphql types changes, run `python manage.py graphql_schema --schema saleor/graphql/schema.graphql` to regenerate schema++**Ensure you use existing scalars**++**Write exhaustive GraphQL descriptions on fields, including limits and behavior**++# Input validation++For complex input shapes prefer using Pydantic model. Ensure Pydantic errors are mapped to Django ValidationError using `pydantic_to_validation_error` from `saleor/graphql/error.py`:++```python+try:+ validated = MyValidatedInput(**input_data)+except PydanticValidationError as exc:+ raise pydantic_to_validation_error(+ exc, error_code=MyErrorCodeEnum.INVALID.value+ ) from exc+```++# Adding new filter++- Create new DB index on django model for each filtered field (add index concurrently)++Example migration:++```python+import django.contrib.postgres.indexes+from django.contrib.postgres.operations import AddIndexConcurrently+from django.db import migrations+class Migration(migrations.Migration):+ atomic = False+ dependencies = [+ (“account”, “0095_repopulate_user_number_of_orders”),+ ]+ operations = [+ AddIndexConcurrently(+ model_name=“transactionitem”,+ index=django.contrib.postgres.indexes.BTreeIndex(+ fields=[“psp_reference”], name=“psp_reference_idx”+ ),+ ),+ ]+```diff --git a/saleor/graphql/CLAUDE.md b/saleor/graphql/CLAUDE.mdnew file mode 120000index 0000000000..47dc3e3d86--- /dev/null+++ b/saleor/graphql/CLAUDE.md@@ -0,0 +1 @@+AGENTS.md\ No newline at end of file