Hi @duckybsd,
I think your requirement will be well supported by Unions. Please make sure that you are running the latest docker image.
- 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
- Post the following schema. We introduce a union type
petthat can refer to either aDogorCat. We set the type oflovetopet
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
- 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
}
}
- Let’s post some
Personnodes, 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
}
}
- Finally, we can query using
fragments. These are the lines with `… on Dog…which helps the query language understand that thebreedbelongs to aDog, andcolorto 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"
}
}
]
}
}