Ancestry trees in GraphQL import

Hello, I’ve run into the issue, when importing following schema, Fauna throws the error:

Can not identity the relationship between ‘Dog’ and ‘Dog’. Missing or ambiguous relational fields were detected. Please make relationships explicit by adding the @relation(name: …) directive on the relational fields for both the ‘Dog’ and ‘Dog’ types

Schema:

type Dog @collection(name: "Dogs") {
  siblings: [Dog] 
  sire: Dog 
  dam: Dog 
  offspring: [Dog] }

Basically trying to reference to the same type more than once results in such error. Adding @relation doesn’t help.
I believe the issue arising when Fauna trying to translate graph schema to internal object relational data model.

Hey,

I’m definitely not an expert (yet) on our GraphQL myself but to me it seems you are bumping into two different things.

  • You can’t have self-relations that are many-to-many (one of the errors mentions that).
  • You have to name your relations once they are ambigious (as far as I know).

So a workaround might be to provide classes for these many to many relations:

type Dog @collection(name: "Dogs") {
  siblings: [DogSibling] @relation
  sire: Dog @relation(name: "dog_sire")
  dam: Dog  @relation(name: "dog_dam")
  offspring: [DogOffspring] @relation
}


type DogSibling @collection(name: "DogSibling") {
  offspring: Dog @relation
}

type DogOffspring @collection(name: "DogOffspring") {
  sibling: Dog @relation
}

I suspect that creating and querying your data will get much more cumbersome though.

Thank you, Brecht! Now I’ve got it. I’ll think now on possible traversal solutions. I wonder if anybody already have worked on implementing trees in FaunaDB and have some experience to share. A couple native graph DBs I’ve looked at ate the mentioned schema as a conventional meal, so it was to some degree perplexing.

Correct me if I’m wrong here but given the example by databrecht let’s say you create a dog called Fido and give it sire = Spot. When you go back query for Spot you aren’t automatically going to see Fido as an offspring. Only Fido knows about this relationship. In order for Spot to know about this you have to update Spot and create a new offspring using the DogDogOffspringRelation object. I’ll have to have two different GraphQL calls depending on if this is a starting a new tree or not.

I gave it a try to implement this but in order to do so it seems as though I need to have all the data of the data that isn’t nullable on the parent to make the update call.

Maybe DogOffspring is really more like DogRelationship which has parent: Dog and child: Dog. ?

As far as I know this should work in both directions but I might be wrong. I’ll have to test it out and build something more complex myself with GraphQL to be certain.

I’ll ask around if someone is available to verify that assumption.

Hi Brech, I tried this wrokaround but the same type of scheme cannot be referenced multiple times, some other solution for this type of relationship??..

Isn’t the @relation directive what you are looking for? https://docs.fauna.com/fauna/current/api/graphql/directives/d_relation