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

27 views Asked by At

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.

What went wrong with my DQL query, and how can I fix it, or do you have any other suggestions to achieve the desired query result?

0

There are 0 answers