Can Indexes use terms matching nested reference fields?

Hello,

I may have just missed it, but I could not find a clear answer to this in the documentation or searching the forums.

E.g., if I have these two collections defined in a GraphQL schema…

type Comment {
    author: Author!
    message: String!
}

type Author {
    firstName: String!
    lastName: String!
}

Can I create an Index for the Comment collection with terms that can filter by, say, the author field’s nested firstName field?

I’ve tried both of these approaches, but both return zero matches when I test.

// with extra "data" field
CreateIndex({
  name: "comments_by_firstname_a",
  source: Collection("Comment"),
  terms: [
    { field: ["data", "author", "data", "firstName"] }
  ]
})

// without extra "data" field
CreateIndex({
  name: "comments_by_firstname_b",
  source: Collection("Comment"),
  terms: [
    { field: ["data", "author", "firstName"] }
  ]
})

Is something like this possible, or will I need to call on multiple indexes and Join / Intersection the results together?

Thanks,
Ross

The short answer is no. But hopefully I can help explain some more.

Indexes work only on the data is contained within a document. Bindings are possible to compute, but those functions have to be pure and cannot read any other Documents.

To see what you can Index when using GraphQL, you must know how the relationships are stored. Check that out in the docs for GraphQL Replationships: GraphQL relationships | Fauna Documentation

In a one-to-many relationship, a reference is only stored in the many-side. Traversing the relationship from both directions is possible with indexes.

First, make sure you use the @relation directive.

type Comment {
    author: Author! @ relation
    message: String!
}

type Author {
    firstName: String!
    lastName: String!
    comments: [Comment] @relation
}

An Author document and a Comment document might look like this:

{
  ref: Ref(Collection("Author"), "5555555"),
  ts: 1631567921050000,
  data: {
    firstName: "Paul"
    lastName: "P"
  }
}
{
  ref: Ref(Collection("Comment"), "12345678"),
  ts: 1631567921050000,
  data: {
    author: Ref(Collection("Author"), "5555555"),
    message: "Hello!"
  }
}

You can see from the Documents that the Comment Documents contain the author Ref. But Refs do not have data fields, so you cannot index on them that way.