I'm asking this question because I wasn't able to find any ressource on the web (including SO, but also the documentation of Apollo-Client which is quite laconic on the subject), and I'm a out of solution.
I've got this client-side schema :
const typeDefs = gql'
extend type Query @client {
getMessageSliceByIdMessaging(id_messaging: String!): [MESSAGE_SLICE]!,
}
type MESSAGE_SLICE {
_id: String!,
__typename: String!,
newest_date_creation: String!,
oldest_date_creation: String!,
list_id_message: [MESSAGE]!,
},
'
I've made this typePolicies in the InMemoryCache
typePolicies: {
Query: {
fields: {
getMessageSliceByIdMessaging : {
merge(existing = [], incoming = [], {cache}) {
//Doing some shenanigans
return some_shenanigans
},
read(existing = []) {
return [...existing] //existing properly return "some_shenanigans" generate in merge function
},
}
}
}
}
But, despite the read function returning the correct "some_shenanigans", when I'm calling :
///FRAGMENT OF MESSAGE_SLICE
const MESSAGE_SLICE_FRAGMENT = gql`
fragment MessageSliceFragment on MESSAGE_SLICE {
_id,
newest_date_creation,
oldest_date_creation,
list_id_message {
...messageFragment,
},
},
${MESSAGE_FRAGMENT}
`
///QUERY TO GET A LIST OF MESSAGE_SLICE
const GET_MESSAGE_SLICE_BY_ID_MESSAGING = gql`
query getMessageSliceByIdMessaging (
$id_messaging: String!,
) {
getMessageSliceByIdMessaging @client (
id_messaging: $id_messaging,
) {
...MessageSliceFragment,
},
},
${MESSAGE_SLICE_FRAGMENT}
`
///CALL OF THE QUERY
const messageSliceListData = useQuery(GET_MESSAGE_SLICE_BY_ID_MESSAGING, {
variables: {
id_messaging: route.id_messaging,
},
})
///CALLING THIS ALLOW TO PAST DATA TO THE MERGER (missing context, but work properly)
await client.writeQuery({
query: GET_MESSAGE_SLICE_BY_ID_MESSAGING,
variables: {
id_messaging,
},
data: {getMessageSliceByIdMessaging: [...SOME_ARRAY_DATA_THAT_NEED_TO_BE_MERGE_IN_THE_TYPE_POLICIES]}
})
messageSliceListData?.data
always return [{}]
or undefined
, and I don't get any Error.
Some thing that I've tried :
- Returning
some_shenanigans
as a reference (usingtoReference
), - Returning
some_shenanigans
as raw data.
I wasn't able to find a tutorial or other sources treating this question with Apollo 3.0 and typePolicies (apart from this link: https://www.apollographql.com/tutorials/fullstack-quickstart/10-managing-local-state, but it's using reactives variables
, not typePolicies
)
So what is the proper way to implement such a solution ?
N.B : I'm using this in a React-Native App, and I'm not in capacity to use the Apollo Developer Tools.