# GraphQL edge/predicate pointing to multiple different types

**URL:** <https://discuss.dgraph.io/t/graphql-edge-predicate-pointing-to-multiple-different-types/9913>\
**Category:** GraphQL\
**Tags:** graphql, kind:question, help-wanted\
**Created:** [August 31, 2020, 5:12am UTC](https://discuss.dgraph.io/t/graphql-edge-predicate-pointing-to-multiple-different-types/9913 "2020-08-31T05:12:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gorillastanley](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/gorillastanley/32/4518_2.png) [@gorillastanley](https://discuss.dgraph.io/u/gorillastanley)\
**Post date:** [August 31, 2020, 5:12am UTC](https://discuss.dgraph.io/t/graphql-edge-predicate-pointing-to-multiple-different-types/9913/1 "2020-08-31T05:12:53Z")

</div>

In GraphQL±, the following is possible:

```auto
type Note {
  <~HAS_NOTE>
}

```

This is useful for acquiring the nodes that point to this Note node through a HAS\_NOTE edge.  
How can this behaviour be replicated in GraphQL? Is there any way to achieve links to multiple different types in only one predicate?

---

<div class="post-metadata">

**Author:** ![anand](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anand/32/5674_2.png) [@anand](https://discuss.dgraph.io/u/anand)\
**Post date:** [August 31, 2020, 5:22am UTC](https://discuss.dgraph.io/t/graphql-edge-predicate-pointing-to-multiple-different-types/9913/2 "2020-08-31T05:22:56Z")

</div>

I think this graphql [example](https://dgraph.io/docs/graphql/schema/graph-links/#two-way-edges-edges-with-an-inverse) might help in achieving what you want. The @hasInverse directive has capability to hold two-way edges.

---

<div class="post-metadata">

**Author:** ![gorillastanley](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/gorillastanley/32/4518_2.png) [@gorillastanley](https://discuss.dgraph.io/u/gorillastanley)\
**Post date:** [August 31, 2020, 5:36am UTC](https://discuss.dgraph.io/t/graphql-edge-predicate-pointing-to-multiple-different-types/9913/3 "2020-08-31T05:36:17Z")

</div>

Thanks.  
However, is there any way to achieve links to multiple different types in only one predicate?

```auto
type Note {
  NOTE_FOR: # generic type here
}

```

---

<div class="post-metadata">

**Author:** ![abhijit-kar](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/abhijit-kar/32/3726_2.png) [@abhijit-kar](https://discuss.dgraph.io/u/abhijit-kar)\
**Post date:** [August 31, 2020, 6:44am UTC](https://discuss.dgraph.io/t/graphql-edge-predicate-pointing-to-multiple-different-types/9913/4 "2020-08-31T06:44:18Z")

</div>

> [@gorillastanley](#):
>
> However, is there any way to achieve links to multiple different types in only one predicate?

Except Interface, I haven’t seen any other approach.

---

<div class="post-metadata">

**Author:** ![abhimanyusinghgaur](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/abhimanyusinghgaur/32/2980_2.png) [@abhimanyusinghgaur](https://discuss.dgraph.io/u/abhimanyusinghgaur)\
**Post date:** [August 31, 2020, 7:30am UTC](https://discuss.dgraph.io/t/graphql-edge-predicate-pointing-to-multiple-different-types/9913/5 "2020-08-31T07:30:46Z")

</div>

We are going to have Unions soon, please have a look at [the RFC for Union types](http://discuss.hypermode.com/t/union-types-in-graphql/9581).  
That may be what you are looking for.

---

<div class="post-metadata">

**Author:** ![aman-bansal](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/aman-bansal/32/4475_2.png) [@aman-bansal](https://discuss.dgraph.io/u/aman-bansal)\
**Post date:** [August 31, 2020, 8:10am UTC](https://discuss.dgraph.io/t/graphql-edge-predicate-pointing-to-multiple-different-types/9913/6 "2020-08-31T08:10:51Z")

</div>

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:

```auto
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

```auto
mutation {
  addCourse(input: [{
  	name: "Welcome to Dgraph",
    chapters: [
      {
        name: "Introduction to graphql",
        data : "Graphql is Easy"
      }
    ]

  }]) {
    course {
      cId
    }
  }
}

```

1. Create Question

```auto
mutation {
  addQuestion(input: [{
  	name: "Questions: Welcome to Dgraph",
    questions: "Did you like it?",
    answer: "yes"
  }]) {
    question {
      cId
    }
  }
}

```

1. Create User

```auto
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.

---

<div class="post-metadata">

**Author:** ![gorillastanley](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/gorillastanley/32/4518_2.png) [@gorillastanley](https://discuss.dgraph.io/u/gorillastanley)\
**Post date:** [September 4, 2020, 12:22am UTC](https://discuss.dgraph.io/t/graphql-edge-predicate-pointing-to-multiple-different-types/9913/7 "2020-09-04T00:22:23Z")

</div>

I’m grateful for your highly detailed response.

To clarify: create an interface, and then I can point to multiple different types by having those different types implement the interface?

---

<div class="post-metadata">

**Author:** ![abhijit-kar](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/abhijit-kar/32/3726_2.png) [@abhijit-kar](https://discuss.dgraph.io/u/abhijit-kar)\
**Post date:** [September 4, 2020, 2:43am UTC](https://discuss.dgraph.io/t/graphql-edge-predicate-pointing-to-multiple-different-types/9913/8 "2020-09-04T02:43:03Z")

</div>

> [@gorillastanley](#):
>
> To clarify: create an interface, and then I can point to multiple different types by having those different types implement the interface?

Yes
