How to use dgraph to perform subqueries

hello,
I’m new to dgraph and I have some problems with query
This is my model

type <AppNode> {
	app_name
	app_host
  } 
  
  type <UrlNode>{
	url_name
	visit_time
	es_id
	visit_user_name
	belong
  }
  
  type <UserNode> {
	user_name
  }
  
  
  # Define Directives and index
  app_name: string @index(term) .
  app_host: string @index(term) .
  url_name: string @index(term) .
  visit_time: string @index(term) .
  es_id: string @index(term) .
  user_name: string @index(term) .
  visit_user_name: [uid] @reverse .
  belong: [uid] @reverse .


  {
	"set": [
  
	  {
		"uid": "_:t1",
		"app_name":"t1",
		"app_host":"t1.com"
	  },
	  {
		"uid": "_:t2",
		"app_name":"t2",
		"app_host":"t2.com"
	  },
	  {

		"url_name":"/t1/login",
		"visit_time":"2023-02-01",
		"es_id":"121212",
		"visit_user_name":{"uid":"_:t.t"},
		"belong": {"uid":"_:t1"}
	  },
	  {

		"url_name":"/t2/admin",
		"visit_time":"2023-06-01",
		"es_id":"777",
		"visit_user_name":{"uid":"_:t.t"},
		"belong": {"uid":"_:t2"}
	  },
	  {

		"url_name":"/t1/upload",
		"visit_time":"2023-08-02",
		"es_id":"999",
		"visit_user_name":{"uid":"_:t.t"},
		"belong": {"uid":"_:t1"}
	  },
	  {
		"uid":"_:t.t",
		"user_name":"t.t"
	  }
	]
  }

Now I want to search :
user_name = "t.t" and visit_time = "2023-06-01"
all of the UrlNode and AppNode

Anyone can help me

Hey @luke,

Can you show us what query you currently have going? Have you seen this in the docs: Connecting Filters - Query language

Hey @luke :pray:

Although I’m not exactly sure what you are wanting to do, my guess is that you want to use @cascade.

So, a DQL query for your problem could look like this:

{
  qUrlNode(func: type(UrlNode)) @filter(eq(UrlNode.visit_time, "2023-06-01")) @cascade(UrlNode.visit_user_name) {
    url
    UrlNode.url_name
    UrlNode.belong {
      uid
      AppNode.app_name
      AppNode.app_host
    }
    UrlNode.visit_user_name @filter(eq(UserNode.user_name, "t.t")) {
      uid
      UserNode.user_name
    }
  }
}

So you query via UrlNode to filter for visit_time. Then @cascade filters out all results where UrlNode.visit_user_name with filter user_name returns null. This result also applies to AppNode results.

Hope this helps! :raised_hands:

P.S.: If the set mutation is the real mutation you have been using, then you are missing dgraph.type on all of the nodes.

Thanks a lot.With your help I try my query like this

{
  find_user(func: eq(user_name, "t.t")) @cascade {
    user_name
    ~visit_user_name @filter(eq(visit_time, "2023-01-01")) {
      url_name
      es_id
      belong {
        app_name
        app_host
      }
    }
  }
}
1 Like