Comments (27)
In fact this concerns all ID conversions. If that is something desirable I will give it a shot. Should not be breaking existing implementations because all types implement From
and Into
themselves.
from async-graphql.
Brilliant! @IcanDivideBy0 I much prefer your solution! Didn't consider removing Display
for those types an option.
from async-graphql.
Since you have this requirement, I think this feature makes sense. I'll add it now.😁
from async-graphql.
Cool. I will see what I can with error_extensions.md on monday.
from async-graphql.
Correct me if I'm wrong but this feels unnecessary complicated.
By removing the impl std::fmt::Display for Cursor
, we can then add
impl<T> From<T> for Cursor
where
T: std::fmt::Display
{
fn from(value: T) -> Self {
Cursor(value.to_string())
}
}
which removes the need for the ToGraphQLCursor
trait ?
Same goes for ID, I don't think those types needs to implement display, Debug should be enough for general purpose
from async-graphql.
@sunli829 you probably forgot to remove the impl fmt::Display for ID
? or any other From
where the type already impl fmt::Display
?
from async-graphql.
I use Uuids as well and I think it is quite common. Maybe implement it for T: Into<Uuid>
so that it works well with the new type pattern?
from async-graphql.
@nicolaiunrein I think that would be a good idea. When converting between ID and Cursor, I need to do id.to_string().into()
which isn't ideal.
from async-graphql.
@nicolaiunrein How is this feature going to be implemented?😁
from async-graphql.
Sorry, I am pretty busy at the moment with other projects. I will look at it tomorrow or on monday.
from async-graphql.
Well, we can't implement generic From
implementation due to conflicting implementations but I would suggest to add two traits:
pub trait ToGraphqlCursor {
fn to_graphql_cursor(&self) -> Cursor;
}
impl<T> ToGraphqlCursor for T
where
T: std::fmt::Display,
{
fn to_graphql_cursor(&self) -> Cursor {
Cursor(self.to_string())
}
}
and
pub trait ToGraphqlID {
fn to_graphql_id(self) -> ID;
}
impl<T> ToGraphqlID for T
where
T: std::fmt::Display,
{
fn to_graphql_id(self) -> ID {
ID(self.to_string())
}
}
which when imported give you two methods to_graphql_cursor
and to_graphql_id
on anything that implements std::fmt::Display
and allows you to instead of calling id.to_string().into()
just id.to_graphql_cursor
. Now this can be quite handy when you implement your own ID type as well:
pub struct MyID(Uuid);
impl std::fmt::Display for MyID { ... }
// somewhere else
let id = my_id.to_graphql_id();
What do you think?
from async-graphql.
I think it's good, it's really easier to use. 😁
In addition, can you add this error_extensions.md when you are free? I think you know more about this than me.
I have been working on a new parser for the past few days and it is now complete. You can upgrade to 1.11.0 to see if there is a problem with your current project.
Thanks again, ToGraphqlID
and ToGraphqlCursor
I will add later. 😁
from async-graphql.
Thank you. 😁
from async-graphql.
@nicolaiunrein It has been implemented in version 1.11.1.
from async-graphql.
from async-graphql.
In the end it's even simpler
let cursor: Cursor = "foo".into();
let my_uuid = uuid::Uuid::parse_str("936DA01F9ABD4d9d80C702AF85C822A8").unwrap();
let cursor: Cursor = my_uuid.into();
from async-graphql.
@IcanDivideBy0 Seems to require generic specialization?
36 | impl<T: Display> From for ID {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate core
:
- impl std::convert::From for T;
from async-graphql.
@nicolaiunrein Is this the reason?
from async-graphql.
Sorry, you actually answered the question. 😊@nicolaiunrein
from async-graphql.
@sunli829 let me send the PR
from async-graphql.
Weird things, you can compile and pass. I want to see how your modification differs from mine.😳
from async-graphql.
Thanks, I understand, because ID also implements Display, so it will conflict. I think the implementation of Display can indeed be removed, which is even simpler. Thank you for your PR. 😁
from async-graphql.
@nicolaiunrein ID
and Cursor
now properly implements From<fmt::Display>
on master.
@sunli829 I let you decide if this worth a release :)
from async-graphql.
Let me do it, just change the version number. 😂
from async-graphql.
In addition, can you add this error_extensions.md when you are free? I think you know more about this than me.
@sunli829 I wrote something but it got quite lengthy. Please review.
from async-graphql.
Thank you so much. I translated the Chinese version later. 🍺
from async-graphql.
@nicolaiunrein The documentation is too good, much better than mine. 😂
from async-graphql.
Related Issues (20)
- Get mutable referenece to the global data defined in the `Context` or `Schema` HOT 1
- Confusing `unused_mut` warning in `#[Object]` HOT 8
- How to handle both directions of one-to-many relation in federated graph
- Question: How to get server to send ping messages on subscriptions? HOT 2
- Parsing multiple operations in a file HOT 1
- Non nullable variables should allow default values HOT 3
- Object with single skipped field but with ComplexObject HOT 2
- As using proxy type
- Using flatten inside an impl with no other fields causes a compile error
- Subscription with MPSC receiver in context data
- Reduce clippy noise from #[Object] macro HOT 2
- Guard trait lifetime HOT 1
- Using generics with both SimpleObject and InputObject as field in output type fails HOT 1
- Subscription Authentication
- Stack overflow after upgrade to 7.0.2 HOT 9
- Does async-graphql validate responses?
- Error reading data from ExtensionContext after upgrade to 7.0.3 HOT 1
- Create a general error formateer
- Allow flatten on arguments HOT 1
- External fields not getting generated in SDL
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async-graphql.