Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@urql/exchange-graphcache

Package Overview
Dependencies
Maintainers
3
Versions
298
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@urql/exchange-graphcache - npm Package Compare versions

Comparing version 1.0.0-beta.1 to 1.0.0-beta.2

dist/types/store.d.ts

2

dist/types/exchange.d.ts

@@ -9,2 +9,2 @@ import { Exchange } from 'urql';

}
export declare const cacheExchange: (opts: CacheExchangeOpts) => Exchange;
export declare const cacheExchange: (opts?: CacheExchangeOpts | undefined) => Exchange;

@@ -9,3 +9,3 @@ import { FieldNode } from 'graphql';

}
export declare const forEachFieldNode: (typename: string, entityKey: string, select: readonly import("graphql").SelectionNode[], ctx: Context, cb: (node: FieldNode) => void) => void;
export declare const forEachFieldNode: (typename: string | void, entityKey: string, select: readonly import("graphql").SelectionNode[], ctx: Context, cb: (node: FieldNode) => void) => void;
export {};

@@ -149,2 +149,239 @@ import { Kind } from "graphql";

function _extends() {
return (_extends = Object.assign || function(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
}).apply(this, arguments);
}
var isFragmentMatching = function(node, typename, entityKey, ctx) {
if (!typename) {
return !1;
} else if (typename === (void 0 !== (typeCondition = node.typeCondition) ? getName(typeCondition) : null)) {
return !0;
}
var typeCondition;
return !getSelectionSet(node).some(function(node) {
if (!isFieldNode(node)) {
return !1;
}
var fieldName = getName(node);
var fieldArgs = getFieldArguments(node, ctx.variables);
var fieldKey = keyOfField(fieldName, fieldArgs);
return !ctx.store.hasField(joinKeys(entityKey, fieldKey));
});
};
var forEachFieldNode = function(typename, entityKey, select, ctx, cb) {
select.forEach(function(node) {
if (!function(node, vars) {
if (void 0 === node.directives) {
return !0;
}
return !node.directives.some(function(directive) {
var name = getName(directive);
var isInclude = "include" === name;
if (!isInclude && "skip" !== name) {
return !1;
}
var arg = directive.arguments ? directive.arguments[0] : null;
if (!arg || "if" !== getName(arg)) {
return !1;
}
var value = evaluateValueNode(arg.value, vars);
if ("boolean" != typeof value && null !== value) {
return !1;
}
return isInclude ? !value : !!value;
});
}(node, ctx.variables)) {
return;
} else if (!isFieldNode(node)) {
var fragmentNode = function(node) {
return node.kind === Kind.INLINE_FRAGMENT;
}(node) ? node : ctx.fragments[getName(node)];
if (void 0 !== fragmentNode && isFragmentMatching(fragmentNode, typename, entityKey, ctx)) {
var fragmentSelect = getSelectionSet(fragmentNode);
forEachFieldNode(typename, entityKey, fragmentSelect, ctx, cb);
}
} else if ("__typename" !== getName(node)) {
cb(node);
}
});
};
var write = function(store, request, data) {
initStoreState(0);
var operation = getMainOperation(request.query);
var result = {
dependencies: getCurrentDependencies()
};
var ctx = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result: result,
store: store
};
var select = getSelectionSet(operation);
var operationName = getOperationName(operation);
if ("Query" === operationName) {
writeSelection(ctx, operationName, select, data);
} else {
writeRoot(ctx, operationName, select, data);
}
clearStoreState();
return result;
};
var writeOptimistic = function(store, request, optimisticKey) {
initStoreState(optimisticKey);
var operation = getMainOperation(request.query);
var result = {
dependencies: getCurrentDependencies()
};
var ctx = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result: result,
store: store
};
var operationName = getOperationName(operation);
if ("Mutation" === operationName) {
var select = getSelectionSet(operation);
forEachFieldNode(operationName, operationName, select, ctx, function _ref(node) {
if (void 0 !== node.selectionSet) {
var fieldName = getName(node);
var resolver = ctx.store.optimisticMutations[fieldName];
if (void 0 !== resolver) {
var fieldArgs = getFieldArguments(node, ctx.variables);
var fieldSelect = getSelectionSet(node);
var resolverValue = resolver(fieldArgs || {}, ctx.store, ctx);
if (!isScalar(resolverValue)) {
writeRootField(ctx, resolverValue, fieldSelect);
}
}
}
});
}
clearStoreState();
return result;
};
var writeFragment = function(store, query, data) {
var fragments = getFragments(query);
var fragment = fragments[Object.keys(fragments)[0]];
if ("production" !== process.env.NODE_ENV && void 0 === fragment) {
throw new Error("At least one fragment must be passed to writeFragment.");
}
var select = getSelectionSet(fragment);
var writeData = _extends({}, data, {
__typename: fragment.typeCondition.name.value
});
var entityKey = store.keyOfEntity(writeData);
if ("production" !== process.env.NODE_ENV && !entityKey) {
throw new Error('You have to pass an "id" or "_id" with your writeFragment data.');
}
var ctx = {
variables: {},
fragments: fragments,
result: {
dependencies: getCurrentDependencies()
},
store: store
};
writeSelection(ctx, entityKey, select, writeData);
};
var writeSelection = function(ctx, entityKey, select, data) {
var store = ctx.store;
var variables = ctx.variables;
var isQuery = "Query" === entityKey;
var typename = data.__typename;
if (!isQuery) {
addDependency(entityKey);
}
store.writeField(isQuery ? entityKey : typename, entityKey, "__typename");
forEachFieldNode(typename, entityKey, select, ctx, function(node) {
var fieldName = getName(node);
var fieldArgs = getFieldArguments(node, variables);
var fieldKey = joinKeys(entityKey, keyOfField(fieldName, fieldArgs));
var fieldValue = data[getFieldAlias(node)];
if (isQuery) {
addDependency(fieldKey);
}
if (void 0 === node.selectionSet) {
store.writeRecord(fieldValue, fieldKey);
} else if (!isScalar(fieldValue)) {
var link = writeField(ctx, fieldKey, node.selectionSet.selections, fieldValue);
store.writeLink(link, fieldKey);
store.removeRecord(fieldKey);
} else {
store.writeRecord(fieldValue, fieldKey);
}
});
};
var writeField = function(ctx, parentFieldKey, select, data) {
if (Array.isArray(data)) {
return data.map(function _ref2(item, index) {
var indexKey = joinKeys(parentFieldKey, "" + index);
return writeField(ctx, indexKey, select, item);
});
} else if (null === data) {
return null;
}
var entityKey = ctx.store.keyOfEntity(data);
var key = null !== entityKey ? entityKey : parentFieldKey;
writeSelection(ctx, key, select, data);
return key;
};
var writeRoot = function(ctx, typename, select, data) {
forEachFieldNode(typename, typename, select, ctx, function(node) {
var fieldName = getName(node);
var fieldAlias = getFieldAlias(node);
var fieldArgs = getFieldArguments(node, ctx.variables);
var fieldValue = data[fieldAlias];
if (void 0 !== node.selectionSet && null !== fieldValue && !isScalar(fieldValue)) {
writeRootField(ctx, fieldValue, node.selectionSet.selections);
}
if ("Mutation" === typename) {
var updater = ctx.store.updates[fieldName];
if (void 0 !== updater) {
updater(data, fieldArgs || {}, ctx.store, ctx);
}
}
});
};
var writeRootField = function(ctx, data, select) {
if (Array.isArray(data)) {
return data.map(function _ref3(item) {
return writeRootField(ctx, item, select);
});
} else if (null === data) {
return;
}
var entityKey = ctx.store.keyOfEntity(data);
if (null !== entityKey) {
writeSelection(ctx, entityKey, select, data);
} else {
writeRoot(ctx, data.__typename, select, data);
}
};
var isScalar = function(x) {
if (Array.isArray(x)) {
return x.some(isScalar);
}
return "object" != typeof x || null !== x && "string" != typeof x.__typename;
};
var currentDependencies = {

@@ -290,56 +527,2 @@ current: null

var isFragmentMatching = function(node, typename, entityKey, ctx) {
if (typename === (void 0 !== (typeCondition = node.typeCondition) ? getName(typeCondition) : null)) {
return !0;
}
var typeCondition;
return !getSelectionSet(node).some(function(node) {
if (!isFieldNode(node)) {
return !1;
}
var fieldName = getName(node);
var fieldArgs = getFieldArguments(node, ctx.variables);
var fieldKey = keyOfField(fieldName, fieldArgs);
return !ctx.store.hasField(joinKeys(entityKey, fieldKey));
});
};
var forEachFieldNode = function(typename, entityKey, select, ctx, cb) {
select.forEach(function(node) {
if (!function(node, vars) {
if (void 0 === node.directives) {
return !0;
}
return !node.directives.some(function(directive) {
var name = getName(directive);
var isInclude = "include" === name;
if (!isInclude && "skip" !== name) {
return !1;
}
var arg = directive.arguments ? directive.arguments[0] : null;
if (!arg || "if" !== getName(arg)) {
return !1;
}
var value = evaluateValueNode(arg.value, vars);
if ("boolean" != typeof value && null !== value) {
return !1;
}
return isInclude ? !value : !!value;
});
}(node, ctx.variables)) {
return;
} else if (!isFieldNode(node)) {
var fragmentNode = function(node) {
return node.kind === Kind.INLINE_FRAGMENT;
}(node) ? node : ctx.fragments[getName(node)];
if (void 0 !== fragmentNode && isFragmentMatching(fragmentNode, typename, entityKey, ctx)) {
var fragmentSelect = getSelectionSet(fragmentNode);
forEachFieldNode(typename, entityKey, fragmentSelect, ctx, cb);
}
} else if ("__typename" !== getName(node)) {
cb(node);
}
});
};
var query = function(store, request) {

@@ -467,181 +650,19 @@ initStoreState(0);

function _extends() {
return (_extends = Object.assign || function(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
}).apply(this, arguments);
}
var write = function(store, request, data) {
initStoreState(0);
var operation = getMainOperation(request.query);
var result = {
dependencies: getCurrentDependencies()
var addCacheOutcome = function(outcome) {
return function(res) {
return {
data: res.data,
error: res.error,
extensions: res.extensions,
operation: _extends({}, res.operation, {
context: _extends({}, res.operation.context, {
meta: _extends({}, res.operation.context.meta, {
cacheOutcome: outcome
})
})
})
};
};
var ctx = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result: result,
store: store
};
var select = getSelectionSet(operation);
var operationName = getOperationName(operation);
if ("Query" === operationName) {
writeSelection(ctx, operationName, select, data);
} else {
writeRoot(ctx, operationName, select, data);
}
clearStoreState();
return result;
};
var writeOptimistic = function(store, request, optimisticKey) {
initStoreState(optimisticKey);
var operation = getMainOperation(request.query);
var result = {
dependencies: getCurrentDependencies()
};
var ctx = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result: result,
store: store
};
var operationName = getOperationName(operation);
if ("Mutation" === operationName) {
var select = getSelectionSet(operation);
forEachFieldNode(operationName, operationName, select, ctx, function _ref(node) {
if (void 0 !== node.selectionSet) {
var fieldName = getName(node);
var resolver = ctx.store.optimisticMutations[fieldName];
if (void 0 !== resolver) {
var fieldArgs = getFieldArguments(node, ctx.variables);
var fieldSelect = getSelectionSet(node);
var resolverValue = resolver(fieldArgs || {}, ctx.store, ctx);
if (!isScalar(resolverValue)) {
writeRootField(ctx, resolverValue, fieldSelect);
}
}
}
});
}
clearStoreState();
return result;
};
var writeFragment = function(store, query, data) {
var fragments = getFragments(query);
var fragment = fragments[Object.keys(fragments)[0]];
if ("production" !== process.env.NODE_ENV && void 0 === fragment) {
throw new Error("At least one fragment must be passed to writeFragment.");
}
var select = getSelectionSet(fragment);
var writeData = _extends({}, data, {
__typename: fragment.typeCondition.name.value
});
var entityKey = store.keyOfEntity(writeData);
if ("production" !== process.env.NODE_ENV && !entityKey) {
throw new Error('You have to pass an "id" or "_id" with your writeFragment data.');
}
var ctx = {
variables: {},
fragments: fragments,
result: {
dependencies: getCurrentDependencies()
},
store: store
};
writeSelection(ctx, entityKey, select, writeData);
};
var writeSelection = function(ctx, entityKey, select, data) {
var isQuery = "Query" === entityKey;
if (!isQuery) {
addDependency(entityKey);
}
var store = ctx.store;
var variables = ctx.variables;
var typename = data.__typename;
store.writeField(typename, entityKey, "__typename");
forEachFieldNode(typename, entityKey, select, ctx, function(node) {
var fieldName = getName(node);
var fieldArgs = getFieldArguments(node, variables);
var fieldKey = joinKeys(entityKey, keyOfField(fieldName, fieldArgs));
var fieldValue = data[getFieldAlias(node)];
if (isQuery) {
addDependency(fieldKey);
}
if (void 0 === node.selectionSet) {
store.writeRecord(fieldValue, fieldKey);
} else if (!isScalar(fieldValue)) {
var link = writeField(ctx, fieldKey, node.selectionSet.selections, fieldValue);
store.writeLink(link, fieldKey);
store.removeRecord(fieldKey);
} else {
store.writeRecord(fieldValue, fieldKey);
}
});
};
var writeField = function(ctx, parentFieldKey, select, data) {
if (Array.isArray(data)) {
return data.map(function _ref2(item, index) {
var indexKey = joinKeys(parentFieldKey, "" + index);
return writeField(ctx, indexKey, select, item);
});
} else if (null === data) {
return null;
}
var entityKey = ctx.store.keyOfEntity(data);
var key = null !== entityKey ? entityKey : parentFieldKey;
writeSelection(ctx, key, select, data);
return key;
};
var writeRoot = function(ctx, typename, select, data) {
forEachFieldNode(typename, typename, select, ctx, function(node) {
var fieldName = getName(node);
var fieldAlias = getFieldAlias(node);
var fieldArgs = getFieldArguments(node, ctx.variables);
var fieldValue = data[fieldAlias];
if (void 0 !== node.selectionSet && null !== fieldValue && !isScalar(fieldValue)) {
writeRootField(ctx, fieldValue, node.selectionSet.selections);
}
if ("Mutation" === typename) {
var updater = ctx.store.updates[fieldName];
if (void 0 !== updater) {
updater(data, fieldArgs || {}, ctx.store, ctx);
}
}
});
};
var writeRootField = function(ctx, data, select) {
if (Array.isArray(data)) {
return data.map(function _ref3(item) {
return writeRootField(ctx, item, select);
});
} else if (null === data) {
return;
}
var entityKey = ctx.store.keyOfEntity(data);
if (null !== entityKey) {
writeSelection(ctx, entityKey, select, data);
}
};
var isScalar = function(x) {
if (Array.isArray(x)) {
return x.some(isScalar);
}
return "object" != typeof x || null !== x && "string" != typeof x.__typename;
};
var addTypeNames = function(op) {

@@ -698,3 +719,6 @@ return _extends({}, op, {

var client = ref.client;
var store = new Store(opts.resolvers, opts.updates, opts.optimistic);
if (!opts) {
opts = {};
}
var store = new Store(opts.resolvers, opts.updates, opts.optimistic, opts.keys);
var optimisticKeys = new Set();

@@ -787,4 +811,4 @@ var ops = new Map();

var cacheOps$ = pipe(cache$, filter(_ref3), map(_ref4));
var cacheResult$ = pipe(cache$, filter(_ref5), tap(_ref6));
var result$ = pipe(forward(merge([ pipe(sharedOps$, filter(_ref7)), cacheOps$ ])), tap(updateCacheWithResult));
var cacheResult$ = pipe(cache$, filter(_ref5), tap(_ref6), map(addCacheOutcome("hit")));
var result$ = pipe(forward(merge([ pipe(sharedOps$, filter(_ref7)), cacheOps$ ])), tap(updateCacheWithResult), map(addCacheOutcome("miss")));
return merge([ result$, cacheResult$ ]);

@@ -791,0 +815,0 @@ };

@@ -1,2 +0,2 @@

import{Kind as e}from"graphql";import{make as t,clearOptimistic as r,get as n,setOptimistic as i,remove as o}from"pessimism";import u from"fast-json-stable-stringify";import{formatDocument as a}from"urql";import{pipe as s,map as c,tap as l,share as f,filter as v,merge as d}from"wonka";var p=function(e){return e.name.value},y=function(e){switch(e.operation){case"query":return"Query";case"mutation":return"Mutation";case"subscription":return"Subscription"}},m=function(e){return void 0!==e.alias?e.alias.value:p(e)},h=function(e){return void 0!==e.selectionSet?e.selectionSet.selections:[]},k=function(t){return t.kind===e.FIELD},b=function(t,r){switch(t.kind){case e.NULL:return null;case e.INT:return parseInt(t.value,10);case e.FLOAT:return parseFloat(t.value);case e.LIST:return t.values.map(function(e){return b(e,r)});case e.OBJECT:return t.fields.reduce(function(e,t){return e[p(t)]=b(t.value,r),e},Object.create(null));case e.VARIABLE:var n=r[p(t)];return void 0!==n?n:null;default:return t.value}},g=function(e,t){return void 0===e.arguments||0===e.arguments.length?null:e.arguments.reduce(function(e,r){return e[p(r)]=b(r.value,t),e},Object.create(null))},O=function(e,t){if(void 0===e.variableDefinitions)return{};var r=t||{};return e.variableDefinitions.reduce(function(e,t){var n=p(t.variable),i=r[n];if(void 0===i){if(void 0===t.defaultValue)return e;i=b(t.defaultValue,r)}return e[n]=i,e},Object.create(null))},w=function(t){return t.kind===e.FRAGMENT_DEFINITION};function E(t){return t.kind===e.OPERATION_DEFINITION}var L=function(e){return e.definitions.find(E)};function _(e,t){return e[p(t)]=t,e}var F=function(e){return e.definitions.filter(w).reduce(_,{})},q=function(e){var t=e.__typename,r=void 0===e.id?e._id:e.id;return null==t?null:function(e){return"Query"===e||"Mutation"===e||"Subscription"===e}(t)?t:null==r?null:t+":"+r},A=function(e,t){return t?e+"("+u(t)+")":e},j=function(e,t){return e+"."+t},S={current:null},T={current:null},I=function(e){if(null===e.current)throw new Error("The cache may only be mutated during operations or as part of its config.");return e.current},M=function(e){S.current=new Set,T.current=e},R=function(){S.current=null,T.current=null},N=function(){return I(S)},P=function(e){I(S).add(e)},x=function(e,t,r){var n=I(T);return i(e,t,r,n)},Q=function(e,t){var r=I(T);return r?i(e,t,void 0,r):o(e,t)},U=function(e,r,n,i){this.records=t(),this.links=t(),this.resolvers=e||{},this.updates=r||{},this.optimisticMutations=n||{},this.keys=i||{}};U.prototype.keyOfEntity=function(e){var t=e.__typename;return void 0!==t&&void 0!==this.keys[t]?this.keys[t](e):q(e)},U.prototype.clearOptimistic=function(e){this.records=r(this.records,e),this.links=r(this.links,e)},U.prototype.getRecord=function(e){return n(this.records,e)},U.prototype.removeRecord=function(e){return this.records=Q(this.records,e)},U.prototype.writeRecord=function(e,t){return this.records=x(this.records,t,e)},U.prototype.getField=function(e,t,r){var n=j(e,A(t,r));return this.getRecord(n)},U.prototype.writeField=function(e,t,r,n){var i=j(t,A(r,n));return this.records=x(this.records,i,e)},U.prototype.getLink=function(e){return n(this.links,e)},U.prototype.removeLink=function(e){return this.links=Q(this.links,e)},U.prototype.writeLink=function(e,t){return this.links=x(this.links,t,e)},U.prototype.resolveValueOrLink=function(e){var t=this.getRecord(e);return void 0!==t?t:this.getLink(e)||null},U.prototype.resolve=function(e,t,r){if("string"==typeof e)return P(e),this.resolveValueOrLink(j(e,A(t,r)));var n=this.keyOfEntity(e);return null===n?null:(P(n),this.resolveValueOrLink(j(n,A(t,r))))},U.prototype.hasField=function(e){return void 0!==this.getRecord(e)||void 0!==this.getLink(e)},U.prototype.updateQuery=function(e,t){var r=D(this,{query:e});J(this,{query:e},t(r.data))},U.prototype.writeFragment=function(e,t){H(this,e,t)};var V=function(t,r,n,i,o){n.forEach(function(n){if(function(e,t){return void 0===e.directives||!e.directives.some(function(e){var r=p(e),n="include"===r;if(!n&&"skip"!==r)return!1;var i=e.arguments?e.arguments[0]:null;if(!i||"if"!==p(i))return!1;var o=b(i.value,t);return!("boolean"!=typeof o&&null!==o||(n?o:!o))})}(n,i.variables))if(k(n))"__typename"!==p(n)&&o(n);else{var u=function(t){return t.kind===e.INLINE_FRAGMENT}(n)?n:i.fragments[p(n)];if(void 0!==u&&function(e,t,r,n){return t===(void 0!==(i=e.typeCondition)?p(i):null)||!h(e).some(function(e){if(!k(e))return!1;var t=p(e),i=g(e,n.variables),o=A(t,i);return!n.store.hasField(j(r,o))});var i}(u,t,r,i)){var a=h(u);V(t,r,a,i,o)}}})},D=function(e,t){M(0);var r=L(t.query),n=Object.create(null),i={completeness:"FULL",dependencies:N(),data:n},o={variables:O(r,t.variables),fragments:F(t.query),result:i,store:e};return i.data=Y(o,"Query",h(r),n),R(),i},Y=function(e,t,r,n){var i="Query"===t;i||P(t);var o=e.store,u=e.variables,a=o.getField(t,"__typename");return"string"!=typeof a?(e.result.completeness="EMPTY",null):(n.__typename=a,V(a,t,r,e,function(r){var s=p(r),c=g(r,u),l=m(r),f=j(t,A(s,c)),v=o.getRecord(f);i&&P(f);var d=o.resolvers[a];if(void 0!==d&&d.hasOwnProperty(s)){var y=d[s](n,c||{},o,e);if(void 0===r.selectionSet)n[l]=void 0!==y?y:null;else{var k=h(r);n[l]=C(e,y,f,k,n[l])}}else if(void 0===r.selectionSet)void 0===v?(e.result.completeness="EMPTY",n[l]=null):n[l]=v;else{var b=h(r),O=o.getLink(f);void 0===O?"object"==typeof v&&null!==v?n[l]=v:(e.result.completeness="EMPTY",n[l]=null):n[l]=B(e,O,b,n[l])}}),n)},C=function(e,t,r,n,i){if(Array.isArray(t))return t.map(function(t,o){var u=void 0!==i?i[o]:void 0,a=j(r,""+o);return C(e,t,a,n,u)});if(null===t)return null;if(G(t)){var o=void 0===i?Object.create(null):i,u=("string"==typeof t?t:e.store.keyOfEntity(t))||r,a=Y(e,u,n,o);if(null!==a&&"object"==typeof t)for(r in t)"__typename"!==r&&t.hasOwnProperty(r)&&(a[r]=t[r]);return a}return e.result.completeness="EMPTY",null},B=function(e,t,r,n){if(Array.isArray(t))return t.map(function(t,i){return B(e,t,r,void 0!==n?n[i]:void 0)});if(null===t)return null;var i=void 0===n?Object.create(null):n;return Y(e,t,r,i)},G=function(e){return"string"==typeof e||"object"==typeof e&&null!==e&&"string"==typeof e.__typename};function z(){return(z=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}var J=function(e,t,r){M(0);var n=L(t.query),i={dependencies:N()},o={variables:O(n,t.variables),fragments:F(t.query),result:i,store:e},u=h(n),a=y(n);return"Query"===a?W(o,a,u,r):Z(o,a,u,r),R(),i},K=function(e,t,r){M(r);var n=L(t.query),i={dependencies:N()},o={variables:O(n,t.variables),fragments:F(t.query),result:i,store:e},u=y(n);if("Mutation"===u){var a=h(n);V(u,u,a,o,function(e){if(void 0!==e.selectionSet){var t=p(e),r=o.store.optimisticMutations[t];if(void 0!==r){var n=g(e,o.variables),i=h(e),u=r(n||{},o.store,o);ee(u)||$(o,u,i)}}})}return R(),i},H=function(e,t,r){var n=F(t),i=n[Object.keys(n)[0]],o=h(i),u=z({},r,{__typename:i.typeCondition.name.value}),a=e.keyOfEntity(u),s={variables:{},fragments:n,result:{dependencies:N()},store:e};W(s,a,o,u)},W=function(e,t,r,n){var i="Query"===t;i||P(t);var o=e.store,u=e.variables,a=n.__typename;o.writeField(a,t,"__typename"),V(a,t,r,e,function(r){var a=p(r),s=g(r,u),c=j(t,A(a,s)),l=n[m(r)];if(i&&P(c),void 0===r.selectionSet)o.writeRecord(l,c);else if(ee(l))o.writeRecord(l,c);else{var f=X(e,c,r.selectionSet.selections,l);o.writeLink(f,c),o.removeRecord(c)}})},X=function(e,t,r,n){if(Array.isArray(n))return n.map(function(n,i){var o=j(t,""+i);return X(e,o,r,n)});if(null===n)return null;var i=e.store.keyOfEntity(n),o=null!==i?i:t;return W(e,o,r,n),o},Z=function(e,t,r,n){V(t,t,r,e,function(r){var i=p(r),o=m(r),u=g(r,e.variables),a=n[o];if(void 0===r.selectionSet||null===a||ee(a)||$(e,a,r.selectionSet.selections),"Mutation"===t){var s=e.store.updates[i];void 0!==s&&s(n,u||{},e.store,e)}})},$=function(e,t,r){if(Array.isArray(t))return t.map(function(t){return $(e,t,r)});if(null!==t){var n=e.store.keyOfEntity(t);null!==n&&W(e,n,r,t)}},ee=function(e){return Array.isArray(e)?e.some(ee):"object"!=typeof e||null!==e&&"string"!=typeof e.__typename},te=function(e){return z({},e,{query:a(e.query)})},re=function(e){return e.context.requestPolicy},ne=function(e){return"query"===e.operationName},ie=function(e){var t=re(e);return ne(e)&&"network-only"!==t},oe=function(e,t){return z({},e,{context:z({},e.context,{requestPolicy:t})})};function ue(e){return ie(e)}function ae(e){return"FULL"!==e.completeness}function se(e){return e.operation}function ce(e){return"FULL"===e.completeness}function le(e){return!ie(e)}var fe=function(e){return function(t){var r=t.forward,n=t.client,i=new U(e.resolvers,e.updates,e.optimistic),o=new Set,u=new Map,a=Object.create(null),p=function(e,t){var r=new Set;function i(e){return r.add(e)}t.forEach(function(e){var t=a[e];void 0!==t&&(a[e]=[],t.forEach(i))}),r.forEach(function(t){if(t!==e.key){var r=u.get(t);u.delete(t),n.reexecuteOperation(r)}})},y=function(e){if(u=re(n=e),function(e){return"mutation"===e.operationName}(n)&&"network-only"!==u){var t=e.key,r=K(i,e,t).dependencies;0!==r.size&&(o.add(t),p(e,r))}var n,u},m=function(e,t){t.forEach(function(t){(a[t]||(a[t]=[])).push(e.key),u.has(e.key)||u.set(e.key,"network-only"===e.context.requestPolicy?oe(e,"cache-and-network"):e)})},h=function(e){var t=re(e),r=D(i,e),n="cache-only"===t||"FULL"===r.completeness;return n&&m(e,r.dependencies),{operation:e,completeness:n?"FULL":"EMPTY",data:r.data}},k=function(e){var t,r=e.data,n=e.operation;null!=r&&(t=J(i,n,r).dependencies);var u=n.key;o.has(u)&&(o.delete(u),i.clearOptimistic(u)),void 0!==t&&(p(n,t),ne(n)&&m(n,t))};function b(e){var t=e.operation;if("cache-and-network"===re(t)){var r=oe(t,"network-only");n.reexecuteOperation(r)}}return function(e){var t=s(e,c(te),l(y),f),n=s(t,v(ue),c(h),f),i=s(n,v(ae),c(se)),o=s(n,v(ce),l(b)),u=s(r(d([s(t,v(le)),i])),l(k));return d([u,o])}}};export{U as Store,fe as cacheExchange,q as defaultKeyOfEntity,D as query,J as write,H as writeFragment,K as writeOptimistic};
import{Kind as e}from"graphql";import{make as t,clearOptimistic as n,get as r,setOptimistic as i,remove as o}from"pessimism";import u from"fast-json-stable-stringify";import{formatDocument as a}from"urql";import{pipe as s,map as c,tap as l,share as f,filter as v,merge as d}from"wonka";var p=function(e){return e.name.value},y=function(e){switch(e.operation){case"query":return"Query";case"mutation":return"Mutation";case"subscription":return"Subscription"}},m=function(e){return void 0!==e.alias?e.alias.value:p(e)},h=function(e){return void 0!==e.selectionSet?e.selectionSet.selections:[]},k=function(t){return t.kind===e.FIELD},b=function(t,n){switch(t.kind){case e.NULL:return null;case e.INT:return parseInt(t.value,10);case e.FLOAT:return parseFloat(t.value);case e.LIST:return t.values.map(function(e){return b(e,n)});case e.OBJECT:return t.fields.reduce(function(e,t){return e[p(t)]=b(t.value,n),e},Object.create(null));case e.VARIABLE:var r=n[p(t)];return void 0!==r?r:null;default:return t.value}},g=function(e,t){return void 0===e.arguments||0===e.arguments.length?null:e.arguments.reduce(function(e,n){return e[p(n)]=b(n.value,t),e},Object.create(null))},O=function(e,t){if(void 0===e.variableDefinitions)return{};var n=t||{};return e.variableDefinitions.reduce(function(e,t){var r=p(t.variable),i=n[r];if(void 0===i){if(void 0===t.defaultValue)return e;i=b(t.defaultValue,n)}return e[r]=i,e},Object.create(null))},w=function(t){return t.kind===e.FRAGMENT_DEFINITION};function _(t){return t.kind===e.OPERATION_DEFINITION}var E=function(e){return e.definitions.find(_)};function L(e,t){return e[p(t)]=t,e}var F=function(e){return e.definitions.filter(w).reduce(L,{})},q=function(e){var t=e.__typename,n=void 0===e.id?e._id:e.id;return null==t?null:function(e){return"Query"===e||"Mutation"===e||"Subscription"===e}(t)?t:null==n?null:t+":"+n},A=function(e,t){return t?e+"("+u(t)+")":e},j=function(e,t){return e+"."+t};function S(){return(S=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var T=function(t,n,r,i,o){r.forEach(function(r){if(function(e,t){return void 0===e.directives||!e.directives.some(function(e){var n=p(e),r="include"===n;if(!r&&"skip"!==n)return!1;var i=e.arguments?e.arguments[0]:null;if(!i||"if"!==p(i))return!1;var o=b(i.value,t);return!("boolean"!=typeof o&&null!==o||(r?o:!o))})}(r,i.variables))if(k(r))"__typename"!==p(r)&&o(r);else{var u=function(t){return t.kind===e.INLINE_FRAGMENT}(r)?r:i.fragments[p(r)];if(void 0!==u&&function(e,t,n,r){return!(!t||t!==(void 0!==(i=e.typeCondition)?p(i):null)&&h(e).some(function(e){if(!k(e))return!1;var t=p(e),i=g(e,r.variables),o=A(t,i);return!r.store.hasField(j(n,o))}));var i}(u,t,n,i)){var a=h(u);T(t,n,a,i,o)}}})},I=function(e,t,n){C(0);var r=E(t.query),i={dependencies:G()},o={variables:O(r,t.variables),fragments:F(t.query),result:i,store:e},u=h(r),a=y(r);return"Query"===a?N(o,a,u,n):P(o,a,u,n),B(),i},M=function(e,t,n){C(n);var r=E(t.query),i={dependencies:G()},o={variables:O(r,t.variables),fragments:F(t.query),result:i,store:e},u=y(r);if("Mutation"===u){var a=h(r);T(u,u,a,o,function(e){if(void 0!==e.selectionSet){var t=p(e),n=o.store.optimisticMutations[t];if(void 0!==n){var r=g(e,o.variables),i=h(e),u=n(r||{},o.store,o);U(u)||Q(o,u,i)}}})}return B(),i},R=function(e,t,n){var r=F(t),i=r[Object.keys(r)[0]],o=h(i),u=S({},n,{__typename:i.typeCondition.name.value}),a=e.keyOfEntity(u),s={variables:{},fragments:r,result:{dependencies:G()},store:e};N(s,a,o,u)},N=function(e,t,n,r){var i=e.store,o=e.variables,u="Query"===t,a=r.__typename;u||z(t),i.writeField(u?t:a,t,"__typename"),T(a,t,n,e,function(n){var a=p(n),s=g(n,o),c=j(t,A(a,s)),l=r[m(n)];if(u&&z(c),void 0===n.selectionSet)i.writeRecord(l,c);else if(U(l))i.writeRecord(l,c);else{var f=x(e,c,n.selectionSet.selections,l);i.writeLink(f,c),i.removeRecord(c)}})},x=function(e,t,n,r){if(Array.isArray(r))return r.map(function(r,i){var o=j(t,""+i);return x(e,o,n,r)});if(null===r)return null;var i=e.store.keyOfEntity(r),o=null!==i?i:t;return N(e,o,n,r),o},P=function(e,t,n,r){T(t,t,n,e,function(n){var i=p(n),o=m(n),u=g(n,e.variables),a=r[o];if(void 0===n.selectionSet||null===a||U(a)||Q(e,a,n.selectionSet.selections),"Mutation"===t){var s=e.store.updates[i];void 0!==s&&s(r,u||{},e.store,e)}})},Q=function(e,t,n){if(Array.isArray(t))return t.map(function(t){return Q(e,t,n)});if(null!==t){var r=e.store.keyOfEntity(t);null!==r?N(e,r,n,t):P(e,t.__typename,n,t)}},U=function(e){return Array.isArray(e)?e.some(U):"object"!=typeof e||null!==e&&"string"!=typeof e.__typename},V={current:null},D={current:null},Y=function(e){if(null===e.current)throw new Error("The cache may only be mutated during operations or as part of its config.");return e.current},C=function(e){V.current=new Set,D.current=e},B=function(){V.current=null,D.current=null},G=function(){return Y(V)},z=function(e){Y(V).add(e)},J=function(e,t,n){var r=Y(D);return i(e,t,n,r)},K=function(e,t){var n=Y(D);return n?i(e,t,void 0,n):o(e,t)},H=function(e,n,r,i){this.records=t(),this.links=t(),this.resolvers=e||{},this.updates=n||{},this.optimisticMutations=r||{},this.keys=i||{}};H.prototype.keyOfEntity=function(e){var t=e.__typename;return void 0!==t&&void 0!==this.keys[t]?this.keys[t](e):q(e)},H.prototype.clearOptimistic=function(e){this.records=n(this.records,e),this.links=n(this.links,e)},H.prototype.getRecord=function(e){return r(this.records,e)},H.prototype.removeRecord=function(e){return this.records=K(this.records,e)},H.prototype.writeRecord=function(e,t){return this.records=J(this.records,t,e)},H.prototype.getField=function(e,t,n){var r=j(e,A(t,n));return this.getRecord(r)},H.prototype.writeField=function(e,t,n,r){var i=j(t,A(n,r));return this.records=J(this.records,i,e)},H.prototype.getLink=function(e){return r(this.links,e)},H.prototype.removeLink=function(e){return this.links=K(this.links,e)},H.prototype.writeLink=function(e,t){return this.links=J(this.links,t,e)},H.prototype.resolveValueOrLink=function(e){var t=this.getRecord(e);return void 0!==t?t:this.getLink(e)||null},H.prototype.resolve=function(e,t,n){if("string"==typeof e)return z(e),this.resolveValueOrLink(j(e,A(t,n)));var r=this.keyOfEntity(e);return null===r?null:(z(r),this.resolveValueOrLink(j(r,A(t,n))))},H.prototype.hasField=function(e){return void 0!==this.getRecord(e)||void 0!==this.getLink(e)},H.prototype.updateQuery=function(e,t){var n=W(this,{query:e});I(this,{query:e},t(n.data))},H.prototype.writeFragment=function(e,t){R(this,e,t)};var W=function(e,t){C(0);var n=E(t.query),r=Object.create(null),i={completeness:"FULL",dependencies:G(),data:r},o={variables:O(n,t.variables),fragments:F(t.query),result:i,store:e};return i.data=X(o,"Query",h(n),r),B(),i},X=function(e,t,n,r){var i="Query"===t;i||z(t);var o=e.store,u=e.variables,a=o.getField(t,"__typename");return"string"!=typeof a?(e.result.completeness="EMPTY",null):(r.__typename=a,T(a,t,n,e,function(n){var s=p(n),c=g(n,u),l=m(n),f=j(t,A(s,c)),v=o.getRecord(f);i&&z(f);var d=o.resolvers[a];if(void 0!==d&&d.hasOwnProperty(s)){var y=d[s](r,c||{},o,e);if(void 0===n.selectionSet)r[l]=void 0!==y?y:null;else{var k=h(n);r[l]=Z(e,y,f,k,r[l])}}else if(void 0===n.selectionSet)void 0===v?(e.result.completeness="EMPTY",r[l]=null):r[l]=v;else{var b=h(n),O=o.getLink(f);void 0===O?"object"==typeof v&&null!==v?r[l]=v:(e.result.completeness="EMPTY",r[l]=null):r[l]=$(e,O,b,r[l])}}),r)},Z=function(e,t,n,r,i){if(Array.isArray(t))return t.map(function(t,o){var u=void 0!==i?i[o]:void 0,a=j(n,""+o);return Z(e,t,a,r,u)});if(null===t)return null;if(ee(t)){var o=void 0===i?Object.create(null):i,u=("string"==typeof t?t:e.store.keyOfEntity(t))||n,a=X(e,u,r,o);if(null!==a&&"object"==typeof t)for(n in t)"__typename"!==n&&t.hasOwnProperty(n)&&(a[n]=t[n]);return a}return e.result.completeness="EMPTY",null},$=function(e,t,n,r){if(Array.isArray(t))return t.map(function(t,i){return $(e,t,n,void 0!==r?r[i]:void 0)});if(null===t)return null;var i=void 0===r?Object.create(null):r;return X(e,t,n,i)},ee=function(e){return"string"==typeof e||"object"==typeof e&&null!==e&&"string"==typeof e.__typename},te=function(e){return function(t){return{data:t.data,error:t.error,extensions:t.extensions,operation:S({},t.operation,{context:S({},t.operation.context,{meta:S({},t.operation.context.meta,{cacheOutcome:e})})})}}},ne=function(e){return S({},e,{query:a(e.query)})},re=function(e){return e.context.requestPolicy},ie=function(e){return"query"===e.operationName},oe=function(e){var t=re(e);return ie(e)&&"network-only"!==t},ue=function(e,t){return S({},e,{context:S({},e.context,{requestPolicy:t})})};function ae(e){return oe(e)}function se(e){return"FULL"!==e.completeness}function ce(e){return e.operation}function le(e){return"FULL"===e.completeness}function fe(e){return!oe(e)}var ve=function(e){return function(t){var n=t.forward,r=t.client;e||(e={});var i=new H(e.resolvers,e.updates,e.optimistic,e.keys),o=new Set,u=new Map,a=Object.create(null),p=function(e,t){var n=new Set;function i(e){return n.add(e)}t.forEach(function(e){var t=a[e];void 0!==t&&(a[e]=[],t.forEach(i))}),n.forEach(function(t){if(t!==e.key){var n=u.get(t);u.delete(t),r.reexecuteOperation(n)}})},y=function(e){if(u=re(r=e),function(e){return"mutation"===e.operationName}(r)&&"network-only"!==u){var t=e.key,n=M(i,e,t).dependencies;0!==n.size&&(o.add(t),p(e,n))}var r,u},m=function(e,t){t.forEach(function(t){(a[t]||(a[t]=[])).push(e.key),u.has(e.key)||u.set(e.key,"network-only"===e.context.requestPolicy?ue(e,"cache-and-network"):e)})},h=function(e){var t=re(e),n=W(i,e),r="cache-only"===t||"FULL"===n.completeness;return r&&m(e,n.dependencies),{operation:e,completeness:r?"FULL":"EMPTY",data:n.data}},k=function(e){var t,n=e.data,r=e.operation;null!=n&&(t=I(i,r,n).dependencies);var u=r.key;o.has(u)&&(o.delete(u),i.clearOptimistic(u)),void 0!==t&&(p(r,t),ie(r)&&m(r,t))};function b(e){var t=e.operation;if("cache-and-network"===re(t)){var n=ue(t,"network-only");r.reexecuteOperation(n)}}return function(e){var t=s(e,c(ne),l(y),f),r=s(t,v(ae),c(h),f),i=s(r,v(se),c(ce)),o=s(r,v(le),l(b),c(te("hit"))),u=s(n(d([s(t,v(fe)),i])),l(k),c(te("miss")));return d([u,o])}}};export{H as Store,ve as cacheExchange,q as defaultKeyOfEntity,W as query,I as write,R as writeFragment,M as writeOptimistic};
//# sourceMappingURL=urql-exchange-graphcache.es.min.js.map

@@ -153,2 +153,239 @@ "use strict";

function _extends() {
return (_extends = Object.assign || function(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
}).apply(this, arguments);
}
var isFragmentMatching = function(node, typename, entityKey, ctx) {
if (!typename) {
return !1;
} else if (typename === (void 0 !== (typeCondition = node.typeCondition) ? getName(typeCondition) : null)) {
return !0;
}
var typeCondition;
return !getSelectionSet(node).some(function(node) {
if (!isFieldNode(node)) {
return !1;
}
var fieldName = getName(node);
var fieldArgs = getFieldArguments(node, ctx.variables);
var fieldKey = keyOfField(fieldName, fieldArgs);
return !ctx.store.hasField(joinKeys(entityKey, fieldKey));
});
};
var forEachFieldNode = function(typename, entityKey, select, ctx, cb) {
select.forEach(function(node) {
if (!function(node, vars) {
if (void 0 === node.directives) {
return !0;
}
return !node.directives.some(function(directive) {
var name = getName(directive);
var isInclude = "include" === name;
if (!isInclude && "skip" !== name) {
return !1;
}
var arg = directive.arguments ? directive.arguments[0] : null;
if (!arg || "if" !== getName(arg)) {
return !1;
}
var value = evaluateValueNode(arg.value, vars);
if ("boolean" != typeof value && null !== value) {
return !1;
}
return isInclude ? !value : !!value;
});
}(node, ctx.variables)) {
return;
} else if (!isFieldNode(node)) {
var fragmentNode = function(node) {
return node.kind === graphql.Kind.INLINE_FRAGMENT;
}(node) ? node : ctx.fragments[getName(node)];
if (void 0 !== fragmentNode && isFragmentMatching(fragmentNode, typename, entityKey, ctx)) {
var fragmentSelect = getSelectionSet(fragmentNode);
forEachFieldNode(typename, entityKey, fragmentSelect, ctx, cb);
}
} else if ("__typename" !== getName(node)) {
cb(node);
}
});
};
var write = function(store, request, data) {
initStoreState(0);
var operation = getMainOperation(request.query);
var result = {
dependencies: getCurrentDependencies()
};
var ctx = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result: result,
store: store
};
var select = getSelectionSet(operation);
var operationName = getOperationName(operation);
if ("Query" === operationName) {
writeSelection(ctx, operationName, select, data);
} else {
writeRoot(ctx, operationName, select, data);
}
clearStoreState();
return result;
};
var writeOptimistic = function(store, request, optimisticKey) {
initStoreState(optimisticKey);
var operation = getMainOperation(request.query);
var result = {
dependencies: getCurrentDependencies()
};
var ctx = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result: result,
store: store
};
var operationName = getOperationName(operation);
if ("Mutation" === operationName) {
var select = getSelectionSet(operation);
forEachFieldNode(operationName, operationName, select, ctx, function _ref(node) {
if (void 0 !== node.selectionSet) {
var fieldName = getName(node);
var resolver = ctx.store.optimisticMutations[fieldName];
if (void 0 !== resolver) {
var fieldArgs = getFieldArguments(node, ctx.variables);
var fieldSelect = getSelectionSet(node);
var resolverValue = resolver(fieldArgs || {}, ctx.store, ctx);
if (!isScalar(resolverValue)) {
writeRootField(ctx, resolverValue, fieldSelect);
}
}
}
});
}
clearStoreState();
return result;
};
var writeFragment = function(store, query, data) {
var fragments = getFragments(query);
var fragment = fragments[Object.keys(fragments)[0]];
if ("production" !== process.env.NODE_ENV && void 0 === fragment) {
throw new Error("At least one fragment must be passed to writeFragment.");
}
var select = getSelectionSet(fragment);
var writeData = _extends({}, data, {
__typename: fragment.typeCondition.name.value
});
var entityKey = store.keyOfEntity(writeData);
if ("production" !== process.env.NODE_ENV && !entityKey) {
throw new Error('You have to pass an "id" or "_id" with your writeFragment data.');
}
var ctx = {
variables: {},
fragments: fragments,
result: {
dependencies: getCurrentDependencies()
},
store: store
};
writeSelection(ctx, entityKey, select, writeData);
};
var writeSelection = function(ctx, entityKey, select, data) {
var store = ctx.store;
var variables = ctx.variables;
var isQuery = "Query" === entityKey;
var typename = data.__typename;
if (!isQuery) {
addDependency(entityKey);
}
store.writeField(isQuery ? entityKey : typename, entityKey, "__typename");
forEachFieldNode(typename, entityKey, select, ctx, function(node) {
var fieldName = getName(node);
var fieldArgs = getFieldArguments(node, variables);
var fieldKey = joinKeys(entityKey, keyOfField(fieldName, fieldArgs));
var fieldValue = data[getFieldAlias(node)];
if (isQuery) {
addDependency(fieldKey);
}
if (void 0 === node.selectionSet) {
store.writeRecord(fieldValue, fieldKey);
} else if (!isScalar(fieldValue)) {
var link = writeField(ctx, fieldKey, node.selectionSet.selections, fieldValue);
store.writeLink(link, fieldKey);
store.removeRecord(fieldKey);
} else {
store.writeRecord(fieldValue, fieldKey);
}
});
};
var writeField = function(ctx, parentFieldKey, select, data) {
if (Array.isArray(data)) {
return data.map(function _ref2(item, index) {
var indexKey = joinKeys(parentFieldKey, "" + index);
return writeField(ctx, indexKey, select, item);
});
} else if (null === data) {
return null;
}
var entityKey = ctx.store.keyOfEntity(data);
var key = null !== entityKey ? entityKey : parentFieldKey;
writeSelection(ctx, key, select, data);
return key;
};
var writeRoot = function(ctx, typename, select, data) {
forEachFieldNode(typename, typename, select, ctx, function(node) {
var fieldName = getName(node);
var fieldAlias = getFieldAlias(node);
var fieldArgs = getFieldArguments(node, ctx.variables);
var fieldValue = data[fieldAlias];
if (void 0 !== node.selectionSet && null !== fieldValue && !isScalar(fieldValue)) {
writeRootField(ctx, fieldValue, node.selectionSet.selections);
}
if ("Mutation" === typename) {
var updater = ctx.store.updates[fieldName];
if (void 0 !== updater) {
updater(data, fieldArgs || {}, ctx.store, ctx);
}
}
});
};
var writeRootField = function(ctx, data, select) {
if (Array.isArray(data)) {
return data.map(function _ref3(item) {
return writeRootField(ctx, item, select);
});
} else if (null === data) {
return;
}
var entityKey = ctx.store.keyOfEntity(data);
if (null !== entityKey) {
writeSelection(ctx, entityKey, select, data);
} else {
writeRoot(ctx, data.__typename, select, data);
}
};
var isScalar = function(x) {
if (Array.isArray(x)) {
return x.some(isScalar);
}
return "object" != typeof x || null !== x && "string" != typeof x.__typename;
};
var currentDependencies = {

@@ -294,56 +531,2 @@ current: null

var isFragmentMatching = function(node, typename, entityKey, ctx) {
if (typename === (void 0 !== (typeCondition = node.typeCondition) ? getName(typeCondition) : null)) {
return !0;
}
var typeCondition;
return !getSelectionSet(node).some(function(node) {
if (!isFieldNode(node)) {
return !1;
}
var fieldName = getName(node);
var fieldArgs = getFieldArguments(node, ctx.variables);
var fieldKey = keyOfField(fieldName, fieldArgs);
return !ctx.store.hasField(joinKeys(entityKey, fieldKey));
});
};
var forEachFieldNode = function(typename, entityKey, select, ctx, cb) {
select.forEach(function(node) {
if (!function(node, vars) {
if (void 0 === node.directives) {
return !0;
}
return !node.directives.some(function(directive) {
var name = getName(directive);
var isInclude = "include" === name;
if (!isInclude && "skip" !== name) {
return !1;
}
var arg = directive.arguments ? directive.arguments[0] : null;
if (!arg || "if" !== getName(arg)) {
return !1;
}
var value = evaluateValueNode(arg.value, vars);
if ("boolean" != typeof value && null !== value) {
return !1;
}
return isInclude ? !value : !!value;
});
}(node, ctx.variables)) {
return;
} else if (!isFieldNode(node)) {
var fragmentNode = function(node) {
return node.kind === graphql.Kind.INLINE_FRAGMENT;
}(node) ? node : ctx.fragments[getName(node)];
if (void 0 !== fragmentNode && isFragmentMatching(fragmentNode, typename, entityKey, ctx)) {
var fragmentSelect = getSelectionSet(fragmentNode);
forEachFieldNode(typename, entityKey, fragmentSelect, ctx, cb);
}
} else if ("__typename" !== getName(node)) {
cb(node);
}
});
};
var query = function(store, request) {

@@ -471,181 +654,19 @@ initStoreState(0);

function _extends() {
return (_extends = Object.assign || function(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
}).apply(this, arguments);
}
var write = function(store, request, data) {
initStoreState(0);
var operation = getMainOperation(request.query);
var result = {
dependencies: getCurrentDependencies()
var addCacheOutcome = function(outcome) {
return function(res) {
return {
data: res.data,
error: res.error,
extensions: res.extensions,
operation: _extends({}, res.operation, {
context: _extends({}, res.operation.context, {
meta: _extends({}, res.operation.context.meta, {
cacheOutcome: outcome
})
})
})
};
};
var ctx = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result: result,
store: store
};
var select = getSelectionSet(operation);
var operationName = getOperationName(operation);
if ("Query" === operationName) {
writeSelection(ctx, operationName, select, data);
} else {
writeRoot(ctx, operationName, select, data);
}
clearStoreState();
return result;
};
var writeOptimistic = function(store, request, optimisticKey) {
initStoreState(optimisticKey);
var operation = getMainOperation(request.query);
var result = {
dependencies: getCurrentDependencies()
};
var ctx = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result: result,
store: store
};
var operationName = getOperationName(operation);
if ("Mutation" === operationName) {
var select = getSelectionSet(operation);
forEachFieldNode(operationName, operationName, select, ctx, function _ref(node) {
if (void 0 !== node.selectionSet) {
var fieldName = getName(node);
var resolver = ctx.store.optimisticMutations[fieldName];
if (void 0 !== resolver) {
var fieldArgs = getFieldArguments(node, ctx.variables);
var fieldSelect = getSelectionSet(node);
var resolverValue = resolver(fieldArgs || {}, ctx.store, ctx);
if (!isScalar(resolverValue)) {
writeRootField(ctx, resolverValue, fieldSelect);
}
}
}
});
}
clearStoreState();
return result;
};
var writeFragment = function(store, query, data) {
var fragments = getFragments(query);
var fragment = fragments[Object.keys(fragments)[0]];
if ("production" !== process.env.NODE_ENV && void 0 === fragment) {
throw new Error("At least one fragment must be passed to writeFragment.");
}
var select = getSelectionSet(fragment);
var writeData = _extends({}, data, {
__typename: fragment.typeCondition.name.value
});
var entityKey = store.keyOfEntity(writeData);
if ("production" !== process.env.NODE_ENV && !entityKey) {
throw new Error('You have to pass an "id" or "_id" with your writeFragment data.');
}
var ctx = {
variables: {},
fragments: fragments,
result: {
dependencies: getCurrentDependencies()
},
store: store
};
writeSelection(ctx, entityKey, select, writeData);
};
var writeSelection = function(ctx, entityKey, select, data) {
var isQuery = "Query" === entityKey;
if (!isQuery) {
addDependency(entityKey);
}
var store = ctx.store;
var variables = ctx.variables;
var typename = data.__typename;
store.writeField(typename, entityKey, "__typename");
forEachFieldNode(typename, entityKey, select, ctx, function(node) {
var fieldName = getName(node);
var fieldArgs = getFieldArguments(node, variables);
var fieldKey = joinKeys(entityKey, keyOfField(fieldName, fieldArgs));
var fieldValue = data[getFieldAlias(node)];
if (isQuery) {
addDependency(fieldKey);
}
if (void 0 === node.selectionSet) {
store.writeRecord(fieldValue, fieldKey);
} else if (!isScalar(fieldValue)) {
var link = writeField(ctx, fieldKey, node.selectionSet.selections, fieldValue);
store.writeLink(link, fieldKey);
store.removeRecord(fieldKey);
} else {
store.writeRecord(fieldValue, fieldKey);
}
});
};
var writeField = function(ctx, parentFieldKey, select, data) {
if (Array.isArray(data)) {
return data.map(function _ref2(item, index) {
var indexKey = joinKeys(parentFieldKey, "" + index);
return writeField(ctx, indexKey, select, item);
});
} else if (null === data) {
return null;
}
var entityKey = ctx.store.keyOfEntity(data);
var key = null !== entityKey ? entityKey : parentFieldKey;
writeSelection(ctx, key, select, data);
return key;
};
var writeRoot = function(ctx, typename, select, data) {
forEachFieldNode(typename, typename, select, ctx, function(node) {
var fieldName = getName(node);
var fieldAlias = getFieldAlias(node);
var fieldArgs = getFieldArguments(node, ctx.variables);
var fieldValue = data[fieldAlias];
if (void 0 !== node.selectionSet && null !== fieldValue && !isScalar(fieldValue)) {
writeRootField(ctx, fieldValue, node.selectionSet.selections);
}
if ("Mutation" === typename) {
var updater = ctx.store.updates[fieldName];
if (void 0 !== updater) {
updater(data, fieldArgs || {}, ctx.store, ctx);
}
}
});
};
var writeRootField = function(ctx, data, select) {
if (Array.isArray(data)) {
return data.map(function _ref3(item) {
return writeRootField(ctx, item, select);
});
} else if (null === data) {
return;
}
var entityKey = ctx.store.keyOfEntity(data);
if (null !== entityKey) {
writeSelection(ctx, entityKey, select, data);
}
};
var isScalar = function(x) {
if (Array.isArray(x)) {
return x.some(isScalar);
}
return "object" != typeof x || null !== x && "string" != typeof x.__typename;
};
var addTypeNames = function(op) {

@@ -704,3 +725,6 @@ return _extends({}, op, {

var client = ref.client;
var store = new Store(opts.resolvers, opts.updates, opts.optimistic);
if (!opts) {
opts = {};
}
var store = new Store(opts.resolvers, opts.updates, opts.optimistic, opts.keys);
var optimisticKeys = new Set();

@@ -793,4 +817,4 @@ var ops = new Map();

var cacheOps$ = wonka.pipe(cache$, wonka.filter(_ref3), wonka.map(_ref4));
var cacheResult$ = wonka.pipe(cache$, wonka.filter(_ref5), wonka.tap(_ref6));
var result$ = wonka.pipe(forward(wonka.merge([ wonka.pipe(sharedOps$, wonka.filter(_ref7)), cacheOps$ ])), wonka.tap(updateCacheWithResult));
var cacheResult$ = wonka.pipe(cache$, wonka.filter(_ref5), wonka.tap(_ref6), wonka.map(addCacheOutcome("hit")));
var result$ = wonka.pipe(forward(wonka.merge([ wonka.pipe(sharedOps$, wonka.filter(_ref7)), cacheOps$ ])), wonka.tap(updateCacheWithResult), wonka.map(addCacheOutcome("miss")));
return wonka.merge([ result$, cacheResult$ ]);

@@ -797,0 +821,0 @@ };

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("graphql"),r=require("pessimism"),n=(e=require("fast-json-stable-stringify"))&&"object"==typeof e&&"default"in e?e.default:e,i=require("urql"),o=require("wonka"),u=function(e){return e.name.value},a=function(e){switch(e.operation){case"query":return"Query";case"mutation":return"Mutation";case"subscription":return"Subscription"}},s=function(e){return void 0!==e.alias?e.alias.value:u(e)},c=function(e){return void 0!==e.selectionSet?e.selectionSet.selections:[]},l=function(e){return e.kind===t.Kind.FIELD},f=function(e,r){switch(e.kind){case t.Kind.NULL:return null;case t.Kind.INT:return parseInt(e.value,10);case t.Kind.FLOAT:return parseFloat(e.value);case t.Kind.LIST:return e.values.map(function(e){return f(e,r)});case t.Kind.OBJECT:return e.fields.reduce(function(e,t){return e[u(t)]=f(t.value,r),e},Object.create(null));case t.Kind.VARIABLE:var n=r[u(e)];return void 0!==n?n:null;default:return e.value}},p=function(e,t){return void 0===e.arguments||0===e.arguments.length?null:e.arguments.reduce(function(e,r){return e[u(r)]=f(r.value,t),e},Object.create(null))},v=function(e,t){if(void 0===e.variableDefinitions)return{};var r=t||{};return e.variableDefinitions.reduce(function(e,t){var n=u(t.variable),i=r[n];if(void 0===i){if(void 0===t.defaultValue)return e;i=f(t.defaultValue,r)}return e[n]=i,e},Object.create(null))},d=function(e){return e.kind===t.Kind.FRAGMENT_DEFINITION};function y(e){return e.kind===t.Kind.OPERATION_DEFINITION}var m=function(e){return e.definitions.find(y)};function h(e,t){return e[u(t)]=t,e}var k=function(e){return e.definitions.filter(d).reduce(h,{})},g=function(e){var t=e.__typename,r=void 0===e.id?e._id:e.id;return null==t?null:function(e){return"Query"===e||"Mutation"===e||"Subscription"===e}(t)?t:null==r?null:t+":"+r},O=function(e,t){return t?e+"("+n(t)+")":e},b=function(e,t){return e+"."+t},w={current:null},E={current:null},_=function(e){if(null===e.current)throw new Error("The cache may only be mutated during operations or as part of its config.");return e.current},L=function(e){w.current=new Set,E.current=e},q=function(){w.current=null,E.current=null},F=function(){return _(w)},j=function(e){_(w).add(e)},A=function(e,t,n){var i=_(E);return r.setOptimistic(e,t,n,i)},S=function(e,t){var n=_(E);return n?r.setOptimistic(e,t,void 0,n):r.remove(e,t)},x=function(e,t,n,i){this.records=r.make(),this.links=r.make(),this.resolvers=e||{},this.updates=t||{},this.optimisticMutations=n||{},this.keys=i||{}};x.prototype.keyOfEntity=function(e){var t=e.__typename;return void 0!==t&&void 0!==this.keys[t]?this.keys[t](e):g(e)},x.prototype.clearOptimistic=function(e){this.records=r.clearOptimistic(this.records,e),this.links=r.clearOptimistic(this.links,e)},x.prototype.getRecord=function(e){return r.get(this.records,e)},x.prototype.removeRecord=function(e){return this.records=S(this.records,e)},x.prototype.writeRecord=function(e,t){return this.records=A(this.records,t,e)},x.prototype.getField=function(e,t,r){var n=b(e,O(t,r));return this.getRecord(n)},x.prototype.writeField=function(e,t,r,n){var i=b(t,O(r,n));return this.records=A(this.records,i,e)},x.prototype.getLink=function(e){return r.get(this.links,e)},x.prototype.removeLink=function(e){return this.links=S(this.links,e)},x.prototype.writeLink=function(e,t){return this.links=A(this.links,t,e)},x.prototype.resolveValueOrLink=function(e){var t=this.getRecord(e);return void 0!==t?t:this.getLink(e)||null},x.prototype.resolve=function(e,t,r){if("string"==typeof e)return j(e),this.resolveValueOrLink(b(e,O(t,r)));var n=this.keyOfEntity(e);return null===n?null:(j(n),this.resolveValueOrLink(b(n,O(t,r))))},x.prototype.hasField=function(e){return void 0!==this.getRecord(e)||void 0!==this.getLink(e)},x.prototype.updateQuery=function(e,t){var r=T(this,{query:e});Q(this,{query:e},t(r.data))},x.prototype.writeFragment=function(e,t){U(this,e,t)};var M=function(e,r,n,i,o){n.forEach(function(n){if(function(e,t){return void 0===e.directives||!e.directives.some(function(e){var r=u(e),n="include"===r;if(!n&&"skip"!==r)return!1;var i=e.arguments?e.arguments[0]:null;if(!i||"if"!==u(i))return!1;var o=f(i.value,t);return!("boolean"!=typeof o&&null!==o||(n?o:!o))})}(n,i.variables))if(l(n))"__typename"!==u(n)&&o(n);else{var a=function(e){return e.kind===t.Kind.INLINE_FRAGMENT}(n)?n:i.fragments[u(n)];if(void 0!==a&&function(e,t,r,n){return t===(void 0!==(i=e.typeCondition)?u(i):null)||!c(e).some(function(e){if(!l(e))return!1;var t=u(e),i=p(e,n.variables),o=O(t,i);return!n.store.hasField(b(r,o))});var i}(a,e,r,i)){var s=c(a);M(e,r,s,i,o)}}})},T=function(e,t){L(0);var r=m(t.query),n=Object.create(null),i={completeness:"FULL",dependencies:F(),data:n},o={variables:v(r,t.variables),fragments:k(t.query),result:i,store:e};return i.data=I(o,"Query",c(r),n),q(),i},I=function(e,t,r,n){var i="Query"===t;i||j(t);var o=e.store,a=e.variables,l=o.getField(t,"__typename");return"string"!=typeof l?(e.result.completeness="EMPTY",null):(n.__typename=l,M(l,t,r,e,function(r){var f=u(r),v=p(r,a),d=s(r),y=b(t,O(f,v)),m=o.getRecord(y);i&&j(y);var h=o.resolvers[l];if(void 0!==h&&h.hasOwnProperty(f)){var k=h[f](n,v||{},o,e);if(void 0===r.selectionSet)n[d]=void 0!==k?k:null;else{var g=c(r);n[d]=R(e,k,y,g,n[d])}}else if(void 0===r.selectionSet)void 0===m?(e.result.completeness="EMPTY",n[d]=null):n[d]=m;else{var w=c(r),E=o.getLink(y);void 0===E?"object"==typeof m&&null!==m?n[d]=m:(e.result.completeness="EMPTY",n[d]=null):n[d]=N(e,E,w,n[d])}}),n)},R=function(e,t,r,n,i){if(Array.isArray(t))return t.map(function(t,o){var u=void 0!==i?i[o]:void 0,a=b(r,""+o);return R(e,t,a,n,u)});if(null===t)return null;if(P(t)){var o=void 0===i?Object.create(null):i,u=("string"==typeof t?t:e.store.keyOfEntity(t))||r,a=I(e,u,n,o);if(null!==a&&"object"==typeof t)for(r in t)"__typename"!==r&&t.hasOwnProperty(r)&&(a[r]=t[r]);return a}return e.result.completeness="EMPTY",null},N=function(e,t,r,n){if(Array.isArray(t))return t.map(function(t,i){return N(e,t,r,void 0!==n?n[i]:void 0)});if(null===t)return null;var i=void 0===n?Object.create(null):n;return I(e,t,r,i)},P=function(e){return"string"==typeof e||"object"==typeof e&&null!==e&&"string"==typeof e.__typename};function K(){return(K=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}var Q=function(e,t,r){L(0);var n=m(t.query),i={dependencies:F()},o={variables:v(n,t.variables),fragments:k(t.query),result:i,store:e},u=c(n),s=a(n);return"Query"===s?V(o,s,u,r):C(o,s,u,r),q(),i},D=function(e,t,r){L(r);var n=m(t.query),i={dependencies:F()},o={variables:v(n,t.variables),fragments:k(t.query),result:i,store:e},s=a(n);if("Mutation"===s){var l=c(n);M(s,s,l,o,function(e){if(void 0!==e.selectionSet){var t=u(e),r=o.store.optimisticMutations[t];if(void 0!==r){var n=p(e,o.variables),i=c(e),a=r(n||{},o.store,o);G(a)||B(o,a,i)}}})}return q(),i},U=function(e,t,r){var n=k(t),i=n[Object.keys(n)[0]],o=c(i),u=K({},r,{__typename:i.typeCondition.name.value}),a=e.keyOfEntity(u),s={variables:{},fragments:n,result:{dependencies:F()},store:e};V(s,a,o,u)},V=function(e,t,r,n){var i="Query"===t;i||j(t);var o=e.store,a=e.variables,c=n.__typename;o.writeField(c,t,"__typename"),M(c,t,r,e,function(r){var c=u(r),l=p(r,a),f=b(t,O(c,l)),v=n[s(r)];if(i&&j(f),void 0===r.selectionSet)o.writeRecord(v,f);else if(G(v))o.writeRecord(v,f);else{var d=Y(e,f,r.selectionSet.selections,v);o.writeLink(d,f),o.removeRecord(f)}})},Y=function(e,t,r,n){if(Array.isArray(n))return n.map(function(n,i){var o=b(t,""+i);return Y(e,o,r,n)});if(null===n)return null;var i=e.store.keyOfEntity(n),o=null!==i?i:t;return V(e,o,r,n),o},C=function(e,t,r,n){M(t,t,r,e,function(r){var i=u(r),o=s(r),a=p(r,e.variables),c=n[o];if(void 0===r.selectionSet||null===c||G(c)||B(e,c,r.selectionSet.selections),"Mutation"===t){var l=e.store.updates[i];void 0!==l&&l(n,a||{},e.store,e)}})},B=function(e,t,r){if(Array.isArray(t))return t.map(function(t){return B(e,t,r)});if(null!==t){var n=e.store.keyOfEntity(t);null!==n&&V(e,n,r,t)}},G=function(e){return Array.isArray(e)?e.some(G):"object"!=typeof e||null!==e&&"string"!=typeof e.__typename},z=function(e){return K({},e,{query:i.formatDocument(e.query)})},J=function(e){return e.context.requestPolicy},H=function(e){return"query"===e.operationName},W=function(e){var t=J(e);return H(e)&&"network-only"!==t},X=function(e,t){return K({},e,{context:K({},e.context,{requestPolicy:t})})};function Z(e){return W(e)}function $(e){return"FULL"!==e.completeness}function ee(e){return e.operation}function te(e){return"FULL"===e.completeness}function re(e){return!W(e)}exports.Store=x,exports.cacheExchange=function(e){return function(t){var r=t.forward,n=t.client,i=new x(e.resolvers,e.updates,e.optimistic),u=new Set,a=new Map,s=Object.create(null),c=function(e,t){var r=new Set;function i(e){return r.add(e)}t.forEach(function(e){var t=s[e];void 0!==t&&(s[e]=[],t.forEach(i))}),r.forEach(function(t){if(t!==e.key){var r=a.get(t);a.delete(t),n.reexecuteOperation(r)}})},l=function(e){if(o=J(n=e),function(e){return"mutation"===e.operationName}(n)&&"network-only"!==o){var t=e.key,r=D(i,e,t).dependencies;0!==r.size&&(u.add(t),c(e,r))}var n,o},f=function(e,t){t.forEach(function(t){(s[t]||(s[t]=[])).push(e.key),a.has(e.key)||a.set(e.key,"network-only"===e.context.requestPolicy?X(e,"cache-and-network"):e)})},p=function(e){var t=J(e),r=T(i,e),n="cache-only"===t||"FULL"===r.completeness;return n&&f(e,r.dependencies),{operation:e,completeness:n?"FULL":"EMPTY",data:r.data}},v=function(e){var t,r=e.data,n=e.operation;null!=r&&(t=Q(i,n,r).dependencies);var o=n.key;u.has(o)&&(u.delete(o),i.clearOptimistic(o)),void 0!==t&&(c(n,t),H(n)&&f(n,t))};function d(e){var t=e.operation;if("cache-and-network"===J(t)){var r=X(t,"network-only");n.reexecuteOperation(r)}}return function(e){var t=o.pipe(e,o.map(z),o.tap(l),o.share),n=o.pipe(t,o.filter(Z),o.map(p),o.share),i=o.pipe(n,o.filter($),o.map(ee)),u=o.pipe(n,o.filter(te),o.tap(d)),a=o.pipe(r(o.merge([o.pipe(t,o.filter(re)),i])),o.tap(v));return o.merge([a,u])}}},exports.defaultKeyOfEntity=g,exports.query=T,exports.write=Q,exports.writeFragment=U,exports.writeOptimistic=D;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("graphql"),r=require("pessimism"),n=(e=require("fast-json-stable-stringify"))&&"object"==typeof e&&"default"in e?e.default:e,i=require("urql"),o=require("wonka"),u=function(e){return e.name.value},a=function(e){switch(e.operation){case"query":return"Query";case"mutation":return"Mutation";case"subscription":return"Subscription"}},s=function(e){return void 0!==e.alias?e.alias.value:u(e)},c=function(e){return void 0!==e.selectionSet?e.selectionSet.selections:[]},l=function(e){return e.kind===t.Kind.FIELD},f=function(e,r){switch(e.kind){case t.Kind.NULL:return null;case t.Kind.INT:return parseInt(e.value,10);case t.Kind.FLOAT:return parseFloat(e.value);case t.Kind.LIST:return e.values.map(function(e){return f(e,r)});case t.Kind.OBJECT:return e.fields.reduce(function(e,t){return e[u(t)]=f(t.value,r),e},Object.create(null));case t.Kind.VARIABLE:var n=r[u(e)];return void 0!==n?n:null;default:return e.value}},p=function(e,t){return void 0===e.arguments||0===e.arguments.length?null:e.arguments.reduce(function(e,r){return e[u(r)]=f(r.value,t),e},Object.create(null))},v=function(e,t){if(void 0===e.variableDefinitions)return{};var r=t||{};return e.variableDefinitions.reduce(function(e,t){var n=u(t.variable),i=r[n];if(void 0===i){if(void 0===t.defaultValue)return e;i=f(t.defaultValue,r)}return e[n]=i,e},Object.create(null))},d=function(e){return e.kind===t.Kind.FRAGMENT_DEFINITION};function y(e){return e.kind===t.Kind.OPERATION_DEFINITION}var m=function(e){return e.definitions.find(y)};function h(e,t){return e[u(t)]=t,e}var k=function(e){return e.definitions.filter(d).reduce(h,{})},g=function(e){var t=e.__typename,r=void 0===e.id?e._id:e.id;return null==t?null:function(e){return"Query"===e||"Mutation"===e||"Subscription"===e}(t)?t:null==r?null:t+":"+r},O=function(e,t){return t?e+"("+n(t)+")":e},b=function(e,t){return e+"."+t};function w(){return(w=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}var _=function(e,r,n,i,o){n.forEach(function(n){if(function(e,t){return void 0===e.directives||!e.directives.some(function(e){var r=u(e),n="include"===r;if(!n&&"skip"!==r)return!1;var i=e.arguments?e.arguments[0]:null;if(!i||"if"!==u(i))return!1;var o=f(i.value,t);return!("boolean"!=typeof o&&null!==o||(n?o:!o))})}(n,i.variables))if(l(n))"__typename"!==u(n)&&o(n);else{var a=function(e){return e.kind===t.Kind.INLINE_FRAGMENT}(n)?n:i.fragments[u(n)];if(void 0!==a&&function(e,t,r,n){return!(!t||t!==(void 0!==(i=e.typeCondition)?u(i):null)&&c(e).some(function(e){if(!l(e))return!1;var t=u(e),i=p(e,n.variables),o=O(t,i);return!n.store.hasField(b(r,o))}));var i}(a,e,r,i)){var s=c(a);_(e,r,s,i,o)}}})},E=function(e,t,r){R(0);var n=m(t.query),i={dependencies:P()},o={variables:v(n,t.variables),fragments:k(t.query),result:i,store:e},u=c(n),s=a(n);return"Query"===s?x(o,s,u,r):j(o,s,u,r),N(),i},L=function(e,t,r){R(r);var n=m(t.query),i={dependencies:P()},o={variables:v(n,t.variables),fragments:k(t.query),result:i,store:e},s=a(n);if("Mutation"===s){var l=c(n);_(s,s,l,o,function(e){if(void 0!==e.selectionSet){var t=u(e),r=o.store.optimisticMutations[t];if(void 0!==r){var n=p(e,o.variables),i=c(e),a=r(n||{},o.store,o);S(a)||A(o,a,i)}}})}return N(),i},q=function(e,t,r){var n=k(t),i=n[Object.keys(n)[0]],o=c(i),u=w({},r,{__typename:i.typeCondition.name.value}),a=e.keyOfEntity(u),s={variables:{},fragments:n,result:{dependencies:P()},store:e};x(s,a,o,u)},x=function(e,t,r,n){var i=e.store,o=e.variables,a="Query"===t,c=n.__typename;a||K(t),i.writeField(a?t:c,t,"__typename"),_(c,t,r,e,function(r){var c=u(r),l=p(r,o),f=b(t,O(c,l)),v=n[s(r)];if(a&&K(f),void 0===r.selectionSet)i.writeRecord(v,f);else if(S(v))i.writeRecord(v,f);else{var d=F(e,f,r.selectionSet.selections,v);i.writeLink(d,f),i.removeRecord(f)}})},F=function(e,t,r,n){if(Array.isArray(n))return n.map(function(n,i){var o=b(t,""+i);return F(e,o,r,n)});if(null===n)return null;var i=e.store.keyOfEntity(n),o=null!==i?i:t;return x(e,o,r,n),o},j=function(e,t,r,n){_(t,t,r,e,function(r){var i=u(r),o=s(r),a=p(r,e.variables),c=n[o];if(void 0===r.selectionSet||null===c||S(c)||A(e,c,r.selectionSet.selections),"Mutation"===t){var l=e.store.updates[i];void 0!==l&&l(n,a||{},e.store,e)}})},A=function(e,t,r){if(Array.isArray(t))return t.map(function(t){return A(e,t,r)});if(null!==t){var n=e.store.keyOfEntity(t);null!==n?x(e,n,r,t):j(e,t.__typename,r,t)}},S=function(e){return Array.isArray(e)?e.some(S):"object"!=typeof e||null!==e&&"string"!=typeof e.__typename},M={current:null},T={current:null},I=function(e){if(null===e.current)throw new Error("The cache may only be mutated during operations or as part of its config.");return e.current},R=function(e){M.current=new Set,T.current=e},N=function(){M.current=null,T.current=null},P=function(){return I(M)},K=function(e){I(M).add(e)},Q=function(e,t,n){var i=I(T);return r.setOptimistic(e,t,n,i)},D=function(e,t){var n=I(T);return n?r.setOptimistic(e,t,void 0,n):r.remove(e,t)},U=function(e,t,n,i){this.records=r.make(),this.links=r.make(),this.resolvers=e||{},this.updates=t||{},this.optimisticMutations=n||{},this.keys=i||{}};U.prototype.keyOfEntity=function(e){var t=e.__typename;return void 0!==t&&void 0!==this.keys[t]?this.keys[t](e):g(e)},U.prototype.clearOptimistic=function(e){this.records=r.clearOptimistic(this.records,e),this.links=r.clearOptimistic(this.links,e)},U.prototype.getRecord=function(e){return r.get(this.records,e)},U.prototype.removeRecord=function(e){return this.records=D(this.records,e)},U.prototype.writeRecord=function(e,t){return this.records=Q(this.records,t,e)},U.prototype.getField=function(e,t,r){var n=b(e,O(t,r));return this.getRecord(n)},U.prototype.writeField=function(e,t,r,n){var i=b(t,O(r,n));return this.records=Q(this.records,i,e)},U.prototype.getLink=function(e){return r.get(this.links,e)},U.prototype.removeLink=function(e){return this.links=D(this.links,e)},U.prototype.writeLink=function(e,t){return this.links=Q(this.links,t,e)},U.prototype.resolveValueOrLink=function(e){var t=this.getRecord(e);return void 0!==t?t:this.getLink(e)||null},U.prototype.resolve=function(e,t,r){if("string"==typeof e)return K(e),this.resolveValueOrLink(b(e,O(t,r)));var n=this.keyOfEntity(e);return null===n?null:(K(n),this.resolveValueOrLink(b(n,O(t,r))))},U.prototype.hasField=function(e){return void 0!==this.getRecord(e)||void 0!==this.getLink(e)},U.prototype.updateQuery=function(e,t){var r=V(this,{query:e});E(this,{query:e},t(r.data))},U.prototype.writeFragment=function(e,t){q(this,e,t)};var V=function(e,t){R(0);var r=m(t.query),n=Object.create(null),i={completeness:"FULL",dependencies:P(),data:n},o={variables:v(r,t.variables),fragments:k(t.query),result:i,store:e};return i.data=Y(o,"Query",c(r),n),N(),i},Y=function(e,t,r,n){var i="Query"===t;i||K(t);var o=e.store,a=e.variables,l=o.getField(t,"__typename");return"string"!=typeof l?(e.result.completeness="EMPTY",null):(n.__typename=l,_(l,t,r,e,function(r){var f=u(r),v=p(r,a),d=s(r),y=b(t,O(f,v)),m=o.getRecord(y);i&&K(y);var h=o.resolvers[l];if(void 0!==h&&h.hasOwnProperty(f)){var k=h[f](n,v||{},o,e);if(void 0===r.selectionSet)n[d]=void 0!==k?k:null;else{var g=c(r);n[d]=C(e,k,y,g,n[d])}}else if(void 0===r.selectionSet)void 0===m?(e.result.completeness="EMPTY",n[d]=null):n[d]=m;else{var w=c(r),_=o.getLink(y);void 0===_?"object"==typeof m&&null!==m?n[d]=m:(e.result.completeness="EMPTY",n[d]=null):n[d]=B(e,_,w,n[d])}}),n)},C=function(e,t,r,n,i){if(Array.isArray(t))return t.map(function(t,o){var u=void 0!==i?i[o]:void 0,a=b(r,""+o);return C(e,t,a,n,u)});if(null===t)return null;if(G(t)){var o=void 0===i?Object.create(null):i,u=("string"==typeof t?t:e.store.keyOfEntity(t))||r,a=Y(e,u,n,o);if(null!==a&&"object"==typeof t)for(r in t)"__typename"!==r&&t.hasOwnProperty(r)&&(a[r]=t[r]);return a}return e.result.completeness="EMPTY",null},B=function(e,t,r,n){if(Array.isArray(t))return t.map(function(t,i){return B(e,t,r,void 0!==n?n[i]:void 0)});if(null===t)return null;var i=void 0===n?Object.create(null):n;return Y(e,t,r,i)},G=function(e){return"string"==typeof e||"object"==typeof e&&null!==e&&"string"==typeof e.__typename},z=function(e){return function(t){return{data:t.data,error:t.error,extensions:t.extensions,operation:w({},t.operation,{context:w({},t.operation.context,{meta:w({},t.operation.context.meta,{cacheOutcome:e})})})}}},J=function(e){return w({},e,{query:i.formatDocument(e.query)})},H=function(e){return e.context.requestPolicy},W=function(e){return"query"===e.operationName},X=function(e){var t=H(e);return W(e)&&"network-only"!==t},Z=function(e,t){return w({},e,{context:w({},e.context,{requestPolicy:t})})};function $(e){return X(e)}function ee(e){return"FULL"!==e.completeness}function te(e){return e.operation}function re(e){return"FULL"===e.completeness}function ne(e){return!X(e)}exports.Store=U,exports.cacheExchange=function(e){return function(t){var r=t.forward,n=t.client;e||(e={});var i=new U(e.resolvers,e.updates,e.optimistic,e.keys),u=new Set,a=new Map,s=Object.create(null),c=function(e,t){var r=new Set;function i(e){return r.add(e)}t.forEach(function(e){var t=s[e];void 0!==t&&(s[e]=[],t.forEach(i))}),r.forEach(function(t){if(t!==e.key){var r=a.get(t);a.delete(t),n.reexecuteOperation(r)}})},l=function(e){if(o=H(n=e),function(e){return"mutation"===e.operationName}(n)&&"network-only"!==o){var t=e.key,r=L(i,e,t).dependencies;0!==r.size&&(u.add(t),c(e,r))}var n,o},f=function(e,t){t.forEach(function(t){(s[t]||(s[t]=[])).push(e.key),a.has(e.key)||a.set(e.key,"network-only"===e.context.requestPolicy?Z(e,"cache-and-network"):e)})},p=function(e){var t=H(e),r=V(i,e),n="cache-only"===t||"FULL"===r.completeness;return n&&f(e,r.dependencies),{operation:e,completeness:n?"FULL":"EMPTY",data:r.data}},v=function(e){var t,r=e.data,n=e.operation;null!=r&&(t=E(i,n,r).dependencies);var o=n.key;u.has(o)&&(u.delete(o),i.clearOptimistic(o)),void 0!==t&&(c(n,t),W(n)&&f(n,t))};function d(e){var t=e.operation;if("cache-and-network"===H(t)){var r=Z(t,"network-only");n.reexecuteOperation(r)}}return function(e){var t=o.pipe(e,o.map(J),o.tap(l),o.share),n=o.pipe(t,o.filter($),o.map(p),o.share),i=o.pipe(n,o.filter(ee),o.map(te)),u=o.pipe(n,o.filter(re),o.tap(d),o.map(z("hit"))),a=o.pipe(r(o.merge([o.pipe(t,o.filter(ne)),i])),o.tap(v),o.map(z("miss")));return o.merge([a,u])}}},exports.defaultKeyOfEntity=g,exports.query=V,exports.write=E,exports.writeFragment=q,exports.writeOptimistic=L;
//# sourceMappingURL=urql-exchange-graphcache.min.js.map
{
"name": "@urql/exchange-graphcache",
"version": "1.0.0-beta.1",
"version": "1.0.0-beta.2",
"description": "A normalized and configurable cache exchange for urql",

@@ -77,3 +77,3 @@ "repository": "https://github.com/kitten/urql-exchange-graphcache",

"graphql": "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0",
"urql": ">= 1.1.3"
"urql": ">= 1.3.0"
},

@@ -80,0 +80,0 @@ "devDependencies": {

@@ -44,5 +44,5 @@ <h2 align="center">@urql/exchange-graphcache</h2>

```sh
yarn add @urql/exchange-suspense
yarn add @urql/exchange-graphcache
# or
npm install --save @urql/exchange-suspense
npm install --save @urql/exchange-graphcache
```

@@ -93,23 +93,36 @@

The documentation contains more information on how to set up some of these
confguration options. More documentation is in progress.
> Note that you don't need any of these options to get started
- [Getting Started guide](./docs/getting-started.md)
- [Architecture information](./docs/architecture.md)
### Keys
## API
Keys are used when you need a slight alteration to the value of your identifier or
when the identifier is a non-traditional property.
The `cacheExchange` accepts an object with three optional configuration items.
[Read more](./docs/keys.md)
```typescript
{
keys?: KeyingConfig;
resolvers?: ResolverConfig;
updates?: UpdatesConfig;
optimistic?: OptimisticMutationConfig;
}
```
### Resolvers
Resolvers are needed when you want to do additional resolving, for example do some
custom date formatting.
[Read more](./docs/resolvers.md)
### Updates
The graph cache will automatically handle updates but some things are quite hard to
incorporate. Let's say you delete/add an item, it's hard for us to know you wanted to
delete or where to add an item in a list.
[Read more](./docs/updates.md)
### Optimistic
Here you can configure optimistic responses, this means that we don't wait for the server
to respond but offer the user to instantly replace the data with the variables from the
mutation.
[Read more](./docs/optimistic.md)
## Maintenance Status
**Active:** Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.

@@ -56,2 +56,11 @@ import gql from 'graphql-tag';

expect(result).toHaveBeenCalledTimes(2);
expect(result.mock.calls[0][0]).toHaveProperty(
'operation.context.meta.cacheOutcome',
'miss'
);
expect(result.mock.calls[1][0]).toHaveProperty(
'operation.context.meta.cacheOutcome',
'hit'
);
});

@@ -58,0 +67,0 @@

@@ -7,2 +7,3 @@ import {

RequestPolicy,
CacheOutcome,
} from 'urql';

@@ -32,2 +33,19 @@

// Returns the given operation result with added cacheOutcome meta field
const addCacheOutcome = (outcome: CacheOutcome) => (res: OperationResult) => ({
data: res.data,
error: res.error,
extensions: res.extensions,
operation: {
...res.operation,
context: {
...res.operation.context,
meta: {
...res.operation.context.meta,
cacheOutcome: outcome,
},
},
},
});
// Returns the given operation with added __typename fields on its query

@@ -81,7 +99,13 @@ const addTypeNames = (op: Operation): Operation => ({

export const cacheExchange = (opts: CacheExchangeOpts): Exchange => ({
export const cacheExchange = (opts?: CacheExchangeOpts): Exchange => ({
forward,
client,
}) => {
const store = new Store(opts.resolvers, opts.updates, opts.optimistic);
if (!opts) opts = {};
const store = new Store(
opts.resolvers,
opts.updates,
opts.optimistic,
opts.keys
);
const optimisticKeys = new Set();

@@ -225,3 +249,4 @@ const ops: OperationMap = new Map();

}
})
}),
map(addCacheOutcome('hit'))
);

@@ -241,3 +266,4 @@

),
tap(updateCacheWithResult)
tap(updateCacheWithResult),
map(addCacheOutcome('miss'))
);

@@ -244,0 +270,0 @@

@@ -24,7 +24,9 @@ import { FieldNode, InlineFragmentNode, FragmentDefinitionNode } from 'graphql';

node: InlineFragmentNode | FragmentDefinitionNode,
typename: string,
typename: void | string,
entityKey: string,
ctx: Context
) => {
if (typename === getTypeCondition(node)) {
if (!typename) {
return false;
} else if (typename === getTypeCondition(node)) {
return true;

@@ -44,3 +46,3 @@ }

export const forEachFieldNode = (
typename: string,
typename: void | string,
entityKey: string,

@@ -47,0 +49,0 @@ select: SelectionSet,

@@ -157,8 +157,9 @@ import {

) => {
const { store, variables } = ctx;
const isQuery = entityKey === 'Query';
const typename = data.__typename;
if (!isQuery) addDependency(entityKey);
const { store, variables } = ctx;
const typename = data.__typename;
store.writeField(typename, entityKey, '__typename');
store.writeField(isQuery ? entityKey : typename, entityKey, '__typename');
forEachFieldNode(typename, entityKey, select, ctx, node => {

@@ -261,2 +262,5 @@ const fieldName = getName(node);

writeSelection(ctx, entityKey, select, data);
} else {
const typename = data.__typename;
writeRoot(ctx, typename, select, data);
}

@@ -263,0 +267,0 @@ };

@@ -30,2 +30,17 @@ import gql from 'graphql-tag';

const NestedClearNameTodo = gql`
mutation($id: ID!) {
__typename
clearName(id: $id) {
__typename
todo {
__typename
id
text
complete
}
}
}
`;
it('passes the "getting-started" example', () => {

@@ -76,5 +91,32 @@ const store = new Store();

});
const newMutatedTodo = {
...mutatedTodo,
text: '',
};
const newMutationRes = write(
store,
{ query: NestedClearNameTodo, variables: { id: '2' } },
{
__typename: 'Mutation',
clearName: {
__typename: 'ClearName',
todo: newMutatedTodo,
},
}
);
expect(newMutationRes.dependencies).toEqual(new Set(['Todo:2']));
queryRes = query(store, { query: Todos });
expect(queryRes.completeness).toBe('FULL');
expect(queryRes.data).toEqual({
...todosData,
todos: [...todosData.todos.slice(0, 2), newMutatedTodo],
});
});
it('Respects property-level resolvers when given', () => {
it('respects property-level resolvers when given', () => {
const store = new Store({ Todo: { text: () => 'hi' } });

@@ -81,0 +123,0 @@ const todosData = {

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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc