GraphQL self reference

Hi all,

Need some help, I’m trying to create a many to many self relation but fauna graphql wont allow me to do it, error: many to many self references are not allowed.

But graphql in essence accepts that, it’s maybe my syntax in fauna that I’m doing wrong

This is what I’m trying to accomplish:

Category object with category children… whose can have more category children.

This is similar to using a tree of category object.

Any help will be highly appreciated. Thanks!

Hi Pedro,

Fauna’s implementation of GraphQL doesn’t support self-referential one-to-many or many-to-many relations. It’s on our roadmap for the future, though.

Cory

You can create an intermediate “link table” yourself. Fauna creates many-to-many relationships by creating an intermediate Collection and abstracting the implementation out of the way for you. It’s more tedious to do yourself but it is possible.

Side note: some times it is useful to do this yourself anyway in order to add properties to a relationship, or “edge properties” if you can think about it like a graph.

Here is an example schema:

type Item {
  name: String!
  children: [ItemLink!] @relation(name: "item_children")
  parents: [ItemLink!] @relation(name: "item_parents")
}

type ItemLink {
  parent: Item! @relation(name: "item_children")
  child: Item! @relation(name: "item_parents")
}

When you create relationships between the parent/child items, you have to create the “links” yourself. Here, I’ve used a nested mutation to create one item and creating a list of children links, each one then creating another item as the child. They are query-able by explicitly selecting all of the links and then selecting the child field of the link.

If you create your own link-table-like Collection, you may want to manually create a unique compound index on the joining fields. This is to ensure that the same link is not made more than once. For this example it could be:

CreateIndex({
  name: "unique_itemLink_parent_child",
  unique: true,
  serialized: true,
  source: "ItemLink",
  terms: [
    {
      field: ["data", "parent"]
    },
    {
      field: ["data", "child"]
    }
  ]
})
1 Like

Hi @pedrynteam. Is there anything more you need help with on this topic, or can we call this one solved?

Cheers!

Hi @ptpaterson

Actually I was testing this logic yesterday and the create works but I was not able to make the connect work. Any ideas?

This is what I’m trying to do; and this is not one step as more categories can be added over time

  • create main category; Fruits and vegetables
  • create category Fruits and the parent to be the above category.

Been trying different ways for the connect and get a success on the response but the link table is empty.

Btw, create with subfields works fine

There are a couple of paths to creating the connection. Which one you choose may depend on how you want to receive the information back.

Create Documents

Create a “Link” and connect parent and child

This returns the Link document and the parent/child in a flat or normalized kind of way.

Or, Update the parent and create a “Link”

If you want to return the parent at the top and the link in between, you can write a mutation like this.

Note that I used partialUpdateItem, which is a preview feature that requires an additional header.

Or, Update the child and create a “Link”

This is the same as updating the parent, only backwards!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.