Hi @boxheed and welcome!
There should be no reason why you cannot use the getters provided to extract the ID in your java client. That is, the ID is intended to be a part of the Ref, and you can rely on using the getters in your production code. EDIT: This should be possible, I am checking what that way should be if not through casting to RefV
, which is warned against in the Javadocs.
Other techniques using FQL
Another thing you can do is Select
for the ID using FQL. I know some folks prefer to do it this way so it doesn’t have to be done client-side.
Here is some pure FQL that demonstrates getting the ID and even the Collection name if you desire.
Let(
{
thing_doc: Create(Collection("things"), { data: { value: true } }),
thing_ref: Select("ref", Var("thing_doc")),
thing_collection_name: Select(["ref", "collection", "id"], Var("thing_doc")),
thing_id: Select(["ref", "id"], Var("thing_doc"))
},
{
ref: Var("thing_ref"),
collection_name: Var("thing_collection_name"),
id: Var("thing_id"),
}
)
// result
{
ref: Ref(Collection("things"), "326390596491018306"),
collection_name: "things",
id: "326390596491018306"
}
similar to responses created by Fauna’s GraphQL API, you can stuff the Ref’s ID and ts
field into the document and just return that, which is popular with some folks.
Let(
{
thing_doc: Create(Collection("things"), { data: { value: true } }),
thing_id: Select(["ref", "id"], Var("thing_doc")),
thing_ts: Select(["ts"], Var("thing_doc")),
thing_data: Select(["data"], Var("thing_doc"))
},
Merge(Var("thing_data"), {
_id: Var("thing_id"),
_ts: Var("thing_ts")
})
)
// result
{
value: true,
_id: "326414080735707202",
_ts: 1647551575330000
}
I personally prefer passing around the whole Ref object, since you can pass it right back in to additional queries. Where your application needs the ID as a value, I like to extract out at the last moment, where the app state still stores the whole Ref. But what you want to do is up to you and what is best for your use case.