Allow @unique directive to support multiple fields

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.

Proposal

Allow @unique directive to support multiple fields. The schema definition could look like:

type Place {
  name: String! @unique(index: "unique_place")
  administrative: String! @unique(index: "unique_place")
  country: String! @unique(index: "unique_place")
}

At the DB level, it should create one Index matching multiple terms.

Workaround

While you wait for this new feature to be released, here are some ways to work around it. If you have implemented your own solution/s that you think could help other users, please share them as replies to this topic and/or make a gist and link to it from the awesome-faunadb list.

Create a compound unique Index through FQL. All constraints will work as expected when querying through the GraphQL later on.

Example

1. Import schema:

type Place {
  name: String!
  administrative: String!
  country: String!
}

2. Create compound Index through FQL:

CreateIndex(
  {
    "name": "unique_place",
    "unique": true,
    "source": Collection("Place"),
    "terms": [
      {
        "field": [
          "data",
          "name"
        ]
      },
      {
        "field": [
          "data",
          "administrative"
        ]
      },
      {
        "field": [
          "data",
          "country"
        ]
      }
    ]
  }
)