Sure! I’m using Rescript, so the style will look a bit weird, but undearneath it’s just apollo-client@3.5.8
The result of writeQuery
is a synchronous { __ref: "ROOT_QUERY" }
, so it’s probably just ignorance on my part as how to wait for that operation to complete. I don’t see any callbacks I can wait for like queries and mutations.
After I call my onCompleted
, my app pushes a new route to transition to the new element. The detail view 404’s for about 200 milliseconds before showing the item.
Here’s the code. I omitted some of lines of error handling for brevity:
let useReadTrip = () => {
React.useCallback((cache, tripId) => {
switch cache.readQuery(~query=GetTrip, {id: tripId}) {
| Some(Belt.Result.Ok({trip})) => Belt.Result.Ok(trip)
...
}
})
}
let useWriteTrip = () => {
React.useCallback((cache, trip) => {
cache.writeQuery(~query=GetTrip, {id: trip.id}, ~data={trip})
})
}
let useCreateTripElement = (~input, ~onCompleted, ~onError) => {
let readTrip = useReadTrip()
let writeTrip = useWriteTrip()
let update = React.useCallback1((cache, result) => {
switch (result.data, result.error) {
| (Some({createElement: element}), None) =>
readTrip(cache, input.tripId)
->Belt.Result.map(trip => {...trip, elements: [...trip.elements, element]})
->Belt.Result.map(trip =>
writeTrip(cache, trip)->(
result => {
// TODO here: how can I know when writeTrip above is written to the cache?
onCompleted(element)
}
)
)
->ignore
...
}
}, [input])
CreateElement.useWithVariables({input}, ~update, ~onError)
}
The readQuery, writeQuery, and a useQuery hook all use this query:
query GetTrip($id: ID!) {
trip(id: $id) {
...Trip
}
}
My detail view is just a few levels down in React, grabbing the element out of the useQuery
’s trip.elements