Gremlinq / ExRam.Gremlinq

A .NET object-graph-mapper for Apache TinkerPop™ Gremlin enabled databases.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Return a list of vertices from within a projection step

ttrichar opened this issue · comments

Hello,

I'm trying to create a single query that returns a list of nested objects, and I'm having some issues getting a list of objects to return within the base object. Here is my data structure:

  public class ProjectsResponse : Vertex
	{
                public virtual string Name { get; set; }

		public virtual Application[] Applications { get; set; }

		public virtual DataToken[] DataTokens { get; set; }
        }

Essentially, I am returning an array of multiple "ProjectResponse", and each "ProjectResponse" has an array of "Application" and "DataToken". Here is an example of what a single ProjectResponse would look like:

{
   "ProjectResponse":{
      "Name":"Project1",
      "Applications":[
         {
            "Name":"Application1"
         },
         {
            "Name":"Application2"
         }
      ],
      "DataTokens":[
         {
            "Name":"DataToken1"
         },
         {
            "Name":"DataToken2"
         }
      ]
   }
}

Here is the query that I am trying to run.

public virtual async Task<ProjectResponse[]> ListProjects(string Lookup){
  return await _g
        .V<Project>()
        .Where(x => x.Registry == Lookup)
        .Project(x => x.ToDynamic()
             .By(x => x.Name)
             .By("Applications",
                   __ => __
                         .Out<Uses>()
                         .OfType<Application>())
             .By("DataTokens",
                   __ => __
                         .In<SecuredBy>()
                         .OfType<DataToken>()
              )).Cast<ProjectResponse>().ToArrayAsync()
}

I am getting an array of ProjectResponses successfully, however if there are multiple applications or datatokens for a certain ProjectResponse, it will only return a single vertex.

I'm assuming it has to do with the call "ToArrayAsync". However, when I try to add that to the end of one of the "By" statements (ex. after OfType), I receive an error, since it's still looking for traversals

Cannot implicitly convert type 'System.Threading.Tasks.ValueTask<Application[]>' to 'ExRam.Gremlinq.Core.IGremlinQueryBase'
Is there a way to pull all of the "Application" and "DataToken" verticies into an array, as opposed to just returning a single vertex? Please let me know if you need additional information.

Thank you so much!

You'd have to use the Fold() operator in By to actually get an array. However, I'm not sure if this would work either.

Having Vertex-Pocos (like ProjectsResponse) inherit from Vertex and have them contain complex sub-structures (such as Application) is IMO not a good style. Gremlinq might be able to desierialize a query result into ProjectsResponse, but there's absolutely no chance that an existing and populated instance of ProjectsResponse can be serialized properly within, say, an AddV-operation. Vertexes and Edges in Tinkerpop are not complex structures. So keep your Pocos simple, and have a try whether deserializing a result to a complex structure like you have it will work.

Thank you very much for your speedy response! That fold() operator actually ended up working for us, however I do see what you're saying about keeping Pocos simple. Definitely something we'll have to keep in mind for future architecture. Also, just wanted to say my team really loves your library, so thank you for creating something so useful!

Cheers

EDIT: Forgot to add this, but thankfully we aren't going to be using this structure for any AddV operations. Just trying to traverse along multiple verticies/edges to create a complex object to use on our end, instead of making several individual gremlin queries. In your opinion, do you think this is the best way to go about creating complex objects via Gremlinq? Any suggestions for improvements/simplification? Thanks again!

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.