Skip to content

Generate typed prepare variables from run queries - #138

Closed
saga-dasgupta wants to merge 1 commit into
mainfrom
saga/typed-prepare-variables
Closed

saga-dasgupta wants to merge 1 commit into
mainfrom
saga/typed-prepare-variables

Conversation

@saga-dasgupta

@saga-dasgupta saga-dasgupta commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Why

A prepare target returns variables for its run target's input query as untyped JSON. The generated prepare result therefore doesn't catch a misspelled key, missing required variable, or incompatible value.

This explores the type-generation approach from Shopify/shopify-function-rust#252 for JavaScript functions.

What

Adds an opt-in @shopify/shopify_function/prepare-variables.cjs codegen plugin with explicit schema-field → named-query mappings:

{
  "prepareVariables": {
    "CartValidationsGeneratePrepareResult.variables": "CartValidationsGenerateRun"
  }
}

The plugin replaces both typescript and typescript-operations for the output. It composes those generators, types only the mapped JSON result fields with the matching query's variables type, and preserves unrelated JSON fields. GraphQL Codegen 6's operations generator emits its own used input-object and enum declarations, so overlapping schema declarations and repeated custom-scalar imports are reconciled.

Includes:

  • README setup instructions and TypeScript/JavaScript JSDoc examples.
  • Compiler-based tests for required/defaulted/nullable variables, query edits, nested input objects, enums, multiple mappings, naming options, and custom scalar imports.
  • An integration test loading the plugin through the codegen CLI by its package subpath.
  • Test CI and a minor-release changeset.

Scope and limitations

  • This is type generation, not mandatory deploy validation. TypeScript checking must run separately; JavaScript needs annotations and checkJs, and null checking needs strictNullChecks.
  • TypeScript's usual escape hatches and structural-assignment behavior still apply.
  • Pairings are explicit; this does not read extension TOML or infer prepare/run targets.
  • No runtime or CLI behavior changes.
  • Mapped fields must be JSON or JSON! on object types and reference named query operations included in codegen's documents.
  • globalNamespace, noExport, onlyEnums, and onlyOperationTypes are not supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant