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

[Discussion] Spatial data with hc #1650

Closed
John0x opened this issue Apr 6, 2020 · 6 comments
Closed

[Discussion] Spatial data with hc #1650

John0x opened this issue Apr 6, 2020 · 6 comments

Comments

@John0x
Copy link
Contributor

John0x commented Apr 6, 2020

Summary

Hey, as discussed before on slack with @PascalSenn and @michaelstaib, I'm kinda struggling with GeoJSON and hc right now.

My current project has to work with a lot of geo data, therefore I want it to be as comfortable as possible. The Geo data is stored in a PostgreSQL database using Postgis. For the interaction between my api and the database I'm using ef core with npgsql. They recommend using the NetTopologySuite to work with spatial data, see:
https://www.npgsql.org/efcore/mapping/nts.html

NetTopologySuite has a base type, called Geometry. It also has more specific types like Point and LineString which extend the Geometry class. The current standard for geo data transportation seems to be GeoJSON, which seems to work pretty well with NetTopologySuite. There even is an existing Json transformer for transforming between NetTopologySuite and GeoJSON: https://github.com/NetTopologySuite/NetTopologySuite.IO.GeoJSON

What's the best way to work with data like that using hc?
I tried different solutions (see attempts down below) and feel kinda stuck right now.

Slack discussion:

Related issues:

Attempt 1: Custom Scalar

Having read about the scalar implementation with hc from the docs I wanted to try that first.

I want to use the NetTopologySuite types like it's something that hc can handle. Being able to just have a Point for an argument type is quite nice. Something like this:

    [ExtendObjectType(Name = "Query")]
    public class PointQuery
    {
        public Point Hello(Point startPoint)
        {
            // do something with startPoint
            var p = new Point(startPoint.x, startPoint.y);
            
            // return a Point instance
            return p;
        }
    }

Scalars can be problematic for the tooling, as pointed out by @PascalSenn on slack, but they seem to be the most comfortable to use, since I can just bind them to the Point class (.BindClrType<Point, GeoScalar>()).

My implementation: https://github.com/John0x/hotchocolate-geojson-test
It's using the aforementioned JsonTransformer but it feels really hacky :/

The input works but I cannot output a json structure. I could probably just return it as a string, but that feels way too hacky.

Attempt 2: Input and output objects and manual conversion

It would be nice to use GeoJSON, since it's used by a lot of people but it's also okayish to not use it, if it's too inconvenient for GraphQL. The idea for this one would be to have properly typed input and output objects for each specific spatial type (like Point and LineString) and then do the conversion manually using extension methods or something else.
Like this:

    [ExtendObjectType(Name = "Query")]
    public class PointQuery
    {
        public Point Hello(PointInput startPoint)
        {
            // convert to real Point
            var pAsPoint = startPoint.toPoint();
            
            var p = new Point(pAsPoint.x, pAsPoint.y);
            
            // convert to PointOutput
            return p.toOutput();
        }
    }

This would probably work just fine and be the cleanest from the perspective of someone consuming the API, but it's more overhead for the development team.

Conclusion

I'm not sure 😄
It would be nice to have a mix between attempt 1 and 2. Being able to use BindClrType to bind something like a DTO to a CLR Type, which takes care of the conversion between the GraphQL representation and the real class.

Or is there already something like that and I just didn't see it?

@michaelstaib michaelstaib added this to the HC-11.x.x milestone Apr 6, 2020
@John0x
Copy link
Contributor Author

John0x commented Apr 7, 2020

Is there no proper solution to this right now or why did it get the HC-11.x.x tag? :)

@michaelstaib
Copy link
Member

The tag is just backlog tag. HC-11.0.0 => current work backlog, HC => HC-11.x.x => later

@steveoh
Copy link
Contributor

steveoh commented Apr 21, 2020

@michaelstaib
Copy link
Member

@John0x we have opened a discussion on slack #spatial

@michaelstaib
Copy link
Member

#1747

@John0x
Copy link
Contributor Author

John0x commented May 6, 2020

closing in favor of: #1878

@John0x John0x closed this as completed May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants