Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt external enums #1029

Open
alefbragin opened this issue Sep 5, 2024 · 1 comment
Open

Adopt external enums #1029

alefbragin opened this issue Sep 5, 2024 · 1 comment

Comments

@alefbragin
Copy link

There is the impl_scalar! macro. It's really convenient for external scalar types:

use chrono::{DateTime, Utc};
impl_scalar!(DateTime<Utc>, schema::DateTime);

My case. My current project has several bins and shared libs. Libs have commonly used enums, libs isn't aware about cynic and schema, but enums are the same. It will be great to have something like impl_enum!(some_lib::SomeEnum, schema::SomeEnum). This approach lacks variant rename feature though , and needs some additions for these cases.

@obmarg
Copy link
Owner

obmarg commented Sep 5, 2024

Hi @alefbragin - thanks for the feature request.

Unfortunately doing this for enums would be somewhere between difficult and impossible. Enums are more complicated than scalars. For scalars cynic simply needs to know the type, but for enums we need to care about all the variants, possible fallbacks, renames and maybe other things I'm forgetting about.

Given the schema and some type names it would probably be possible to generate a cynic::Enum impl - but it'd have to make some assumptions about the shape of your actual enum. And if that impl was wrong in any way the compiler errors probably wouldn't be that nice - there's just not enough info in impl_enum!("my-schema", MyEnum, schema::MyEnum) that we could use to attach errors to.

And the final problem is the orphan rule - impl_scalar uses some tricks (specifically this generic parameter on IsScalar) to work around the orphan rule - usually you wouldn't be able to implement a 3rd party trait on a 3rd party type like it does. The Enum trait does not have an equivalent parameter, so the orphan rule starts to apply again.

The generic parameter on IsScalar adds a bit of annoying complexity to cynic. I think it's worthwhile for scalars, because by their nature they tend to be defined in different crates. But I'm not so sure for enums - particularly given the limitations I outlined above.

If re-arranging your crates isn't possible or desired at the moment (which is fair enough) I'd recommend just having two enums and providing some From impls to easily convert between the two.

There might be some other way I can help cater to this use case in cynic, but I don't know what it is right now. I'll keep this issue open for now to think about it.

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

No branches or pull requests

2 participants