cruddl icon indicating copy to clipboard operation
cruddl copied to clipboard

Collect last entity in chain

Open rmortes opened this issue 2 years ago • 5 comments

Hi! I'm fairly new to graph databases, so I may not know the correct terminology.

If I have a schema like this one:

Schema
graph TD
    A[Employer] -->|Worked with| B(Employee 1)
    A[Employer] -->|Worked with| C(Employee 2)
    A[Employer] -->|Worked with| D(Employee 3)
    A[Employer] -->|Worked with| E(Employee 4)
    A[Employer] -->|Worked with| F(Employee 5)
    B -->|Taught| C
    C -->|Taught| D
    D -->|Taught| E
    E -->|Taught| F

Is there a way to retrieve the last entity, Employee 5, using this library?

Also, my schema may not be as "pure" as that one, looking more like:

Schema
graph TD
    A[Employer] -->|Worked with| B(Employee 1)
    A[Employer] -->|Worked with| C(Employee 2)
    A[Employer] -->|Worked with| D(Employee 3)
    A[Employer] -->|Worked with| E(Employee 4)
    A[Employer] -->|Worked with| F(Employee 5)
    A[Employer] -->|Worked with| G(Employee 6)
    B -->|Taught| C
    C -->|Taught| D
    D -->|Taught| E
    E -->|Taught| F
    B -->|Taught| G
    C -->|Taught| H(Employee 7)

But I'd still want to retrieve the lastof the "teaching" chain for a specific employer

Thanks!

rmortes avatar Jul 20 '22 09:07 rmortes

Hello, we do not currently have any way to request this directly in our graphql API. And as far as I know there is no way to recursively specify fragments in graphql. Because of this you have to specifiy how many layers you want to request similar to this article: https://hashinteractive.com/blog/graphql-recursive-query-with-fragments/ Because of this I think there is currently no really good solution to your problem using this library.

mfusser avatar Jul 20 '22 11:07 mfusser

I don't really want to recursively request fragments in the query. I'd like my schema to be something like

type Employer {
  employees: [Employee!]! @relation
  lastEmployee: Employee @something
}

But as I understand it, there's no good way to do this currently with this library, right? Then, could I create a custom resolver for that field? How would the query look like?

rmortes avatar Jul 20 '22 16:07 rmortes

As we do not use a lot of the graph-based features of arangoDB my expertise in that regard is very limited. Maybe you can find a solution in the arangoDB documentation: https://www.arangodb.com/docs/devel/aql/graphs.html There is also an ArangoDB Slack workspace where hopefully someone can help you out https://slack.arangodb.com/

mfusser avatar Jul 21 '22 09:07 mfusser

If I get @rmortes right, it would be more about a new type of aggregation in a collect field

    lastEmployee: Employee @collect(path: "employees", aggregate: LAST, order: "createdAt")

As relations in cruddl don't have an understanding of a last element this would have to be enhanced with a sort field which defines the order in which an element is LAST. @rmortes, did I get you right?

henkesn avatar Jul 21 '22 09:07 henkesn

No I didn't.

henkesn avatar Jul 21 '22 09:07 henkesn

Sorry for replying so late.

I guess you want to

  • walk the graph following "worked_with" and "taught" relations
  • and then find the objects with the longest path

For this to work, two things are missing in cruddl:

  • @collect supports basic path traversal, but it does not support traversing two edges at once, i.e. you need to be explicit which relation to follow. You could theoretically do something like "follow zero or one taught relation, then follow zero or one worked with relation, then follow zero or one taught relation..." but that would probably be a nightmare in performance
  • there is no way to sort objects by path length. We don't even expose the path concept anywhere in @collect

The first thing might be supported some day, but it's really not high on our priority list. The second thing sounds like a very special case, and we don't really have a construct of expressions. I honestly don't think we will support this with the current design of cruddl.

I'll close this issue therefore.

Yogu avatar Sep 21 '22 16:09 Yogu