Use a custom index for a relationship in GraphQL

When you fetch the relationship like so:

{
  user {
    todos {
      _id
      name
      sortOrder
    }
  }
}

I’d like to be able to use a custom index because I want to filter the todos in this case to remove the documents with deletedAt and also to be able to sort them according to their sortOrder.

Filtering at nested levels is not currently possible. That would require either:

This is indeed unfortunate – the query that you show is the most idiomatic thing to do in GraphQL. The two feature requests I linked to are on the roadmap, but they are behind other features that should be landing soon. Until then the best workaround is to use a UDF to resolve the relationships and filters in one step. I believe that this is what you are doing in this post: How to handle deletedAt in Graphql and faunadb? - #5 by aprilmintacpineda

{
  getUserGoals {
    data {
      name
      deletedAt
    }
  }
}

I believe that this is what you are doing in this post: How to handle deletedAt in Graphql and faunadb? - #5 by aprilmintacpineda

Correct.

getUserGoals is a custom query. However, that’s a query on its own, what I need is for the same thing to work for relationships as well.

So currently, to get all goals of the user sorted by sortOrder (desc) and also filtered by deletedAt (only shows goals that are not deleted), we do:

{
  getUserGoals {
    data {
      _id
      name
      sortOrder
    }
  }
}

But, we also have a query to fetch user data, it’s also a custom query getUserData, the function that powers it is as follows:

Update(Function("getUserData"), {
  role: "admin",
  body: Query(
    Lambda(
      "",
      Get(CurrentIdentity())
    )
  )
});

The question is: How can I do the following query:

{
  getUserData {
    _id
    email
    firstName
    lastName
    goals {
      data {
        _id
        name
        sortOrder
      }
    }
  }
}

and get the same effect? That is, the goal should be sorted by sortOrder (descending) and it should only give me goals that are not deleted.

If you need to get both results at the same time, then you might consider a new resolver that returns an @embedded type

type GetUserDataPayload @embedded {
  user: User!
  goals: [Goal]!
}

hmmm, I’ll give that a try.

Hi @aprilmintacpineda! Were you able to get the embedded type to work?

One other thing I just thought of is that you can make multiple top-level queries in the same request.

{
  me: findUserByID(id: "1234") {  # Or perhaps a custom resolver that returns CurrentIdentity
    _id
    email
    firstName
    lastName
  }  

  myTopGoals: getUserGoals {
    data {
      _id
      name
      sortOrder
    }
  }
}
1 Like

That’s also a really good solution.