Sqlx: Feature Request: Support juniper::ID

Created on 15 May 2020  路  9Comments  路  Source: launchbadge/sqlx

Would it be inline with the vision for sqlx to add Juniper GraphQL (de)serialization support under a feature flag similar to UUID? As an first pass adding juniper::ID would solve most of my use cases for this.

All 9 comments

GraphQL IDs aren't a common SQL datatype so I don't think it would be appropriate to add specific integration for this.

GraphQL in general is a completely different technology so it doesn't make much sense to integrate it into SQLx, but instead maybe building a fork of SQLx into a GraphQL client.

It looks like juniper::ID is trivially convertible to and from strings so I would do that conversion in your code. For type safety, you can create your own wrapper type and derive sqlx::Type for it:

#[derive(PartialEq, Debug, sqlx::Type)]
#[sqlx(transparent)] // means we treat this type as `String` and encode it as a `TEXT` field.
pub struct JuniperId(String);

impl From<JuniperId> for juniper::ID { ... }
impl From<juniper::ID> for JuniperId { ... }

That looks like almost exactly what I want to do. Is it possible to encode the String in the Juniper ID as a postgres INT4 instead of TEXT?

Sure, just define the wrapped type as i32 instead and do the conversion.

Also I'm still relatively new to Rust. Where do I find the sqlx(transparent) macro on docs.rs?

It's not its own macro, it's an inert attribute recognized by the sqlx::Type derive. We don't have this very well documented yet, I think we need to start an MDbook site to collect all the relevant information.

Thanks!

Update: I'm reading more and less confident this is going to work - juniper uses the types to generate GraphQL types. I'm trying to reuse the same struct for both fetching from sqlx and publishing that data through GraphQL. My suspicion is that if I use any type other then ID then it will not show up as I want in my frontend (that is, as type = ID).

I think my best path at the moment may be to query with sqlx into struct UserDB and then copy every field over to struct User for use in GraphQL.

We should add proxy support through a with attribute similar to serde, in FromRow. That would help here among a few other areas.

Closing as there isn't an actionable request here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PoiScript picture PoiScript  路  5Comments

neo-clon picture neo-clon  路  4Comments

abonander picture abonander  路  3Comments

sify21 picture sify21  路  3Comments

abonander picture abonander  路  5Comments