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

Feature: Add foreach() function. #5335

Open
MichelDiz opened this issue Apr 30, 2020 · 3 comments
Open

Feature: Add foreach() function. #5335

MichelDiz opened this issue Apr 30, 2020 · 3 comments
Labels
area/querylang/function area/upsert Issues related to upsert operations. dgraph Issue or PR created by an internal Dgraph contributor. exp/expert Deeply technical issue not recommended to beginners. kind/feature Something completely new we should consider. status/needs-attention This issue needs more eyes on it, more investigation might be required before accepting/rejecting it

Comments

@MichelDiz
Copy link
Contributor

MichelDiz commented Apr 30, 2020

Experience Report

What you wanted to do

I would like to use loops in queries and upsert blocks. In this case something similar to "foreach".

What you actually did

There's nothing similar to this in Dgraph right now.

Why that wasn't great, with examples

A loop like foreach would be very useful. There are situations that we want to iterate over objects in a query. Whether to display a particular form structure in the response or to create specific mutations in the upsert block. Which is not currently supported in Dgraph.

Syntax

I believe that Syntax from foreach would be very similar to the K-shortest path.

More examples

I think a "foreach" func would be the solution for almost all the things like "groupby value".

Ref of a DB doing something similar to it:
https://neo4j.com/docs/cypher-manual/current/clauses/foreach/
https://docs.mongodb.com/manual/reference/method/cursor.forEach/

A foreach loop could solve this issue #4779 in a blink of an eye (eliminating the misuse of an extra block). I gonna add a comment there with an example using "foreach".

see #4779 (comment)

In this link https://discuss.dgraph.io/t/foreach-func-in-graphql-loops-in-bulk-upsert/5533 I have other use cases. Most of them related to upsert block, issues with Facets bad responses (e.g: #4160), and so on.

Pay attention that this link is a little old. I used different syntax just to illustrate.

Reference for the group by issue: #4170

e.g: "for each group found in groupby create a new object to use in response".

foreach(in: PARAM1, title: PARAM2).
> For each UID in PARAM1, create a new object in the response with the title in PARAM2
{
 var(func: has(kind)) @groupby(kind) {
    T as count(uid)
  }

 foreach(func: foreach(in: T, title: kind)) {
   name
   age
   total : val(T)
 }
}

Desirable Result

{
  "data": {
    "q": [
      {
        "dog": [
              {
                "total": 1
              },
              {
                "uid": "0x1",
                "name": "Bingo",
                "age": "3"
              }
      ]
      },
      {
        "animal": [
              {
                "total": 2
              },
              {
                "uid": "0x1",
                "name": "Bingo",
                "age": "3"
              },
              {
                "uid": "0x3",
                "name": "Angry Purr",
                "age": "1"
              }
      ]
      },
      {
        "cat": [
              {
                "total": 1
              },
              {
                "uid": "0x3",
                "name": "Angry Purr",
                "age": "1"
              }
      ]
      }
    ]
  }

Any external references to support your case

https://discuss.dgraph.io/t/foreach-func-in-graphql-loops-in-bulk-upsert/5533

@MichelDiz MichelDiz added kind/feature Something completely new we should consider. exp/expert Deeply technical issue not recommended to beginners. status/needs-attention This issue needs more eyes on it, more investigation might be required before accepting/rejecting it area/upsert Issues related to upsert operations. area/querylang/function labels Apr 30, 2020
@jostillmanns
Copy link

and all of the sudden dgraph queries are turing complete 😁

@amaster507
Copy link
Contributor

This would be useful for me too. I will be working with data and provide users a interface to do bulk actions on data. Set a tag on all contacts that have X, etc.

I also will be working with contact data managing children in school grades and each year need to do bulk updates to adjust school grades.

Also, working with CRM there will be dozens of use cases for looping across data and processing it. If this is not on the db layer then it will be done in application layer. Doing it at a lower layer I believe will be more beneficial and reduce network load.

@damienburke
Copy link

Re https://discuss.dgraph.io/t/filter-on-absence-of-an-edge/8333/5, this would be useful for me too. Similar to above comment, I am faced with now pushing logic to the application layer.

@MichelDiz MichelDiz reopened this Jul 30, 2022
@MichelDiz MichelDiz added the dgraph Issue or PR created by an internal Dgraph contributor. label Oct 25, 2022
@dgraph-io dgraph-io deleted a comment from minhaj-shakeel Feb 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/querylang/function area/upsert Issues related to upsert operations. dgraph Issue or PR created by an internal Dgraph contributor. exp/expert Deeply technical issue not recommended to beginners. kind/feature Something completely new we should consider. status/needs-attention This issue needs more eyes on it, more investigation might be required before accepting/rejecting it
Development

No branches or pull requests

5 participants