@@ -37,7 +37,8 @@ const graphQLClient = new GraphQLClient(process.env.BASE_PATH + '/graphql', {
3737 credentials : 'include'
3838} )
3939
40- const graphqlFetcher = ( query , ...args ) => {
40+ const graphqlFetcher = ( key ) => {
41+ const [ query , ...args ] = key
4142 // Creates an object. Odd indexes are keys and even indexes are values.
4243 // Needs to be flat to avoid unnecessary rerendering since swr does shallow comparison.
4344 const variables = [ ...( args || [ ] ) ] . reduce ( ( acc , arg , index , arr ) => {
@@ -66,7 +67,7 @@ export const useMutateApi = (operation) => {
6667 setLoading ( true )
6768
6869 try {
69- const data = await graphqlFetcher ( operation . query , ...flatVars )
70+ const data = await graphqlFetcher ( [ operation . query , ...flatVars ] )
7071
7172 setState ( { ...state , errors : null , data } )
7273 } catch ( error ) {
@@ -87,20 +88,17 @@ export const useMatchMutate = () => {
8788 throw new Error ( 'matchMutate requires the cache provider to be a Map instance' )
8889 }
8990
90- const flatVars = toPairs ( args ) . flat ( )
91- const argKey = unstableSerialize ( flatVars ) . slice ( 1 )
92-
9391 let matchingKey
9492
9593 for ( const key of cache . keys ( ) ) {
9694 if ( key . includes ( operation ) ) {
9795 const value = cache . get ( key )
9896
99- if ( ! value || ! value [ operation ] ) {
97+ if ( ! value ) {
10098 continue
10199 }
102100
103- if ( ! argKey || key . includes ( argKey ) ) {
101+ if ( key === unstableSerialize ( value . _k ) ) {
104102 matchingKey = key
105103 break
106104 }
@@ -110,7 +108,7 @@ export const useMatchMutate = () => {
110108 if ( ! matchingKey ) return ( ) => { }
111109
112110 return executor ( cache . get ( matchingKey ) , ( ...args ) => {
113- mutate ( matchingKey , ... args )
111+ mutate ( matchingKey , args [ 0 ] )
114112 } )
115113 }
116114}
0 commit comments