Hello all! I’m quite new to FQL and I tried to write a FQL function for deleting items based on the nested field. I have following types:
type User {
name: String
email: String! @unique
hashedPassword: String
role: String!
}
type Token {
hashedToken: String!
type: String!
expiresAt: Time!
sentTo: String!
user: User! @relation
}
And I would like to create a FQL function for deleting Tokens by ID of the user. I want the GraphQL mutation that would use this function as a resolver to have this signature:
type Mutation {
deleteTokensByUserId(userId: ID!): [Token] @resolver(name: "delete_tokens_by_user_id")
}
After some try and error I came up with the following solution:
CreateIndex({
name: "all_tokens_by_user_id",
source: Collection("Token"),
terms: [
{ field: ["data", "user", "id"] }
]
})
CreateFunction({
name: "delete_tokens_by_user_id",
body: Query(
Lambda(
["userId"],
Let(
{
tokenDoc: Map(
Paginate(Match(Index("all_tokens_by_user_id"), Var("userId"))),
Lambda("tokenRef", Delete(Var("tokenRef")))
)
},
Select(["data"], Var("tokenDoc"))
)
)
)
})
The function works and returns correct data type, but I have a gut feeling, that it can be written in a smarter, more concise way.
Could you point me in some direction? Thanks in advance!