Is there possible to have same edge to different type of node?

Hi @duckybsd,
I think your requirement will be well supported by Unions. Please make sure that you are running the latest docker image.

  1. Start docker image
docker run --rm -it -p 8000:8000 -p 8080:8080 -p 9080:9080 -p 5080:5080 -p 6080:6080  dgraph/standalone:master
  1. Post the following schema. We introduce a union type pet that can refer to either a Dog or Cat. We set the type of love to pet
type Person {
  id: ID!
  first_name: String! @search(by: [exact])
  last_name: String! @search(by: [exact])
  love: pet 
}

type Dog {
  breed: String! @id @search(by: [hash])
}

type Cat {
  color: String! @id @search(by: [hash])
}

union pet = Dog | Cat 
  1. Let’s post some pets. I use the graphql playground and use the following mutations and queries.
mutation{
  addDog(input:[{breed: "Beagle"},{breed: "Corgi"}]){
    numUids
  }
  
  addCat(input:[{color: "red"}, {color:"black"}]){
    numUids
  }
}
  1. Let’s post some Person nodes, one each as a dog and cat lover.
mutation{
  addPerson(input: [{first_name: "Jim", last_name: "D", love: {dogRef: {breed: "Corgi"}}},
    			    {first_name: "James", last_name: "D", love: {catRef: {color: "black"}}  }]){
    numUids
  }
}
  1. Finally, we can query using fragments. These are the lines with `… on Dog…which helps the query language understand that thebreedbelongs to aDog, and colorto aCat``.
query{
  queryPerson(first: 5){
    first_name
    love {... on Dog {
        breed
      }
      ... on Cat {
        color
      }
    }
  }
}

The query should provide a response as below:

{
  "data": {
    "queryPerson": [
      {
        "first_name": "Jim",
        "love": {
          "breed": "Corgi"
        }
      },
      {
        "first_name": "James",
        "love": {
          "color": "black"
        }
      }
    ]
  }
}
1 Like