defining a GraphQL mutation in DGraph that adds a node that is of union type within another node

96 Views Asked by At

I am new to GraphQL and Dgraph and have been reading through the manual.

I am following an example in the manual posted here: https://dgraph.io/docs/graphql/schema/types/#union-type

I am seeking to use a mutation to add a node or list of Nodes and associate them as members acceptable within the addHome input function.

These nodes are however also of type Union and so I need to somehow cast the HomeMember to a Dog and define the category as an animal.

I see someone posted something here but i can't pull the pieces together since the code it suggests appears to already have been generated by DGraph: https://discuss.dgraph.io/t/union-types-in-graphql/9581

I am unsure but have a feeling I perhaps need to somehow override the input HomeMemberRef which is automatically generated into the schema by DGraph and disable both parrot and human ?

My code so far looks like this:

mutation {
    addHome(input: [ 
    {address: "London", 
      
    // Here I need to add something a member that casts the
    // HomeMember to a Dog, defines the category as an animal and is accepted within 
    // this addHome input function
      
    }
    ]) {
    home{
    address
    id
    members
    }
    } 
    }
1

There are 1 best solutions below

0
On

Found the answer posted on another page: https://dgraph.io/docs/graphql/mutations/mutations-overview/

My GraphQL works using this:

mutation {
addHome(input: [ 
{address: "London", 
  
  
 members: [
            { dogRef: { category: Mammal, breed: "German Shepherd"} },
   
]
}
]) {

    home {
      address
      members {
        ... on Dog {
          breed
        }
        ... on Parrot {
          repeatsWords
        }
        ... on Human {
          name
        }
      }
    }
}
}