In a DQL statement, how to pass the UID as a variable within the subquery block below

In the given Dgraph data model

type LearningStage { 
    learning_stage_name: String! 
} 
type Grade { 
    grade_name: String!
    belonged__learning_stage: LearningStage! @revese
    contained_subject: [Subject!]
} 
type Subject { 
    subject_name: String!
    belonged_learning_stage: [LearningStage!]! @revese
} 
type TextBookHead { 
    name: String!
    belonged__subject: Subject! @revese
    belonged__grade: Grade! @revese
}

how can I retrieve all the LearningStages in the system, where each LearningStage includes the Grades belonging to it, the Subjects contained within those Grades, and the TextBookHead objects belonging to those Subjects’ corresponding Grades.

At the moment, I attempted to use the UID of Grade nodes under LearningStages as a variable and pass it to the query block of TextBookHead to filter out those TextBookHead objects that do not belong to the current grade, as shown in the DQL query below:

{
  switch_text_book_infos(func: type("LearningStage")) {
    uid
    LearningStage.learning_stage_name
    LearningStage.serial_num_in_whole_education_life_cycle

    allGrades as ~Grade.belonged__learning_stage {
      uid
    }
    LearningStage.ownGrades: ~Grade.belonged__learning_stage {
      uid
      Grade.grade_name
      Grade.contained_subject {
        uid
        Subject.subject_name
        Subject.display_order_num_in_learning_stage

        Subject.ownTextBookHeads: ~TextBookHead.belonged__subject @cascade {
          uid
          TextBookHead.name
          TextBookHead.total_fascicle_num
          TextBookHead.press_institution_name
          TextBookHead.published_time
          TextBookHead.cover_pic_url
          TextBookHead.belonged__grade @filter(uid(val(allGrades))) {
            uid
            Grade.grade_name
            Grade.serial_num_in_learing_stage
          }
        }
      }
    }
  }
}

However, it didn’t work as expected, and the allGrades variable didn’t seem to retrieve any values.

What went wrong, and how can I accomplish the above-mentioned query requirements? Do you have any suggestions? Modifying the data model is also an option.

You are mixing @hasInverse directive (GraphQL) and reverse notation from GraphQL, plus a type “reverse”.
You can set @reverse on the DQL schema and get relations like ~Grade.belonged__learning_stage working but I would suggest to set links at GraphQL level if you have decided to use GraphQL.
You would need to name the reverse edge and use GrapphQL mutations to set the data (our GraphQL layer will ensure that hasReverse is honored and correct predicates are created.

Let us know your approach so we can help better…

Thank you for your response. After further reading the official documentation, we realized that our initial design of the data model, based on our previous experience with MySQL databases and with limited knowledge of Dgraph, may not align with our business requirements. We are currently in the process of reevaluating and redefining our data model, so the questions we had earlier are not applicable at the moment.

We apologize for any inconvenience our premature questions may have caused, and we sincerely appreciate your responses.

2 Likes