Skip to content

Type prepare results with the run query's variables struct - #252

Open
saga-dasgupta wants to merge 2 commits into
mainfrom
prepare-run-variables
Open

saga-dasgupta wants to merge 2 commits into
mainfrom
prepare-run-variables

Conversation

@saga-dasgupta

@saga-dasgupta saga-dasgupta commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Why

A prepare target returns the variables for its run target's input query as untyped JSON. Nothing checks that they match what the run query declares. A renamed, added, or retyped variable deploys fine, and then the run query resolves nothing at checkout.

What

Depends on two bluejay PRs:

The first is based on the second. Until both ship in a release, the workspace Cargo.toml patches the bluejay crates in from Shopify/bluejay#149's branch.

typegen implements wasm_api's Serialize and Deserialize for the variables struct through bluejay's new CodeGenerator hooks. The struct reuses the code input objects already use, moved into a shared object_impls, so it serializes directly.

Overriding the prepare result's variables field then types it as the run query's variables struct instead of JsonValue:

#[typegen("schema.graphql", custom_scalar_overrides = {
    "CartValidationsGeneratePrepareResult.variables" => cart_validations_generate_run::InputVariables,
})]
pub mod schema {
    // query Input($ruleSetHandle: String! = "rules-default") { ... }
    #[query("src/cart_validations_generate_run.graphql")]
    pub mod cart_validations_generate_run {}
}

// `CartValidationsGeneratePrepareResult.variables` is now
// `Option<cart_validations_generate_run::InputVariables>`.
Ok(schema::CartValidationsGeneratePrepareResult {
    variables: Some(schema::cart_validations_generate_run::InputVariables {
        rule_set_handle: Some(rule_set_handle),
    }),
})

A prepare target that no longer matches its run query fails to compile:

Change to the run query Error in the prepare target
Variable renamed or removed E0560, unknown field
Variable added E0063, missing field
Variable retyped E0308, mismatched types
Prepare returns a hand-written JsonValue E0308, mismatched types

Breaking change

  • A variable whose type contains a custom scalar that the schema module aliases to its own type now needs that type to implement wasm_api's Serialize and Deserialize, and Debug, PartialEq and Clone. This applies to any #[query] module, not only run queries. Input objects already need these. The built-in mappings (String, JsonValue, Decimal) all have them.

Limits

  • String, ID, and the custom scalars mapped to String (such as Handle) are the same Rust type, so switching between them doesn't fail to compile.
  • bluejay reports an error when a variables struct's name clashes with a fragment's or operation's struct. A variable named $self, $Self, $super, $crate or $_ panics the macro, the same way fields with those names already do. Both are covered in Generate a struct for each operation's variables bluejay#149.

@saga-dasgupta
saga-dasgupta force-pushed the prepare-run-variables branch 2 times, most recently from f8a12e4 to bdddfb5 Compare October 5, 2026 16:58
@saga-dasgupta saga-dasgupta reopened this Oct 5, 2026
@saga-dasgupta
saga-dasgupta force-pushed the prepare-run-variables branch from bdddfb5 to 157b6e5 Compare October 5, 2026 18:25
@saga-dasgupta saga-dasgupta changed the title Type prepare results with the run query's generated Variables Type prepare results with the run query's variables struct Oct 5, 2026
@saga-dasgupta
saga-dasgupta force-pushed the prepare-run-variables branch 3 times, most recently from c661396 to 5385b4a Compare October 5, 2026 21:34
A prepare target returns the variables for its run target's input query as
untyped `JSON`. Nothing checked that they matched what the run query declares,
so a renamed, added, or retyped variable deployed fine and failed at checkout,
with the run query resolving nothing.

bluejay now generates a struct for the variables of each operation that
declares them: `<Operation>Variables`, or `RootVariables` for an anonymous
operation, with a field per variable. A non-null variable with no default is a
plain field; any other is an `Option` whose `None` omits it. `typegen`
implements wasm_api's `Serialize` and `Deserialize` for it with the same code
as input objects, so it serializes directly.

bluejay's `typegen` also takes `custom_scalar_overrides`, so a prepare result's
`variables` field can be typed as the run query's variables struct instead of
`JsonValue`:

    #[typegen("schema.graphql", custom_scalar_overrides = {
        "CartValidationsGeneratePrepareResult.variables" => cart_validations_generate_run::InputVariables,
    })]

A prepare target that no longer matches its run query then fails to compile:

- a renamed or removed variable is an unknown field (E0560)
- an added variable is a missing field (E0063)
- a retyped variable, or a hand-written `JsonValue`, is a mismatch (E0308)

Depends on Shopify/bluejay#149 and Shopify/bluejay#148, patched in from
Shopify/bluejay#149's branch, which has both, until they ship in a release.
@saga-dasgupta
saga-dasgupta force-pushed the prepare-run-variables branch from 5385b4a to 0174886 Compare October 6, 2026 14:33
@saga-dasgupta
saga-dasgupta marked this pull request as ready for review October 6, 2026 14:38
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