Coder Social home page Coder Social logo

Comments (27)

nicolaiunrein avatar nicolaiunrein commented on April 27, 2024 2

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.

nicolaiunrein avatar nicolaiunrein commented on April 27, 2024 2

Brilliant! @IcanDivideBy0 I much prefer your solution! Didn't consider removing Display for those types an option.

from async-graphql.

sunli829 avatar sunli829 commented on April 27, 2024 1

Since you have this requirement, I think this feature makes sense. I'll add it now.😁

from async-graphql.

nicolaiunrein avatar nicolaiunrein commented on April 27, 2024 1

Cool. I will see what I can with error_extensions.md on monday.

from async-graphql.

IcanDivideBy0 avatar IcanDivideBy0 commented on April 27, 2024 1

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.

IcanDivideBy0 avatar IcanDivideBy0 commented on April 27, 2024 1

@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.

nicolaiunrein avatar nicolaiunrein commented on April 27, 2024

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.

phated avatar phated commented on April 27, 2024

@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.

sunli829 avatar sunli829 commented on April 27, 2024

@nicolaiunrein How is this feature going to be implemented?😁

from async-graphql.

nicolaiunrein avatar nicolaiunrein commented on April 27, 2024

Sorry, I am pretty busy at the moment with other projects. I will look at it tomorrow or on monday.

from async-graphql.

nicolaiunrein avatar nicolaiunrein commented on April 27, 2024

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.

sunli829 avatar sunli829 commented on April 27, 2024

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.

sunli829 avatar sunli829 commented on April 27, 2024

Thank you. 😁

from async-graphql.

sunli829 avatar sunli829 commented on April 27, 2024

@nicolaiunrein It has been implemented in version 1.11.1.

from async-graphql.

IcanDivideBy0 avatar IcanDivideBy0 commented on April 27, 2024

IcanDivideBy0@690f2b4

from async-graphql.

IcanDivideBy0 avatar IcanDivideBy0 commented on April 27, 2024

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.

sunli829 avatar sunli829 commented on April 27, 2024

@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.

sunli829 avatar sunli829 commented on April 27, 2024

@nicolaiunrein Is this the reason?

from async-graphql.

sunli829 avatar sunli829 commented on April 27, 2024

Sorry, you actually answered the question. 😊@nicolaiunrein

from async-graphql.

IcanDivideBy0 avatar IcanDivideBy0 commented on April 27, 2024

@sunli829 let me send the PR

from async-graphql.

sunli829 avatar sunli829 commented on April 27, 2024

Weird things, you can compile and pass. I want to see how your modification differs from mine.😳

from async-graphql.

sunli829 avatar sunli829 commented on April 27, 2024

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.

IcanDivideBy0 avatar IcanDivideBy0 commented on April 27, 2024

@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.

sunli829 avatar sunli829 commented on April 27, 2024

Let me do it, just change the version number. 😂

from async-graphql.

nicolaiunrein avatar nicolaiunrein commented on April 27, 2024

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.

sunli829 avatar sunli829 commented on April 27, 2024

Thank you so much. I translated the Chinese version later. 🍺

from async-graphql.

sunli829 avatar sunli829 commented on April 27, 2024

@nicolaiunrein The documentation is too good, much better than mine. 😂

from async-graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.