-
Notifications
You must be signed in to change notification settings - Fork 426
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
Implement custom scalar for u8 type #862
Comments
have not able to find extended scalars covering Uuid, some forms for chrono time, non negative and positive integers... |
@LegNeato little help over here if possible ? Or point us to direction where we can find something regarding this. |
@kunjee17 The issue is that you don’t own neither the type nor the trait, so your not allowed to implement the trait for the type (see the error you’re getting). You can wrap the u8 in a new-type if you want, something like:
|
@dyedgreen I thought it is allowed because of macros. I did it as given in the documentation. |
It's not allowed at the language level, so doing code-gen via macros won't change that unfortunately 😅 |
@tyranron, @LegNeato , sorry to bother you with this, but would you be open to a PR that implements common Rust types as custom GQL types natively? The approach suggested by @dyedgreen in the comment above is the best one we have out of the box, but it has a number of problems:
What I would like to do is to implement custom GQL types for native Rust types not supported by GQL inside Juniper. E.g. Overall, I think it will be a net benefit for Juniper. |
@rimutaka putting it straight: I'm very skeptical to what you've described. It's not wise to put every single scalar right into Juniper. Currently (and for the next major release too), we tend to provide only the ones declared by the GraphQL spec. And commonly-used wide-known having-at-least-minimal-spec extensions like https://www.graphql-scalars.dev/docs/scalars, but behind a feature flag only.
I still prefer to keep it "user makes a newtype with the desired semantics, if he needs it". The downsides you've described doesn't look that bad to me (worthing to put things into Juniper).
That's a common newtype pattern in Rust. We do use it a lot, for example, as we do have a lot of custom scalars. Usually, this looks like: #[derive(AsRef, Clone, Debug, Display, Eq, Into, PartialEq)]
#[as_ref(forward)]
pub struct UserName(String); Regarding the
When you build the schema, you control the in/out parts of the program. So for what you've described it's enough to have
You only need to define you custom GraphQL scalars once and use @LegNeato @ilslv would like to hear your thoughts on it as well. |
I pretty much agree. Working with numbers in Rust maybe painful sometimes, but it provides more safety guarantees and makes you handle edge-cases explicitly. I don't think that erroring or panicing on those edge cases is the way to do it in Rust. Definitely not out of the box.
This approach can be appealing in case there was a common community-agreed spec for custom scalars with naming and all that. But from what I can tell specs like graphql-scalars don't have it. I think the reason behind this is that other languages don't treat numbers like Rust does, so enforcing non-native way of doing numbers may become painful for front-end interacting with this crate. Also, the thing is preventing you from implementing |
@ilslv , did you say that implementing this trait may get me out of newtyping?
It looks doable. I'm just not sure about |
@rimutaka yep, there is an example inside crates integration tests: juniper/integration_tests/juniper_tests/src/custom_scalar.rs Lines 135 to 156 in 3a70403
|
@ilslv , thanks for the link, sir! I managed to make it compile with #[graphql_object(scalar = MyScalarValue)]
impl TestType {
fn long_field() -> i64 {
i64::from(i32::MAX) + 1
}
} as in your example, but it falls over #[derive(Debug, Deserialize, GraphQLObject)]
struct MyStruct {
pub num: i64,
} with a long list of missing implementations:
All scalar examples I could find had Is it possible to make |
@rimutaka yes, this is possible, but looks like not documented well enough unfortunately. All derive macros use juniper/integration_tests/juniper_tests/src/codegen/object_derive.rs Lines 823 to 827 in 3a70403
|
@rimutaka thanks for the effort with docs, but, at the moment, we make quite enough breaking changes to the macro system and semantics, so there is no point to dig the docs now, as they will need total rewrite anyway, once the chages settle. |
I leave this issue open for a while as a reminder for new docs to describe this case. |
I'm trying to update my custom scalar implementation for u64 to work with the new What got me unstuck was this example https://github.com/graphql-rust/juniper/blob/master/integration_tests/juniper_tests/src/codegen/scalar_value_derive.rs. Consider adding a link to that file from the guide. I'm happy to make a PR with it as a separate example. |
@rimutaka can you please describe what exactly was confusing about the book? Is it wording |
Yes, that's the one. I starting adapting the custom implementation I had, but wasn't sure what changed there. It looked like you completely removed GraphQLScalarValue. The change log was still referring to it as if it's in use. Small things like that. Long story short, that integration example worked as-is and took me just a few minutes to merge with my custom code hence my suggestion to link to it. I wouldn't rush to make changes. It could be just me being a bit obtuse :) |
Yes, because old |
Might be totally dumb question. But I couldn't make it work.
Here is my code for making Custom Scalar for
u8
type.But I m getting error
I can't figure it out what is wrong in my code. Any help or pointers are more than welcome.
The text was updated successfully, but these errors were encountered: