GraphQL edge/predicate pointing to multiple different types

Hi @gorillastanley
Extending @abhijit-kar suggestion, we can define an interface that will help with the flexibility to achieve links to multiple types via one predicate.
For example:
Consider following schema:

type User {
    userId: ID!
    username: String! @search(by: [hash, term])
    email: String! @search(by: [hash, regexp])
    content: [Content!]
}

interface Content {
    cId: ID!
    name: String! @search(by: [hash, regexp])
}

type Course implements Content {
    chapters: [Chapter!]!
}

type Chapter {
    name: String! @search(by: [hash, regexp])
    data: String! @search(by: [hash, regexp])
}

type Question implements Content {
    questions: String! @search(by: [hash, regexp])
    answer: String! @search(by: [hash, regexp])
}

Then we can have mutations in the following order:

  1. Create Course
mutation {
  addCourse(input: [{
  	name: "Welcome to Dgraph",
    chapters: [
      {
        name: "Introduction to graphql",
        data : "Graphql is Easy"
      }
    ]

  }]) {
    course {
      cId
    }
  }
}

  1. Create Question
mutation {
  addQuestion(input: [{
  	name: "Questions: Welcome to Dgraph",
    questions: "Did you like it?",
    answer: "yes"
  }]) {
    question {
      cId
    }
  }
}
  1. Create User
mutation {
  addUser(input: [{
    username: "XYZ",
    email: "xyz@abc.com",
    content: [
     {
      cId: "0x3" #check this id
    },
      {
        cId: "0x5" #check this id
      }
    ]
    }]) {
    user {
      userId
      username
      Content {
        cId
        name
        ... on Course {
          chapters {
            name
            data
          }
        }

        ... on Question {
          questions
          answer
        }
      }
    }
  }
}

Notice the return of the third mutation. content points to two different types i.e. Course and Question. Using @hasInverse directive you can manage two-way edges.

2 Likes