apollo-offline-hooks
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -260,2 +260,3 @@ 'use strict'; | ||
var setOfflineConfig = setConfig; | ||
var updateApolloCache = updateCache; | ||
var useMutation = function useMutation(mutation, _a) { | ||
@@ -307,4 +308,5 @@ if (_a === void 0) { | ||
exports.setOfflineConfig = setOfflineConfig; | ||
exports.updateApolloCache = updateApolloCache; | ||
exports.useMutation = useMutation; | ||
exports.useSubscription = useSubscription; | ||
//# sourceMappingURL=apollo-offline-hooks.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,r,t=require("tslib"),i=require("@apollo/react-hooks"),a=require("apollo-utilities"),o=(e=require("immer"))&&"object"==typeof e&&"default"in e?e.default:e;(r=exports.OperationTypes||(exports.OperationTypes={})).AUTO="auto",r.ADD="add",r.REMOVE="remove",r.UPDATE="update";var n=["delete","deleted","discard","discarded","erase","erased","remove","removed"],u=["update","updated","upsert","upserted","edit","edited","modify","modified","analyze","activate"],p=["create","created","put","set","add","added","new","insert","inserted","duplicate","import"],s=function(e){return null!=e&&"object"==typeof e},d={prefixesForRemove:n,prefixesForUpdate:u,prefixesForAdd:p,idField:"id"},c=function(e,r){return t.__assign(t.__assign({},e),(i=r,a=Object.keys(e).filter((function(e){return"__typename"!==e})),o={},i?(a.forEach((function(e){void 0!==i[e]&&(o[e]=i[e])})),o):o));var i,a,o},l=function(e){var r,i,n=e.client,u=e.data,p=e.idField,l=e.updateQuery,f=e.operationType,y=void 0===f?exports.OperationTypes.AUTO:f,v=e.mapResultToUpdate;if(u){var T=Object.keys(u)[0],m=y===exports.OperationTypes.AUTO?function(e){void 0===e&&(e="");var r=function(r){return e.toLowerCase().startsWith(r)||e.toLowerCase().startsWith("on"+r)},t=exports.OperationTypes.AUTO;return[[d.prefixesForAdd,exports.OperationTypes.ADD],[d.prefixesForRemove,exports.OperationTypes.REMOVE],[d.prefixesForUpdate,exports.OperationTypes.UPDATE]].forEach((function(e){var i=e[1];e[0].some(r)&&(t=i)})),t}(T):y;if(u[T]){var x,_=v?v(u):u[T],O=l.query||l,A=l.variables||{},F=a.resultKeyNameFromField(O.definitions[0].selectionSet.selections[0]);try{x=n.readQuery({query:O,variables:A})}catch(e){return}if(x){var b=function(e,r){switch(e){case exports.OperationTypes.ADD:return function(e,i){return Array.isArray(e)?i?t.__spreadArrays(e.filter((function(e){return e[r]!==i[r]})),[i]):t.__spreadArrays(e):i};case exports.OperationTypes.UPDATE:return function(e,i){return Array.isArray(e)?i?e.map((function(e){return e[r]===i[r]?c(e,i):e})):t.__spreadArrays(e):c(e,i)};case exports.OperationTypes.REMOVE:return function(e,i){return Array.isArray(e)?i?e.filter((function(e){return e[r]!==i[r]})):t.__spreadArrays(e):null};default:return function(e){return e}}}(m,p||(null===(i=(r=d).getIdFieldFromObject)||void 0===i?void 0:i.call(r,_))||d.idField),U=o(x,(function(e){var r=e[F],t=function e(r,t){return void 0===t&&(t=[]),Array.isArray(r)?t:s(r)?(Object.keys(r).some((function(a){var o=e(r[a],t.concat(a));return!!o&&(i=o,!0)})),i):void 0;var i}(r),i=b(function(e,r){return s(e)&&r&&0!==r.length?r.reduce((function(e,r){var t,i=null===(t=e)||void 0===t?void 0:t[r];return void 0!==i?i:null}),e):e}(r,t),_);t&&0!==t.length?function(e,r,t){void 0===r&&(r=[]),r.reduce((function(r,i,a,o){return o.length-1===a?(r[i]=t,e):r[i]}),e)}(r,t,i):e[F]=i}));n.writeQuery({query:O,variables:A,data:U})}}}};Object.keys(i).forEach((function(e){"default"!==e&&Object.defineProperty(exports,e,{enumerable:!0,get:function(){return i[e]}})})),exports.prefixesForAdd=p,exports.prefixesForRemove=n,exports.prefixesForUpdate=u,exports.setOfflineConfig=function(e){return Object.assign(d,e)},exports.useMutation=function(e,r){void 0===r&&(r={});var a=r.updateQuery,o=r.idField,n=r.operationType,u=r.mapResultToUpdate,p=t.__rest(r,["updateQuery","idField","operationType","mapResultToUpdate"]),s=i.useMutation(e,p),d=s[0];return[function(e){return d(function(e){var r=e.updateQuery,i=e.idField,a=e.operationType,o=e.mapResultToUpdate,n=t.__rest(e,["updateQuery","idField","operationType","mapResultToUpdate"]);return!r||n.update?n:t.__assign({update:function(e,t){l({client:e,data:t.data,idField:i,mapResultToUpdate:o,operationType:a,updateQuery:r})}},n)}(t.__assign({updateQuery:a,idField:o,operationType:n,mapResultToUpdate:u},e)))},s[1]]},exports.useSubscription=function(e,r){return void 0===r&&(r={}),i.useSubscription(e,(o=(a=r).updateQuery,n=a.idField,u=a.operationType,p=a.mapResultToUpdate,s=t.__rest(a,["updateQuery","idField","operationType","mapResultToUpdate"]),!o||s.onSubscriptionData?s:t.__assign({onSubscriptionData:function(e){l({client:e.client,data:e.subscriptionData.data,updateQuery:o,operationType:u,idField:n,mapResultToUpdate:p})}},s)));var a,o,n,u,p,s}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t,r=require("tslib"),i=require("@apollo/react-hooks"),a=require("apollo-utilities"),o=(e=require("immer"))&&"object"==typeof e&&"default"in e?e.default:e;(t=exports.OperationTypes||(exports.OperationTypes={})).AUTO="auto",t.ADD="add",t.REMOVE="remove",t.UPDATE="update";var n=["delete","deleted","discard","discarded","erase","erased","remove","removed"],u=["update","updated","upsert","upserted","edit","edited","modify","modified","analyze","activate"],p=["create","created","put","set","add","added","new","insert","inserted","duplicate","import"],s=function(e){return null!=e&&"object"==typeof e},d={prefixesForRemove:n,prefixesForUpdate:u,prefixesForAdd:p,idField:"id"},c=function(e,t){return r.__assign(r.__assign({},e),(i=t,a=Object.keys(e).filter((function(e){return"__typename"!==e})),o={},i?(a.forEach((function(e){void 0!==i[e]&&(o[e]=i[e])})),o):o));var i,a,o},l=function(e){var t,i,n=e.client,u=e.data,p=e.idField,l=e.updateQuery,f=e.operationType,y=void 0===f?exports.OperationTypes.AUTO:f,v=e.mapResultToUpdate;if(u){var T=Object.keys(u)[0],m=y===exports.OperationTypes.AUTO?function(e){void 0===e&&(e="");var t=function(t){return e.toLowerCase().startsWith(t)||e.toLowerCase().startsWith("on"+t)},r=exports.OperationTypes.AUTO;return[[d.prefixesForAdd,exports.OperationTypes.ADD],[d.prefixesForRemove,exports.OperationTypes.REMOVE],[d.prefixesForUpdate,exports.OperationTypes.UPDATE]].forEach((function(e){var i=e[1];e[0].some(t)&&(r=i)})),r}(T):y;if(u[T]){var x,_=v?v(u):u[T],O=l.query||l,A=l.variables||{},F=a.resultKeyNameFromField(O.definitions[0].selectionSet.selections[0]);try{x=n.readQuery({query:O,variables:A})}catch(e){return}if(x){var b=function(e,t){switch(e){case exports.OperationTypes.ADD:return function(e,i){return Array.isArray(e)?i?r.__spreadArrays(e.filter((function(e){return e[t]!==i[t]})),[i]):r.__spreadArrays(e):i};case exports.OperationTypes.UPDATE:return function(e,i){return Array.isArray(e)?i?e.map((function(e){return e[t]===i[t]?c(e,i):e})):r.__spreadArrays(e):c(e,i)};case exports.OperationTypes.REMOVE:return function(e,i){return Array.isArray(e)?i?e.filter((function(e){return e[t]!==i[t]})):r.__spreadArrays(e):null};default:return function(e){return e}}}(m,p||(null===(i=(t=d).getIdFieldFromObject)||void 0===i?void 0:i.call(t,_))||d.idField),U=o(x,(function(e){var t=e[F],r=function e(t,r){return void 0===r&&(r=[]),Array.isArray(t)?r:s(t)?(Object.keys(t).some((function(a){var o=e(t[a],r.concat(a));return!!o&&(i=o,!0)})),i):void 0;var i}(t),i=b(function(e,t){return s(e)&&t&&0!==t.length?t.reduce((function(e,t){var r,i=null===(r=e)||void 0===r?void 0:r[t];return void 0!==i?i:null}),e):e}(t,r),_);r&&0!==r.length?function(e,t,r){void 0===t&&(t=[]),t.reduce((function(t,i,a,o){return o.length-1===a?(t[i]=r,e):t[i]}),e)}(t,r,i):e[F]=i}));n.writeQuery({query:O,variables:A,data:U})}}}},f=l;Object.keys(i).forEach((function(e){"default"!==e&&Object.defineProperty(exports,e,{enumerable:!0,get:function(){return i[e]}})})),exports.prefixesForAdd=p,exports.prefixesForRemove=n,exports.prefixesForUpdate=u,exports.setOfflineConfig=function(e){return Object.assign(d,e)},exports.updateApolloCache=f,exports.useMutation=function(e,t){void 0===t&&(t={});var a=t.updateQuery,o=t.idField,n=t.operationType,u=t.mapResultToUpdate,p=r.__rest(t,["updateQuery","idField","operationType","mapResultToUpdate"]),s=i.useMutation(e,p),d=s[0];return[function(e){return d(function(e){var t=e.updateQuery,i=e.idField,a=e.operationType,o=e.mapResultToUpdate,n=r.__rest(e,["updateQuery","idField","operationType","mapResultToUpdate"]);return!t||n.update?n:r.__assign({update:function(e,r){l({client:e,data:r.data,idField:i,mapResultToUpdate:o,operationType:a,updateQuery:t})}},n)}(r.__assign({updateQuery:a,idField:o,operationType:n,mapResultToUpdate:u},e)))},s[1]]},exports.useSubscription=function(e,t){return void 0===t&&(t={}),i.useSubscription(e,(o=(a=t).updateQuery,n=a.idField,u=a.operationType,p=a.mapResultToUpdate,s=r.__rest(a,["updateQuery","idField","operationType","mapResultToUpdate"]),!o||s.onSubscriptionData?s:r.__assign({onSubscriptionData:function(e){l({client:e.client,data:e.subscriptionData.data,updateQuery:o,operationType:u,idField:n,mapResultToUpdate:p})}},s)));var a,o,n,u,p,s}; | ||
//# sourceMappingURL=apollo-offline-hooks.cjs.production.min.js.map |
@@ -257,2 +257,3 @@ import { __rest, __assign, __spreadArrays } from 'tslib'; | ||
var setOfflineConfig = setConfig; | ||
var updateApolloCache = updateCache; | ||
var useMutation = function useMutation(mutation, _a) { | ||
@@ -292,3 +293,3 @@ if (_a === void 0) { | ||
export { OperationTypes, prefixesForAdd, prefixesForRemove, prefixesForUpdate, setOfflineConfig, useMutation, useSubscription }; | ||
export { OperationTypes, prefixesForAdd, prefixesForRemove, prefixesForUpdate, setOfflineConfig, updateApolloCache, useMutation, useSubscription }; | ||
//# sourceMappingURL=apollo-offline-hooks.esm.js.map |
import * as ApolloReactCommon from '@apollo/react-common'; | ||
import { BaseSubscriptionOptions, ExecutionResult, MutationResult, OperationVariables } from '@apollo/react-common'; | ||
import * as ApolloReactHooks from '@apollo/react-hooks'; | ||
import { OfflineOptions } from './offline'; | ||
import * as offline from './offline'; | ||
import { DocumentNode } from 'graphql'; | ||
export * from './const'; | ||
export * from '@apollo/react-hooks'; | ||
export declare const setOfflineConfig: (config: import("./offline").OfflineConfig) => import("./offline").OfflineConfig; | ||
export declare type OfflineOptions<TData> = offline.OfflineOptions<TData>; | ||
export declare const setOfflineConfig: (config: offline.OfflineConfig) => offline.OfflineConfig; | ||
export declare const updateApolloCache: <TData = any>({ client, data, idField, updateQuery, operationType, mapResultToUpdate }: offline.OfflineOptions<TData> & { | ||
client: import("apollo-cache").DataProxy | import("apollo-client").ApolloClient<any>; | ||
data: TData; | ||
}) => void; | ||
export declare type MutationHookOptions<TData, TVariables> = ApolloReactHooks.MutationHookOptions<TData, TVariables> & OfflineOptions<TData>; | ||
@@ -10,0 +15,0 @@ export declare type MutationFunctionOptions<TData, TVariables> = ApolloReactCommon.MutationFunctionOptions<TData, TVariables> & OfflineOptions<TData>; |
{ | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "name": "apollo-offline-hooks", |
@@ -39,3 +39,3 @@ # Apollo Offline Hooks | ||
This package extends `useMutation` options allowing to update cached queries in one line of code instead of writing complex `update` functions. | ||
This package extends [useMutation](https://www.apollographql.com/docs/react/api/react-hooks/#options-2) options allowing to update cached queries in one line of code instead of writing complex `update` functions. | ||
@@ -55,3 +55,3 @@ For example this code | ||
const [createTodo] = useMutation(createTodoMutation, { | ||
updateQuery: todosQuery // <== notice updateQuery option | ||
updateQuery: todosQuery // <== pass a gql query you want to update | ||
}); | ||
@@ -100,9 +100,13 @@ | ||
const newTodo = data.createTodo; | ||
const cache = proxy.readQuery({query: todosQuery}); | ||
proxy.writeQuery({ | ||
query: todosQuery, | ||
data: { | ||
todos: [...cache.todos, newTodo] | ||
} | ||
}); | ||
try { | ||
const cache = proxy.readQuery({query: todosQuery}); | ||
proxy.writeQuery({ | ||
query: todosQuery, | ||
data: { | ||
todos: [...cache.todos, newTodo] | ||
} | ||
}); | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
@@ -135,3 +139,3 @@ }); | ||
const [deleteTodo] = useMutation(deleteTodoMutation, { | ||
updateQuery: todosQuery // <== notice updateQuery option | ||
updateQuery: todosQuery, | ||
@@ -187,9 +191,13 @@ // to delete an item we need to provide it's id | ||
update: proxy => { | ||
const cache = proxy.readQuery({query: todosQuery}); | ||
proxy.writeQuery({ | ||
query: todosQuery, | ||
data: { | ||
todos: cache.todos.filter(item => item.id !== todo.id) | ||
} | ||
}); | ||
try { | ||
const cache = proxy.readQuery({query: todosQuery}); | ||
proxy.writeQuery({ | ||
query: todosQuery, | ||
data: { | ||
todos: cache.todos.filter(item => item.id !== todo.id) | ||
} | ||
}); | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
@@ -201,2 +209,4 @@ }); | ||
// apollo client is clever enough to update an item in cache | ||
// although if you want to update an item with different type you'll have to write | ||
// a manual update function | ||
return updateTodo({ | ||
@@ -227,2 +237,4 @@ variables: {id: todo.id, done: !todo.done} | ||
[Other options](https://www.apollographql.com/docs/react/api/react-hooks/#options-2) | ||
Offline options can be passed to the `useMutation` hook or to the mutation function directly. | ||
@@ -265,2 +277,4 @@ | ||
[Other options](https://www.apollographql.com/docs/react/api/react-hooks/#options-3) | ||
## Customize default configurations | ||
@@ -295,3 +309,51 @@ | ||
## `updateApolloCache` directly | ||
This package also exposes `updateApolloCache` function directly, that can be used to build custom implementations | ||
Example | ||
```typescript | ||
import {updateApolloCache} from 'apollo-offline-hooks'; | ||
const newTodo = { | ||
__typename: 'Todo', | ||
id: 1, | ||
task: 'New todo', | ||
createdAt: new Date().toISOString() | ||
}; | ||
updateApolloCache({ | ||
client, | ||
data: {createTodo: newTodo}, | ||
updateQuery: todosQuery | ||
}); | ||
``` | ||
Function signature | ||
```typescript | ||
type OfflineOptions<TData> = { | ||
updateQuery?: QueryWithVariables | DocumentNode; | ||
idField?: string; | ||
operationType?: OperationTypes; | ||
mapResultToUpdate?(data: NonNullable<TData>): Item; | ||
}; | ||
type UpdateCacheOptions<TData = any> = OfflineOptions<TData> & { | ||
client: ApolloClient<any> | DataProxy; | ||
data: TData; | ||
}; | ||
const updateApolloCache: <TData = any>({ | ||
client, | ||
data, | ||
idField, | ||
updateQuery, | ||
operationType, | ||
mapResultToUpdate | ||
}: UpdateCacheOptions<TData>) => void; | ||
``` | ||
## Credits | ||
This package is based on [Amplify Offline Helpers](https://github.com/awslabs/aws-mobile-appsync-sdk-js/blob/master/OFFLINE_HELPERS.md) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
100500
657
351