Skip to content

feat: custom ser/de for scalars#980

Draft
obmarg wants to merge 14 commits intomainfrom
obmarg/push-rwuwzxpvmrkv
Draft

feat: custom ser/de for scalars#980
obmarg wants to merge 14 commits intomainfrom
obmarg/push-rwuwzxpvmrkv

Conversation

@obmarg
Copy link
Owner

@obmarg obmarg commented Jun 9, 2024

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:

  • There's at least one todo!() in there.
  • There's some commented out code that needs fixed
  • 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 obmarg added this to the 4.0.0 milestone Jun 9, 2024
@obmarg obmarg added the breaking A breaking change label Jun 9, 2024
@netlify
Copy link

netlify bot commented Jun 9, 2024

Deploy Preview for cynic-querygen-web canceled.

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

@obmarg obmarg force-pushed the obmarg/push-rwuwzxpvmrkv branch from 98146da to 8f601f6 Compare February 17, 2025 13:32
}

/// Indicates that a type is a scalar that maps to the given schema scalar.
// TODO: Think about the names of the scalar traits....
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to think about this

@obmarg obmarg force-pushed the obmarg/push-rwuwzxpvmrkv branch from f9e279e to cd07a50 Compare February 17, 2025 19:36
@obmarg obmarg force-pushed the obmarg/push-rwuwzxpvmrkv branch from cd07a50 to e7c02f0 Compare February 19, 2025 21:24
@obmarg obmarg mentioned this pull request Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking A breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom (de)serializers

1 participant