In a one-to-one relation between two collections, provide a mechanism to control in what collection the reference will be stored

Fauna relies on your feedback to educate our product development process. To help us prioritize this feature in our product roadmap, please vote for this topic and complete our 2020 GraphQL Roadmap Survey.

Issue description

When establishing a one-to-one relation, the GraphQL API automatically determines which of the documents (backing up the related object types) the reference establishing the relation will be stored on. The current implementation works as described below:

When importing the following schema…

type Room {
  name: String!
  person: Person
}

type Person {
  name: String!
  room: Room
}

…The code sorts the types alphabetically by name and then stores the Ref in the last one. In this case, it will be the Room collection backing up the Room object type.

Then, a document from the Person collection would look like this:

{
  "ref": Ref(Collection("Person"), "258627039444075027"),
  "ts": 1582904981100000,
  "data": {
    "name": "John"
  }
}

And, a document from the Room as follows:

{
  "ref": Ref(Collection("Room"), "258627039448269331"),
  "ts": 1582904993890000,
  "data": {
    "name": "Room ABC",
    "person": Ref(Collection("Person"), "258627039444075027")
  }
}

The main problem with this current implementation is that it might be problematic when working with other features. For example, when establishing ABAC permissions, users might need to determine where the reference is stored.

Proposals

When establishing a one-to-one relation, we should provide a mechanism (during schema definition if possible), for allowing users to determine in which of the documents (backing up the related object types) the reference establishing the relation will be stored on.

  1. We could add an argument to the @relation directive for defining which sides of the relation the reference will be stored on.
  2. Alternatively, we could constrain setting the @relation directive to one of the sides of the relation only, and use the type with the directive for storing the reference. However, it is unclear whether or not this might this be a problem when we need to disambiguate various relations.

Workaround

As suspected, you can change the names of the collections so that the one where you want the Ref is alphabetically last. For example, in the example above, you could change Person to Z_Person, or Room to A_Room. (Please note that names cannot start with underscores in the current version of the GraphQL API, so you could not use _Room.)

If you have implemented your own solution/s that you think could help other users with this, please share them as replies to this topic and/or make a gist and link to it from the awesome-faunadb list.