Query with respective to internal node

This is my schema

type Student {
  id: ID!
  name: String!
  educationDetails: [Education]
}

type Education {
  id: ID!
  degree: String!
  startYear: DateTime!
  endYear: DateTime!
  collegeName: String!
  user: Student! @hasInverse(field: educationDetails)
}

is there a way that I can bring all the education details of a student by querying on education and filtering on user internally

query{
 queryEducation{
  id
  collegeName    #These education details should be of below user ID
  degree
  startYear
  endYear
  user(filter:{id:"#Some User ID"}){
    id
    name
  	}
	}
}

Yes. You can definitely filter internal nodes if they match any of the following properties :

  1. The field being filtered is of object type ID or has an id directive succeeding it
  2. The field that is to be filtered has a search directive succeeding it

However that isn’t always the best way to filter through subfields. In your schema, since you are querying on education first, the n-th subfilter might not exist ( leading to a null return, since not all Education objects will have a common student to them). A better schema would be as follows:

type Student {
id: ID!
name: String! @search
educationDetails: [Education!]
}

type Education {
id: ID!
degree: String!
startYear: DateTime!
endYear: DateTime!
collegeName: String!
user: Student @hasInverse(field: educationDetails)
}

And you should filter on queryStudent instead :

query requireStudents($id1: [ID!] = []) {
  queryStudent(filter: {id: $id1}) {
    name
    educationDetails {
      collegeName
      degree
    }
  }
}

Read up more regarding directive over here → https://dgraph.io/docs/graphql/directives/

1 Like