Hi @ms007 and welcome!
To perform any kind of compute which is accessible in your GraphQL API, you will need to add a Query
field and implement a customer resolver with the @resolver
directive. The customer resolver is defined using FQL (v4)
The index for the relationship should already be created for you when you upload your schema. Make sure you are using @relation
for your fields (all lowercase) and not @Relation
.
You can also use the @embedded
directive to create a type to return without it being associated with a Collection.
For example, with the following schema:
type Todo {
title: String!
description: String!
userId: String!
list: List! @relation
completed: Boolean
}
type List {
name: String!
todos: [Todo] @relation
}
type ListSummary @embedded {
list: List!
todoCount: Int!
}
type Query {
summarizeLists: [ListSummary!]! @resolver(name: "summarizeLists", paginated: true)
}
Your query might look like this:
query {
summarizeLists(_size: 50) {
data {
list {
_id
name
}
todoCount
}
after
}
}
Alternative: Store count of Todos in the List
You can add a field to the List type that stores the count of Todos. This would require you to update the related list every time that a Todo is created or deleted, though. You can do that override by using https://docs.fauna.com/fauna/current/api/graphql/directives/d_generateudfresolvers
Alternative: Use the latest version of FQL (v10)
The latest version of FQL has a projection syntax that makes it similar to GraphQL. So instead of defining some things in GraphQL and some things in FQL, you could define everything and use FQL.
Create Collections and Indexes
Collection.create({ name: "User" })
Collection.create({ name: "List" })
Collection.create({
name: "Todo",
indexes: {
byUser: {
terms: [{ field: "user" }]
},
byList: {
terms: [{ field: "list" }]
}
}
})
Create some initial data (Assumes users already exist)
let me = User.byId("368697182365155408")
let list1 = List.create({ name: "My Favorite Things" })
[
{ title: "Raindrops on roses", user: me, list: list1 },
{ title: "Whiskers on kittens", user: me, list: list1 },
].map(data => Todo.create(data))
Run the query
List.all().map(list => list {
id,
ts,
name,
todoCount: Todo.byList(list).count()
})
>> SUCCESS
{
data: [
{
id: "368697327727149137",
ts: Time("2023-06-27T14:27:01.980Z"),
name: "My Favorite Things",
todoCount: 2
}
]
}
Check out the beta for our new dashboard here, and beta documentation here.