cynic icon indicating copy to clipboard operation
cynic copied to clipboard

feat: custom ser/de for scalars

Open obmarg opened this issue 1 year ago • 1 comments

Why are we making this change?

One of cynics features is that it allows you to use types from any crate as custom scalars, bypassing the usual orphan rule restrictions. In other words, cynic doesn't have to specifically add support for that crate, and that crate doesn't need to add support for cynic either.

Currently this uses the serde::Serialize & serde::Deserialize impls for the type. This is fine when the format used by the serde impls matches that used by the server you're talking to. But when the format does not match, you can no longer use the type directly, and have to create a wrapping newtype instead.

What effects does this change have?

This PR updates the cynic code so that it uses a set of cynic traits for serialising & deserialising scalars. These types are generic over the underlying schema type - allowing the ser/de of scalars to hook into the same schema specific mechanisms we do to match types to custom scalars.

This means that an application can serialise one individual type differently depending on what the server it's sending to expects.

Fixes #782

Todo:

  • [x] There's at least one todo!() in there.
  • [x] There's some commented out code that needs fixed
  • [x] More thorough tests of flatten to make sure I've not broken it
  • [ ] Doc updates
  • [ ] Write tests of this functionality.
  • [ ] Figure out if I need more where bounds on some of the generated impls

obmarg avatar Jun 09 '24 15:06 obmarg

Deploy Preview for cynic-querygen-web canceled.

Name Link
Latest commit e7c02f09fed96bca939958ff42348f57fc18ba14
Latest deploy log https://app.netlify.com/sites/cynic-querygen-web/deploys/67b64c166d724e0008b687ee

netlify[bot] avatar Jun 09 '24 15:06 netlify[bot]