Socket
Socket
Sign inDemoInstall

@urql/exchange-graphcache

Package Overview
Dependencies
Maintainers
3
Versions
294
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-rc.0 to 1.0.0-rc.1

dist/types/ast/schemaPredicates.d.ts

3

dist/types/ast/node.d.ts

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

import { NamedTypeNode, NameNode, SelectionNode, SelectionSetNode, InlineFragmentNode, FieldNode, OperationDefinitionNode, FragmentDefinitionNode } from 'graphql';
import { NamedTypeNode, NameNode, SelectionNode, SelectionSetNode, InlineFragmentNode, FieldNode, FragmentDefinitionNode } from 'graphql';
/** Returns the name of a given node */

@@ -6,3 +6,2 @@ export declare const getName: (node: {

}) => string;
export declare const getOperationName: (node: OperationDefinitionNode) => "Query" | "Mutation" | "Subscription";
export declare const getFragmentTypeName: (node: FragmentDefinitionNode) => string;

@@ -9,0 +8,0 @@ /** Returns either the field's name or the field's alias */

@@ -8,3 +8,4 @@ import { Exchange } from 'urql';

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

@@ -1,4 +0,3 @@

import { Variables, KeyGenerator } from '../types';
export declare const keyOfEntity: KeyGenerator;
import { Variables } from '../types';
export declare const keyOfField: (fieldName: string, args?: Variables | null | undefined) => string;
export declare const joinKeys: (parentKey: string, key: string) => string;
export * from './operations';
export * from './types';
export { keyOfEntity as defaultKeyOfEntity } from './helpers';
export { Store } from './store';
export { cacheExchange } from './exchange';

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

export { query, readOperation } from './query';
export { query, read } from './query';
export { write, writeOptimistic, writeFragment } from './write';
import { OperationRequest, Variables, Fragments } from '../types';
import { Store } from '../store';
import { SchemaPredicates } from '../ast/schemaPredicates';
interface Context {

@@ -7,2 +8,3 @@ store: Store;

fragments: Fragments;
schemaPredicates?: SchemaPredicates;
}

@@ -9,0 +11,0 @@ export declare const invalidate: (store: Store, request: OperationRequest) => void;

@@ -1,19 +0,9 @@

import { Fragments, Variables, Data, Completeness, OperationRequest } from '../types';
import { Data, OperationRequest } from '../types';
import { Store } from '../store';
export interface QueryResult {
completeness: Completeness;
dependencies: Set<string>;
partial: boolean;
data: null | Data;
}
interface Context {
result: QueryResult;
store: Store;
variables: Variables;
fragments: Fragments;
}
/** Reads a request entirely from the store */
export declare const query: (store: Store, request: OperationRequest) => QueryResult;
export declare const startQuery: (store: Store, request: OperationRequest) => QueryResult;
export declare const readOperation: (store: Store, request: OperationRequest, data: Data) => QueryResult;
export declare const readRoot: (ctx: Context, entityKey: string, select: readonly import("graphql").SelectionNode[], originalData: Data) => Data;
export {};
export declare const query: (store: Store, request: OperationRequest, data?: Data | undefined) => QueryResult;
export declare const read: (store: Store, request: OperationRequest, input?: Data | undefined) => QueryResult;
import { FieldNode } from 'graphql';
import { Fragments, Variables, SelectionSet, Scalar } from '../types';
import { Store } from '../store';
import { SchemaPredicates } from '../ast/schemaPredicates';
interface Context {

@@ -8,2 +9,3 @@ store: Store;

fragments: Fragments;
schemaPredicates?: SchemaPredicates;
}

@@ -10,0 +12,0 @@ export declare class SelectionIterator {

import { DocumentNode } from 'graphql';
import * as Pessimism from 'pessimism';
import { EntityField, Link, ResolverConfig, DataField, Variables, Data, UpdatesConfig, OptimisticMutationConfig, KeyingConfig } from './types';
import { SchemaPredicates } from './ast/schemaPredicates';
export declare const initStoreState: (optimisticKey: number | null) => void;

@@ -8,2 +9,3 @@ export declare const clearStoreState: () => void;

export declare const addDependency: (dependency: string) => void;
declare type RootField = 'query' | 'mutation' | 'subscription';
export declare class Store {

@@ -16,3 +18,13 @@ records: Pessimism.Map<EntityField>;

keys: KeyingConfig;
constructor(resolvers?: ResolverConfig, updates?: Partial<UpdatesConfig>, optimisticMutations?: OptimisticMutationConfig, keys?: KeyingConfig);
schemaPredicates?: SchemaPredicates;
rootFields: {
query: string;
mutation: string;
subscription: string;
};
rootNames: {
[name: string]: RootField;
};
constructor(schemaPredicates?: SchemaPredicates, resolvers?: ResolverConfig, updates?: Partial<UpdatesConfig>, optimisticMutations?: OptimisticMutationConfig, keys?: KeyingConfig);
getRootKey(name: RootField): string;
keyOfEntity(data: Data): string | null;

@@ -38,1 +50,2 @@ clearOptimistic(optimisticKey: number): void;

}
export {};

@@ -61,2 +61,1 @@ import { DocumentNode, FragmentDefinitionNode, SelectionNode } from 'graphql';

}
export declare type Completeness = 'EMPTY' | 'PARTIAL' | 'FULL';
import warning from "warning";
import { Kind } from "graphql";
import { Kind, buildClientSchema, isNullableType, isNonNullType, isListType, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType } from "graphql";

@@ -17,15 +17,2 @@ import { asMutable, make, clearOptimistic, get, setOptimistic, remove } from "pessimism";

var getOperationName = function(node) {
switch (node.operation) {
case "query":
return "Query";
case "mutation":
return "Mutation";
case "subscription":
return "Subscription";
}
};
var getFieldAlias = function(node) {

@@ -39,2 +26,7 @@ return void 0 !== node.alias ? node.alias.value : getName(node);

var getTypeCondition = function(ref) {
var typeCondition = ref.typeCondition;
return void 0 !== typeCondition ? getName(typeCondition) : null;
};
var isFieldNode = function(node) {

@@ -185,15 +177,2 @@ return node.kind === Kind.FIELD;

var keyOfEntity = function(data) {
var typeName = data.__typename;
var id = void 0 === data.id ? data._id : data.id;
if (null == typeName) {
return null;
} else if ("Query" === typeName || "Mutation" === typeName || "Subscription" === typeName) {
return typeName;
} else if (null == id) {
return null;
}
return typeName + ":" + id;
};
var keyOfField = function(fieldName, args) {

@@ -221,9 +200,11 @@ return args ? fieldName + "(" + stringify(args) + ")" : fieldName;

var isFragmentMatching = function(node, typename, entityKey, ctx) {
var isFragmentHeuristicallyMatching = function(node, typename, entityKey, ctx) {
if (!typename) {
return !1;
} else if (typename === (void 0 !== (typeCondition = node.typeCondition) ? getName(typeCondition) : null)) {
}
var typeCondition = getTypeCondition(node);
if (typename === typeCondition) {
return !0;
}
var typeCondition;
"production" !== process.env.NODE_ENV && warning(!1, "Heuristic Fragment Matching: A fragment is trying to match against the `%s` type, but the type condition is `%s`. Since GraphQL allows for interfaces `%s` may be aninterface.\nA schema needs to be defined for this match to be deterministic, otherwise the fragment will be matched heuristically!", typename, typeCondition, typeCondition);
return !getSelectionSet(node).some(function(node) {

@@ -262,5 +243,7 @@ if (!isFieldNode(node)) {

var fragmentNode = !isInlineFragment(node) ? this.context.fragments[getName(node)] : node;
if (void 0 !== fragmentNode && isFragmentMatching(fragmentNode, this.typename, this.entityKey, this.context)) {
this.indexStack.push(0);
this.selectionStack.push(getSelectionSet(fragmentNode));
if (void 0 !== fragmentNode) {
if (void 0 !== this.context.schemaPredicates ? this.context.schemaPredicates.isInterfaceOfType(getTypeCondition(fragmentNode), this.typename) : isFragmentHeuristicallyMatching(fragmentNode, this.typename, this.entityKey, this.context)) {
this.indexStack.push(0);
this.selectionStack.push(getSelectionSet(fragmentNode));
}
}

@@ -301,7 +284,8 @@ continue;

result: result,
store: store
store: store,
schemaPredicates: store.schemaPredicates
};
var select = getSelectionSet(operation);
var operationName = getOperationName(operation);
if ("Query" === operationName) {
var operationName = ctx.store.getRootKey(operation.operation);
if (operationName === ctx.store.getRootKey("query")) {
writeSelection(ctx, operationName, select, data);

@@ -324,6 +308,7 @@ } else {

result: result,
store: store
store: store,
schemaPredicates: store.schemaPredicates
};
var operationName = getOperationName(operation);
if ("Mutation" === operationName) {
var operationName = ctx.store.getRootKey(operation.operation);
if (operationName === ctx.store.getRootKey("mutation")) {
var select = getSelectionSet(operation);

@@ -372,3 +357,4 @@ var iter = new SelectionIterator(operationName, operationName, select, ctx);

},
store: store
store: store,
schemaPredicates: store.schemaPredicates
};

@@ -381,3 +367,3 @@ writeSelection(ctx, entityKey, select, writeData);

var variables = ctx.variables;
var isQuery = "Query" === entityKey;
var isQuery = entityKey === ctx.store.getRootKey("query");
var typename = data.__typename;

@@ -444,3 +430,3 @@ if (!isQuery) {

}
if ("Mutation" === typename || "Subscription" === typename) {
if (typename === ctx.store.getRootKey("mutation") || typename === ctx.store.getRootKey("subscription")) {
var updater = ctx.store.updates[typename][fieldName];

@@ -558,6 +544,10 @@ if (void 0 !== updater) {

var Store = function Store(resolvers, updates, optimisticMutations, keys) {
var Store = function Store(schemaPredicates, resolvers, updates, optimisticMutations, keys) {
var obj;
this.records = asMutable(make());
this.links = asMutable(make());
this.resolvers = resolvers || {};
this.optimisticMutations = optimisticMutations || {};
this.keys = keys || {};
this.schemaPredicates = schemaPredicates;
this.updates = {

@@ -567,13 +557,55 @@ Mutation: updates && updates.Mutation || {},

};
this.optimisticMutations = optimisticMutations || {};
this.keys = keys || {};
if (schemaPredicates) {
var schema = schemaPredicates.schema;
var queryType = schema.getQueryType();
var mutationType = schema.getMutationType();
var subscriptionType = schema.getSubscriptionType();
var queryName = queryType ? queryType.name : "Query";
var mutationName = mutationType ? mutationType.name : "Mutation";
var subscriptionName = subscriptionType ? subscriptionType.name : "Subscription";
this.rootFields = {
query: queryName,
mutation: mutationName,
subscription: subscriptionName
};
this.rootNames = ((obj = {})[queryName] = "query", obj[mutationName] = "mutation",
obj[subscriptionName] = "subscription", obj);
} else {
this.rootFields = {
query: "Query",
mutation: "Mutation",
subscription: "Subscription"
};
this.rootNames = {
Query: "query",
Mutation: "mutation",
Subscription: "subscription"
};
}
};
Store.prototype.keyOfEntity = function keyOfEntity$1(data) {
Store.prototype.getRootKey = function getRootKey(name) {
return this.rootFields[name];
};
Store.prototype.keyOfEntity = function keyOfEntity(data) {
var typename = data.__typename;
if (void 0 !== typename && void 0 !== this.keys[typename]) {
return this.keys[typename](data);
var id = data.id;
var _id = data._id;
if (!typename) {
return null;
} else if (this.rootNames[typename]) {
return this.rootNames[typename];
}
var key;
if (this.keys[typename]) {
key = "" + this.keys[typename](data);
} else if (null != id) {
key = "" + id;
} else if (null != _id) {
key = "" + _id;
} else {
return keyOfEntity(data);
return null;
}
return key ? typename + ":" + key : null;
};

@@ -650,5 +682,6 @@

fragments: getFragments(request.query),
store: store
store: store,
schemaPredicates: store.schemaPredicates
};
invalidateSelection(ctx, "Query", getSelectionSet(operation));
invalidateSelection(ctx, ctx.store.getRootKey("query"), getSelectionSet(operation));
clearStoreState();

@@ -666,4 +699,3 @@ }(this, {

Store.prototype.updateQuery = function updateQuery(ctx, updater) {
var ref = startQuery(this, ctx);
var output = updater("EMPTY" === ref.completeness ? null : ref.data);
var output = updater(read(this, ctx).data);
if (null !== output) {

@@ -678,5 +710,5 @@ startWrite(this, ctx, output);

var query = function(store, request) {
var query = function(store, request, data) {
initStoreState(0);
var result = startQuery(store, request);
var result = read(store, request, data);
clearStoreState();

@@ -686,37 +718,20 @@ return result;

var startQuery = function(store, request) {
var read = function(store, request, input) {
var operation = getMainOperation(request.query);
var root = Object.create(null);
var result = {
completeness: "FULL",
dependencies: getCurrentDependencies(),
data: root
};
var rootKey = store.getRootKey(operation.operation);
var rootSelect = getSelectionSet(operation);
var ctx = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result: result,
store: store
partial: !1,
store: store,
schemaPredicates: store.schemaPredicates
};
result.data = readSelection(ctx, "Query", getSelectionSet(operation), root);
return result;
};
var readOperation = function(store, request, data) {
initStoreState(0);
var operation = getMainOperation(request.query);
var result = {
completeness: "FULL",
var data = input || Object.create(null);
data = "Query" !== rootKey ? readRoot(ctx, rootKey, rootSelect, data) : readSelection(ctx, rootKey, rootSelect, data);
return {
dependencies: getCurrentDependencies(),
data: null
partial: void 0 === data ? !1 : ctx.partial,
data: void 0 === data ? null : data
};
var ctx = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result: result,
store: store
};
result.data = readRoot(ctx, getOperationName(operation), getSelectionSet(operation), data);
clearStoreState();
return result;
};

@@ -756,4 +771,5 @@

if (null !== entityKey) {
var data = Object.create(null);
return readSelection(ctx, entityKey, select, data);
var newData$1 = Object.create(null);
var fieldValue = readSelection(ctx, entityKey, select, newData$1);
return void 0 === fieldValue ? null : fieldValue;
} else {

@@ -765,12 +781,12 @@ return readRoot(ctx, originalData.__typename, select, originalData);

var readSelection = function(ctx, entityKey, select, data) {
var isQuery = "Query" === entityKey;
var store = ctx.store;
var variables = ctx.variables;
var schemaPredicates = ctx.schemaPredicates;
var isQuery = entityKey === store.getRootKey("query");
if (!isQuery) {
addDependency(entityKey);
}
var store = ctx.store;
var variables = ctx.variables;
var typename = isQuery ? "Query" : store.getField(entityKey, "__typename");
var typename = isQuery ? entityKey : store.getField(entityKey, "__typename");
if ("string" != typeof typename) {
ctx.result.completeness = "EMPTY";
return null;
return;
}

@@ -780,2 +796,4 @@ data.__typename = typename;

var node;
var hasFields = !1;
var hasPartials = !1;
while (void 0 !== (node = iter.next())) {

@@ -790,4 +808,5 @@ var fieldName = getName(node);

}
var dataFieldValue = void 0;
var resolvers = store.resolvers[typename];
if (void 0 !== resolvers && resolvers.hasOwnProperty(fieldName)) {
if (void 0 !== resolvers && "function" == typeof resolvers[fieldName]) {
if (void 0 !== fieldValue) {

@@ -797,65 +816,77 @@ data[fieldAlias] = fieldValue;

var resolverValue = resolvers[fieldName](data, fieldArgs || {}, store, ctx);
if (void 0 === node.selectionSet) {
data[fieldAlias] = void 0 !== resolverValue ? resolverValue : null;
if (void 0 !== node.selectionSet) {
resolverValue = resolveResolverResult(ctx, resolverValue, typename, fieldName, fieldKey, getSelectionSet(node), data[fieldAlias]);
}
var isNull = null == resolverValue;
if (isNull && void 0 !== schemaPredicates) {
dataFieldValue = void 0;
} else {
var fieldSelect = getSelectionSet(node);
data[fieldAlias] = resolveResolverResult(ctx, resolverValue, fieldKey, fieldSelect, data[fieldAlias]);
dataFieldValue = isNull ? null : resolverValue;
}
} else if (void 0 === node.selectionSet) {
if (void 0 === fieldValue) {
ctx.result.completeness = "EMPTY";
data[fieldAlias] = null;
} else {
data[fieldAlias] = fieldValue;
}
dataFieldValue = fieldValue;
} else {
var fieldSelect$1 = getSelectionSet(node);
var fieldSelect = getSelectionSet(node);
var link = store.getLink(fieldKey);
if (void 0 === link) {
if ("object" == typeof fieldValue && null !== fieldValue) {
data[fieldAlias] = fieldValue;
} else {
ctx.result.completeness = "EMPTY";
data[fieldAlias] = null;
}
} else {
data[fieldAlias] = resolveLink(ctx, link, fieldSelect$1, data[fieldAlias]);
if (void 0 !== link) {
dataFieldValue = resolveLink(ctx, link, typename, fieldName, fieldSelect, data[fieldAlias]);
} else if ("object" == typeof fieldValue && null !== fieldValue) {
dataFieldValue = fieldValue;
}
}
if (void 0 === dataFieldValue && void 0 !== schemaPredicates && schemaPredicates.isFieldNullable(typename, fieldName)) {
hasPartials = !0;
data[fieldAlias] = null;
} else if (void 0 === dataFieldValue) {
return;
} else {
hasFields = !0;
data[fieldAlias] = dataFieldValue;
}
}
return data;
if (hasPartials) {
ctx.partial = !0;
}
return isQuery && hasPartials && !hasFields ? void 0 : data;
};
var resolveResolverResult = function(ctx, result, key, select, prevData) {
var resolveResolverResult = function(ctx, result, typename, fieldName, key, select, prevData) {
if (Array.isArray(result)) {
return result.map(function _ref(childResult, index) {
var data = void 0 !== prevData ? prevData[index] : void 0;
var indexKey = joinKeys(key, "" + index);
return resolveResolverResult(ctx, childResult, indexKey, select, data);
});
var schemaPredicates = ctx.schemaPredicates;
var isListNullable = void 0 !== schemaPredicates && schemaPredicates.isListNullable(typename, fieldName);
var newResult = new Array(result.length);
for (var i = 0, l = result.length; i < l; i++) {
var data = void 0 !== prevData ? prevData[i] : void 0;
var childKey = joinKeys(key, "" + i);
var childResult = resolveResolverResult(ctx, result[i], typename, fieldName, childKey, select, data);
if (void 0 === childResult && !isListNullable) {
return;
} else {
result[i] = void 0 !== childResult ? childResult : null;
}
}
return newResult;
} else if (null === result) {
return null;
} else if (isDataOrKey(result)) {
var data = void 0 === prevData ? Object.create(null) : prevData;
var childKey = ("string" == typeof result ? result : ctx.store.keyOfEntity(result)) || key;
var selectionResult = readSelection(ctx, childKey, select, data);
if (null !== selectionResult && "object" == typeof result) {
for (key in result) {
if ("__typename" !== key && result.hasOwnProperty(key)) {
selectionResult[key] = result[key];
}
}
}
return selectionResult;
var data$1 = void 0 === prevData ? Object.create(null) : prevData;
var childKey$1 = ("string" == typeof result ? result : ctx.store.keyOfEntity(result)) || key;
return readSelection(ctx, childKey$1, select, data$1);
}
"production" !== process.env.NODE_ENV && warning(!1, "Invalid resolver value: The resolver at `%s` returned a scalar (number, boolean, etc), but the GraphQL query expects a selection set for this field.\nIf necessary, use Cache.resolve() to resolve a link or entity from the cache.", key);
ctx.result.completeness = "EMPTY";
return null;
return;
};
var resolveLink = function(ctx, link, select, prevData) {
var resolveLink = function(ctx, link, typename, fieldName, select, prevData) {
if (Array.isArray(link)) {
var schemaPredicates = ctx.schemaPredicates;
var isListNullable = void 0 !== schemaPredicates && schemaPredicates.isListNullable(typename, fieldName);
var newLink = new Array(link.length);
for (var i = 0, l = link.length; i < l; i++) {
newLink[i] = resolveLink(ctx, link[i], select, void 0 !== prevData ? prevData[i] : void 0);
var childLink = resolveLink(ctx, link[i], typename, fieldName, select, void 0 !== prevData ? prevData[i] : void 0);
if (void 0 === childLink && !isListNullable) {
return;
} else {
newLink[i] = void 0 !== childLink ? childLink : null;
}
}

@@ -866,4 +897,4 @@ return newLink;

} else {
var data$1 = void 0 === prevData ? Object.create(null) : prevData;
return readSelection(ctx, link, select, data$1);
var data = void 0 === prevData ? Object.create(null) : prevData;
return readSelection(ctx, link, select, data);
}

@@ -876,17 +907,69 @@ };

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 SchemaPredicates = function SchemaPredicates(schema) {
this.schema = buildClientSchema(schema);
};
SchemaPredicates.prototype.isFieldNullable = function isFieldNullable(typename, fieldName) {
var field = getField(this.schema, typename, fieldName);
if (void 0 === field) {
return !1;
}
return isNullableType(field.type);
};
SchemaPredicates.prototype.isListNullable = function isListNullable(typename, fieldName) {
var field = getField(this.schema, typename, fieldName);
if (void 0 === field) {
return !1;
}
var ofType = isNonNullType(field.type) ? field.type.ofType : field.type;
return isListType(ofType) && isNullableType(ofType.ofType);
};
SchemaPredicates.prototype.isInterfaceOfType = function isInterfaceOfType(typeCondition, typename) {
if (!typename || !typeCondition) {
return !1;
}
if (typename === typeCondition) {
return !0;
}
var abstractType = this.schema.getType(typeCondition);
expectAbstractType(abstractType, typeCondition);
var objectType = this.schema.getType(typename);
expectObjectType(objectType, typename);
return this.schema.isPossibleType(abstractType, objectType);
};
var getField = function(schema, typename, fieldName) {
var type = schema.getType(typename);
expectObjectType(type, typename);
var object = type;
if (void 0 === object) {
"production" !== process.env.NODE_ENV && warning(!1, "Invalid type: The type `%s` is not a type in the defined schema, but the GraphQL document expects it to exist.\nTraversal will continue, however this may lead to undefined behavior!", typename);
return;
}
var field = object.getFields()[fieldName];
if (void 0 === field) {
"production" !== process.env.NODE_ENV && warning(!1, "Invalid field: The field `%s` does not exist on `%s`, but the GraphQL document expects it to exist.\nTraversal will continue, however this may lead to undefined behavior!", fieldName, typename);
return;
}
return field;
};
var expectObjectType = function(type, typename) {
"production" !== process.env.NODE_ENV && browser(type instanceof GraphQLObjectType, "Invalid type: The type `%s` is not an object in the defined schema, but the GraphQL document is traversing it.", typename);
};
var expectAbstractType = function(type, typename) {
"production" !== process.env.NODE_ENV && browser(type instanceof GraphQLInterfaceType || type instanceof GraphQLUnionType, "Invalid type: The type `%s` is not an Interface or Union type in the defined schema, but a fragment in the GraphQL document is using it as a type condition.", typename);
};
var addCacheOutcome = function(op, outcome) {
return _extends({}, op, {
context: _extends({}, op.context, {
meta: _extends({}, op.context.meta, {
cacheOutcome: outcome
})
};
};
})
});
};

@@ -926,11 +1009,11 @@

function _ref3(res) {
return "FULL" !== res.completeness;
return "miss" === res.outcome;
}
function _ref4(res) {
return res.operation;
return addCacheOutcome(res.operation, res.outcome);
}
function _ref5(res) {
return "FULL" === res.completeness;
return "miss" !== res.outcome;
}

@@ -949,3 +1032,3 @@

}
var store = new Store(opts.resolvers, opts.updates, opts.optimistic, opts.keys);
var store = new Store(opts.schema ? new SchemaPredicates(opts.schema) : void 0, opts.resolvers, opts.updates, opts.optimistic, opts.keys);
var optimisticKeys = new Set();

@@ -997,11 +1080,16 @@ var ops = new Map();

var policy = getRequestPolicy(operation);
var res = query(store, operation);
var isComplete = "cache-only" === policy || "FULL" === res.completeness;
if (isComplete) {
updateDependencies(operation, res.dependencies);
var ref = query(store, operation);
var data = ref.data;
var partial = ref.partial;
var cacheOutcome;
if (null === data) {
cacheOutcome = "miss";
} else {
updateDependencies(operation, ref.dependencies);
cacheOutcome = !partial || "cache-only" === policy ? "hit" : "partial";
}
return {
outcome: cacheOutcome,
operation: operation,
completeness: isComplete ? "FULL" : "EMPTY",
data: res.data
data: data
};

@@ -1028,3 +1116,3 @@ };

} else {
data = readOperation(store, operation, data).data;
data = query(store, operation, data).data;
}

@@ -1045,8 +1133,16 @@ }

};
function _ref6(ref) {
var operation = ref.operation;
if ("cache-and-network" === getRequestPolicy(operation)) {
function _ref6(res) {
var operation = res.operation;
var outcome = res.outcome;
var policy = getRequestPolicy(operation);
if ("cache-and-network" === policy || "cache-first" === policy && "partial" === outcome) {
var networkOnly = toRequestPolicy(operation, "network-only");
client.reexecuteOperation(networkOnly);
}
return {
operation: addCacheOutcome(operation, outcome),
data: res.data,
error: res.error,
extensions: res.extensions
};
}

@@ -1057,4 +1153,4 @@ return function(ops$) {

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

@@ -1065,3 +1161,3 @@ };

export { Store, cacheExchange, keyOfEntity as defaultKeyOfEntity, query, readOperation, write, writeFragment, writeOptimistic };
export { Store, cacheExchange, query, read, write, writeFragment, writeOptimistic };
//# sourceMappingURL=urql-exchange-graphcache.es.js.map

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

import"warning";import{Kind as e}from"graphql";import{asMutable as t,make as r,clearOptimistic as n,get as i,setOptimistic as o,remove as u}from"pessimism";import a from"fast-json-stable-stringify";import{formatDocument as s}from"urql";import{pipe as l,map as c,tap as f,share as v,filter as d,merge as p}from"wonka";var y=function(e){return e.name.value},h=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:y(e)},k=function(e){return void 0!==e.selectionSet?e.selectionSet.selections:[]},g=function(t){return t.kind===e.FIELD},b=function(t){return t.kind===e.INLINE_FRAGMENT},_=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:for(var n=new Array(t.values.length),i=0,o=t.values.length;i<o;i++)n[i]=_(t.values[i],r);return n;case e.OBJECT:for(var u=Object.create(null),a=0,s=t.fields.length;a<s;a++){var l=t.fields[a];u[y(l)]=_(l.value,r)}return u;case e.VARIABLE:var c=r[y(t)];return void 0!==c?c:null;default:return t.value}},w=function(e,t){if(void 0===e.arguments||0===e.arguments.length)return null;for(var r=Object.create(null),n=0,i=e.arguments.length;n<i;n++){var o=e.arguments[n];r[y(o)]=_(o.value,t)}return r},O=function(e,t){if(void 0===e.variableDefinitions)return{};var r=t||{};return e.variableDefinitions.reduce(function(e,t){var n=y(t.variable),i=r[n];if(void 0===i){if(void 0===t.defaultValue)return e;i=_(t.defaultValue,r)}return e[n]=i,e},Object.create(null))},x=function(t){return t.kind===e.FRAGMENT_DEFINITION};function L(t){return t.kind===e.OPERATION_DEFINITION}var S=function(e){return e.definitions.find(L)};function E(e,t){return e[y(t)]=t,e}var A=function(e){return e.definitions.filter(x).reduce(E,{})},F=function(e,t){if(void 0===e.directives)return!0;for(var r=e.directives,n=0,i=r.length;n<i;n++){var o=r[n],u=y(o),a="include"===u;if(a||"skip"===u){var s=o.arguments?o.arguments[0]:null;if(s&&"if"===y(s)){var l=_(s.value,t);if("boolean"==typeof l||null===l)return a?!!l:!l}}}return!0},q=function(e){var t=e.__typename,r=void 0===e.id?e._id:e.id;return null==t?null:"Query"===t||"Mutation"===t||"Subscription"===t?t:null==r?null:t+":"+r},R=function(e,t){return t?e+"("+a(t)+")":e},j=function(e,t){return e+"."+t};function M(){return(M=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 T=function(e,t,r,n){return!(!t||t!==(void 0!==(i=e.typeCondition)?y(i):null)&&k(e).some(function(e){if(!g(e))return!1;var t=y(e),i=w(e,n.variables),o=R(t,i);return!n.store.hasField(j(r,o))}));var i},I=function(e,t,r,n){this.typename=e,this.entityKey=t,this.context=n,this.indexStack=[0],this.selectionStack=[r]};I.prototype.next=function(){for(;0!==this.indexStack.length;){var e=this.indexStack[this.indexStack.length-1]++,t=this.selectionStack[this.selectionStack.length-1];if(e>=t.length)this.indexStack.pop(),this.selectionStack.pop();else{var r=t[e];if(F(r,this.context.variables)){if(g(r)){if("__typename"===y(r))continue;return r}var n=b(r)?r:this.context.fragments[y(r)];void 0!==n&&T(n,this.typename,this.entityKey,this.context)&&(this.indexStack.push(0),this.selectionStack.push(k(n)))}}}};var N=function(e){return Array.isArray(e)?e.some(N):"object"!=typeof e||null!==e&&"string"!=typeof e.__typename},P=function(e,t,r){J(0);var n=Q(e,t,r);return H(),n},Q=function(e,t,r){var n=S(t.query),i={dependencies:W()},o={variables:O(n,t.variables),fragments:A(t.query),result:i,store:e},u=k(n),a=h(n);return"Query"===a?Y(o,a,u,r):C(o,a,u,r),i},U=function(e,t,r){J(r);var n=S(t.query),i={dependencies:W()},o={variables:O(n,t.variables),fragments:A(t.query),result:i,store:e},u=h(n);if("Mutation"===u)for(var a,s=k(n),l=new I(u,u,s,o);void 0!==(a=l.next());)if(void 0!==a.selectionSet){var c=y(a),f=o.store.optimisticMutations[c];if(void 0!==f){var v=w(a,o.variables),d=k(a),p=f(v||{},o.store,o);N(p)||K(o,p,d)}}return H(),i},V=function(e,t,r){var n=A(t),i=n[Object.keys(n)[0]];if(void 0!==i){var o=k(i),u=M({},r,{__typename:i.typeCondition.name.value}),a=e.keyOfEntity(u);if(a){var s={variables:{},fragments:n,result:{dependencies:W()},store:e};Y(s,a,o,u)}}},Y=function(e,t,r,n){var i=e.store,o=e.variables,u="Query"===t,a=n.__typename;u||X(t),i.writeField(u?t:a,t,"__typename");for(var s,l=new I(a,t,r,e);void 0!==(s=l.next());){var c=y(s),f=w(s,o),v=j(t,R(c,f)),d=n[m(s)];if(u&&X(v),void 0===s.selectionSet)i.writeRecord(d,v);else if(N(d))i.writeRecord(d,v);else{var p=k(s),h=D(e,v,p,d);i.writeLink(h,v),i.removeRecord(v)}}},D=function(e,t,r,n){if(Array.isArray(n)){for(var i=new Array(n.length),o=0,u=n.length;o<u;o++){var a=n[o],s=j(t,""+o),l=D(e,s,r,a);i[o]=l}return i}if(null===n)return null;var c=e.store.keyOfEntity(n),f=null!==c?c:t;return Y(e,f,r,n),f},C=function(e,t,r,n){for(var i,o=new I(t,t,r,e);void 0!==(i=o.next());){var u=y(i),a=m(i),s=w(i,e.variables),l=n[a];if(void 0!==i.selectionSet&&null!==l&&!N(l)){var c=k(i);K(e,l,c)}if("Mutation"===t||"Subscription"===t){var f=e.store.updates[t][u];void 0!==f&&f(n,s||{},e.store,e)}}},K=function(e,t,r){if(Array.isArray(t)){for(var n=new Array(t.length),i=0,o=t.length;i<o;i++)n[i]=K(e,t[i],r);return n}if(null!==t){var u=e.store.keyOfEntity(t);null!==u?Y(e,u,r,t):C(e,t.__typename,r,t)}},B=function(e,t,r){var n,i=e.store,o=e.variables,u="Query"===t;if(u)n=t;else{if(X(t),"string"!=typeof(n=i.getField(t,"__typename")))return;i.removeRecord(j(t,R("__typename")))}for(var a,s=new I(n,t,r,e);void 0!==(a=s.next());){var l=y(a),c=w(a,o),f=j(t,R(l,c));if(u&&X(f),void 0===a.selectionSet)i.removeRecord(f);else{var v=k(a),d=i.getLink(f);if(i.removeLink(f),void 0===d)void 0!==i.getRecord(f)&&i.removeRecord(f);else if(Array.isArray(d))for(var p=0,h=d.length;p<h;p++){var m=d[p];null!==m&&B(e,m,v)}else null!==d&&B(e,d,v)}}},G={current:null},z={current:null},J=function(e){G.current=new Set,z.current=e},H=function(){G.current=null,z.current=null},W=function(){return G.current},X=function(e){G.current.add(e)},Z=function(e,t,r){return o(e,t,r,z.current||0)},$=function(e,t){var r=z.current||0;return r?o(e,t,void 0,r):u(e,t)},ee=function(e,n,i,o){this.records=t(r()),this.links=t(r()),this.resolvers=e||{},this.updates={Mutation:n&&n.Mutation||{},Subscription:n&&n.Subscription||{}},this.optimisticMutations=i||{},this.keys=o||{}};ee.prototype.keyOfEntity=function(e){var t=e.__typename;return void 0!==t&&void 0!==this.keys[t]?this.keys[t](e):q(e)},ee.prototype.clearOptimistic=function(e){this.records=n(this.records,e),this.links=n(this.links,e)},ee.prototype.getRecord=function(e){return i(this.records,e)},ee.prototype.removeRecord=function(e){return this.records=$(this.records,e)},ee.prototype.writeRecord=function(e,t){return this.records=Z(this.records,t,e)},ee.prototype.getField=function(e,t,r){var n=j(e,R(t,r));return this.getRecord(n)},ee.prototype.writeField=function(e,t,r,n){var i=j(t,R(r,n));return this.records=Z(this.records,i,e)},ee.prototype.getLink=function(e){return i(this.links,e)},ee.prototype.removeLink=function(e){return this.links=$(this.links,e)},ee.prototype.writeLink=function(e,t){return this.links=Z(this.links,t,e)},ee.prototype.resolveValueOrLink=function(e){var t=this.getRecord(e);return void 0!==t?t:this.getLink(e)||null},ee.prototype.resolve=function(e,t,r){if("string"==typeof e)return X(e),this.resolveValueOrLink(j(e,R(t,r)));var n=this.keyOfEntity(e);return null===n?null:(X(n),this.resolveValueOrLink(j(n,R(t,r))))},ee.prototype.invalidateQuery=function(e,t){!function(e,t){J(0);var r=S(t.query),n={variables:O(r,t.variables),fragments:A(t.query),store:e};B(n,"Query",k(r)),H()}(this,{query:e,variables:t})},ee.prototype.hasField=function(e){return void 0!==this.getRecord(e)||void 0!==this.getLink(e)},ee.prototype.updateQuery=function(e,t){var r=re(this,e),n=t("EMPTY"===r.completeness?null:r.data);null!==n&&Q(this,e,n)},ee.prototype.writeFragment=function(e,t){V(this,e,t)};var te=function(e,t){J(0);var r=re(e,t);return H(),r},re=function(e,t){var r=S(t.query),n=Object.create(null),i={completeness:"FULL",dependencies:W(),data:n},o={variables:O(r,t.variables),fragments:A(t.query),result:i,store:e};return i.data=ue(o,"Query",k(r),n),i},ne=function(e,t,r){J(0);var n=S(t.query),i={completeness:"FULL",dependencies:W(),data:null},o={variables:O(n,t.variables),fragments:A(t.query),result:i,store:e};return i.data=ie(o,h(n),k(n),r),H(),i},ie=function(e,t,r,n){if("string"!=typeof n.__typename)return n;var i=Object.create(null);i.__typename=n.__typename;for(var o,u=new I(t,t,r,e);void 0!==(o=u.next());){var a=m(o),s=n[a];i[a]=void 0===o.selectionSet||null===s||N(s)?s:oe(e,k(o),s)}return i},oe=function(e,t,r){if(Array.isArray(r)){for(var n=new Array(r.length),i=0,o=r.length;i<o;i++)n[i]=oe(e,t,r[i]);return n}if(null===r)return null;var u=e.store.keyOfEntity(r);if(null!==u){var a=Object.create(null);return ue(e,u,t,a)}return ie(e,r.__typename,t,r)},ue=function(e,t,r,n){var i="Query"===t;i||X(t);var o=e.store,u=e.variables,a=i?"Query":o.getField(t,"__typename");if("string"!=typeof a)return e.result.completeness="EMPTY",null;n.__typename=a;for(var s,l=new I(a,t,r,e);void 0!==(s=l.next());){var c=y(s),f=w(s,u),v=m(s),d=j(t,R(c,f)),p=o.getRecord(d);i&&X(d);var h=o.resolvers[a];if(void 0!==h&&h.hasOwnProperty(c)){void 0!==p&&(n[v]=p);var g=h[c](n,f||{},o,e);if(void 0===s.selectionSet)n[v]=void 0!==g?g:null;else{var b=k(s);n[v]=ae(e,g,d,b,n[v])}}else if(void 0===s.selectionSet)void 0===p?(e.result.completeness="EMPTY",n[v]=null):n[v]=p;else{var _=k(s),O=o.getLink(d);void 0===O?"object"==typeof p&&null!==p?n[v]=p:(e.result.completeness="EMPTY",n[v]=null):n[v]=se(e,O,_,n[v])}}return n},ae=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 ae(e,t,a,n,u)});if(null===t)return null;if(le(t)){var o=void 0===i?Object.create(null):i,u=("string"==typeof t?t:e.store.keyOfEntity(t))||r,a=ue(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},se=function(e,t,r,n){if(Array.isArray(t)){for(var i=new Array(t.length),o=0,u=t.length;o<u;o++)i[o]=se(e,t[o],r,void 0!==n?n[o]:void 0);return i}if(null===t)return null;var a=void 0===n?Object.create(null):n;return ue(e,t,r,a)},le=function(e){return"string"==typeof e||"object"==typeof e&&null!==e&&"string"==typeof e.__typename},ce=function(e){return function(t){return{data:t.data,error:t.error,extensions:t.extensions,operation:M({},t.operation,{context:M({},t.operation.context,{meta:M({},t.operation.context.meta,{cacheOutcome:e})})})}}},fe=function(e){return M({},e,{query:s(e.query)})},ve=function(e){return e.context.requestPolicy},de=function(e){return"query"===e.operationName},pe=function(e){var t=ve(e);return de(e)&&"network-only"!==t},ye=function(e,t){return M({},e,{context:M({},e.context,{requestPolicy:t})})};function he(e){return pe(e)}function me(e){return"FULL"!==e.completeness}function ke(e){return e.operation}function ge(e){return"FULL"===e.completeness}function be(e){return!pe(e)}var _e=function(e){return function(t){var r=t.forward,n=t.client;e||(e={});var i=new ee(e.resolvers,e.updates,e.optimistic,e.keys),o=new Set,u=new Map,a=Object.create(null),s=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=ve(n=e),function(e){return"mutation"===e.operationName}(n)&&"network-only"!==u){var t=e.key,r=U(i,e,t).dependencies;0!==r.size&&(o.add(t),s(e,r))}var n,u},h=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?ye(e,"cache-and-network"):e)})},m=function(e){var t=ve(e),r=te(i,e),n="cache-only"===t||"FULL"===r.completeness;return n&&h(e,r.dependencies),{operation:e,completeness:n?"FULL":"EMPTY",data:r.data}},k=function(e){var t,r,n=e.operation,u=e.error,a=e.extensions,l=de(n),c=e.data,f=n.key;if(o.has(f)&&(o.delete(f),i.clearOptimistic(f)),null!=c)if(t=P(i,n,c).dependencies,l){var v=te(i,n);c=v.data,r=v.dependencies}else c=ne(i,n,c).data;return void 0!==t&&s(e.operation,t),l&&void 0!==r&&h(e.operation,r),{data:c,error:u,extensions:a,operation:n}};function g(e){var t=e.operation;if("cache-and-network"===ve(t)){var r=ye(t,"network-only");n.reexecuteOperation(r)}}return function(e){var t=l(e,c(fe),f(y),v),n=l(t,d(he),c(m),v),i=l(n,d(me),c(ke)),o=l(n,d(ge),f(g),c(ce("hit"))),u=l(r(p([l(t,d(be)),i])),c(k),c(ce("miss")));return p([u,o])}}};export{ee as Store,_e as cacheExchange,q as defaultKeyOfEntity,te as query,ne as readOperation,P as write,V as writeFragment,U as writeOptimistic};
import"warning";import{Kind as e,buildClientSchema as t,isNullableType as r,isNonNullType as n,isListType as i}from"graphql";import{asMutable as o,make as a,clearOptimistic as u,get as s,setOptimistic as c,remove as l}from"pessimism";import f from"fast-json-stable-stringify";import{formatDocument as v}from"urql";import{pipe as d,map as y,tap as p,share as h,filter as m,merge as g}from"wonka";var k=function(e){return e.name.value},b=function(e){return void 0!==e.alias?e.alias.value:k(e)},w=function(e){return void 0!==e.selectionSet?e.selectionSet.selections:[]},O=function(e){var t=e.typeCondition;return void 0!==t?k(t):null},_=function(t){return t.kind===e.FIELD},x=function(t){return t.kind===e.INLINE_FRAGMENT},S=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:for(var n=new Array(t.values.length),i=0,o=t.values.length;i<o;i++)n[i]=S(t.values[i],r);return n;case e.OBJECT:for(var a=Object.create(null),u=0,s=t.fields.length;u<s;u++){var c=t.fields[u];a[k(c)]=S(c.value,r)}return a;case e.VARIABLE:var l=r[k(t)];return void 0!==l?l:null;default:return t.value}},R=function(e,t){if(void 0===e.arguments||0===e.arguments.length)return null;for(var r=Object.create(null),n=0,i=e.arguments.length;n<i;n++){var o=e.arguments[n];r[k(o)]=S(o.value,t)}return r},A=function(e,t){if(void 0===e.variableDefinitions)return{};var r=t||{};return e.variableDefinitions.reduce(function(e,t){var n=k(t.variable),i=r[n];if(void 0===i){if(void 0===t.defaultValue)return e;i=S(t.defaultValue,r)}return e[n]=i,e},Object.create(null))},q=function(t){return t.kind===e.FRAGMENT_DEFINITION};function L(t){return t.kind===e.OPERATION_DEFINITION}var N=function(e){return e.definitions.find(L)};function P(e,t){return e[k(t)]=t,e}var F=function(e){return e.definitions.filter(q).reduce(P,{})},E=function(e,t){if(void 0===e.directives)return!0;for(var r=e.directives,n=0,i=r.length;n<i;n++){var o=r[n],a=k(o),u="include"===a;if(u||"skip"===a){var s=o.arguments?o.arguments[0]:null;if(s&&"if"===k(s)){var c=S(s.value,t);if("boolean"==typeof c||null===c)return u?!!c:!c}}}return!0},T=function(e,t){return t?e+"("+f(t)+")":e},j=function(e,t){return e+"."+t};function I(){return(I=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 K=function(e,t,r,n){return!(!t||t!==O(e)&&w(e).some(function(e){if(!_(e))return!1;var t=k(e),i=R(e,n.variables),o=T(t,i);return!n.store.hasField(j(r,o))}))},M=function(e,t,r,n){this.typename=e,this.entityKey=t,this.context=n,this.indexStack=[0],this.selectionStack=[r]};M.prototype.next=function(){for(;0!==this.indexStack.length;){var e=this.indexStack[this.indexStack.length-1]++,t=this.selectionStack[this.selectionStack.length-1];if(e>=t.length)this.indexStack.pop(),this.selectionStack.pop();else{var r=t[e];if(E(r,this.context.variables)){if(_(r)){if("__typename"===k(r))continue;return r}var n=x(r)?r:this.context.fragments[k(r)];void 0!==n&&(void 0!==this.context.schemaPredicates?this.context.schemaPredicates.isInterfaceOfType(O(n),this.typename):K(n,this.typename,this.entityKey,this.context))&&(this.indexStack.push(0),this.selectionStack.push(w(n)))}}}};var Q=function(e){return Array.isArray(e)?e.some(Q):"object"!=typeof e||null!==e&&"string"!=typeof e.__typename},V=function(e,t,r){Y(0);var n=D(e,t,r);return Z(),n},D=function(e,t,r){var n=N(t.query),i={dependencies:$()},o={variables:A(n,t.variables),fragments:F(t.query),result:i,store:e,schemaPredicates:e.schemaPredicates},a=w(n),u=o.store.getRootKey(n.operation);return u===o.store.getRootKey("query")?G(o,u,a,r):J(o,u,a,r),i},C=function(e,t,r){Y(r);var n=N(t.query),i={dependencies:$()},o={variables:A(n,t.variables),fragments:F(t.query),result:i,store:e,schemaPredicates:e.schemaPredicates},a=o.store.getRootKey(n.operation);if(a===o.store.getRootKey("mutation"))for(var u,s=w(n),c=new M(a,a,s,o);void 0!==(u=c.next());)if(void 0!==u.selectionSet){var l=k(u),f=o.store.optimisticMutations[l];if(void 0!==f){var v=R(u,o.variables),d=w(u),y=f(v||{},o.store,o);Q(y)||U(o,y,d)}}return Z(),i},B=function(e,t,r){var n=F(t),i=n[Object.keys(n)[0]];if(void 0!==i){var o=w(i),a=I({},r,{__typename:i.typeCondition.name.value}),u=e.keyOfEntity(a);if(u){var s={variables:{},fragments:n,result:{dependencies:$()},store:e,schemaPredicates:e.schemaPredicates};G(s,u,o,a)}}},G=function(e,t,r,n){var i=e.store,o=e.variables,a=t===e.store.getRootKey("query"),u=n.__typename;a||ee(t),i.writeField(a?t:u,t,"__typename");for(var s,c=new M(u,t,r,e);void 0!==(s=c.next());){var l=k(s),f=R(s,o),v=j(t,T(l,f)),d=n[b(s)];if(a&&ee(v),void 0===s.selectionSet)i.writeRecord(d,v);else if(Q(d))i.writeRecord(d,v);else{var y=w(s),p=z(e,v,y,d);i.writeLink(p,v),i.removeRecord(v)}}},z=function(e,t,r,n){if(Array.isArray(n)){for(var i=new Array(n.length),o=0,a=n.length;o<a;o++){var u=n[o],s=j(t,""+o),c=z(e,s,r,u);i[o]=c}return i}if(null===n)return null;var l=e.store.keyOfEntity(n),f=null!==l?l:t;return G(e,f,r,n),f},J=function(e,t,r,n){for(var i,o=new M(t,t,r,e);void 0!==(i=o.next());){var a=k(i),u=b(i),s=R(i,e.variables),c=n[u];if(void 0!==i.selectionSet&&null!==c&&!Q(c)){var l=w(i);U(e,c,l)}if(t===e.store.getRootKey("mutation")||t===e.store.getRootKey("subscription")){var f=e.store.updates[t][a];void 0!==f&&f(n,s||{},e.store,e)}}},U=function(e,t,r){if(Array.isArray(t)){for(var n=new Array(t.length),i=0,o=t.length;i<o;i++)n[i]=U(e,t[i],r);return n}if(null!==t){var a=e.store.keyOfEntity(t);null!==a?G(e,a,r,t):J(e,t.__typename,r,t)}},H=function(e,t,r){var n,i=e.store,o=e.variables,a="Query"===t;if(a)n=t;else{if(ee(t),"string"!=typeof(n=i.getField(t,"__typename")))return;i.removeRecord(j(t,T("__typename")))}for(var u,s=new M(n,t,r,e);void 0!==(u=s.next());){var c=k(u),l=R(u,o),f=j(t,T(c,l));if(a&&ee(f),void 0===u.selectionSet)i.removeRecord(f);else{var v=w(u),d=i.getLink(f);if(i.removeLink(f),void 0===d)void 0!==i.getRecord(f)&&i.removeRecord(f);else if(Array.isArray(d))for(var y=0,p=d.length;y<p;y++){var h=d[y];null!==h&&H(e,h,v)}else null!==d&&H(e,d,v)}}},W={current:null},X={current:null},Y=function(e){W.current=new Set,X.current=e},Z=function(){W.current=null,X.current=null},$=function(){return W.current},ee=function(e){W.current.add(e)},te=function(e,t,r){return c(e,t,r,X.current||0)},re=function(e,t){var r=X.current||0;return r?c(e,t,void 0,r):l(e,t)},ne=function(e,t,r,n,i){var u;if(this.records=o(a()),this.links=o(a()),this.resolvers=t||{},this.optimisticMutations=n||{},this.keys=i||{},this.schemaPredicates=e,this.updates={Mutation:r&&r.Mutation||{},Subscription:r&&r.Subscription||{}},e){var s=e.schema,c=s.getQueryType(),l=s.getMutationType(),f=s.getSubscriptionType(),v=c?c.name:"Query",d=l?l.name:"Mutation",y=f?f.name:"Subscription";this.rootFields={query:v,mutation:d,subscription:y},this.rootNames=((u={})[v]="query",u[d]="mutation",u[y]="subscription",u)}else this.rootFields={query:"Query",mutation:"Mutation",subscription:"Subscription"},this.rootNames={Query:"query",Mutation:"mutation",Subscription:"subscription"}};ne.prototype.getRootKey=function(e){return this.rootFields[e]},ne.prototype.keyOfEntity=function(e){var t,r=e.__typename,n=e.id,i=e._id;if(!r)return null;if(this.rootNames[r])return this.rootNames[r];if(this.keys[r])t=""+this.keys[r](e);else if(null!=n)t=""+n;else{if(null==i)return null;t=""+i}return t?r+":"+t:null},ne.prototype.clearOptimistic=function(e){this.records=u(this.records,e),this.links=u(this.links,e)},ne.prototype.getRecord=function(e){return s(this.records,e)},ne.prototype.removeRecord=function(e){return this.records=re(this.records,e)},ne.prototype.writeRecord=function(e,t){return this.records=te(this.records,t,e)},ne.prototype.getField=function(e,t,r){var n=j(e,T(t,r));return this.getRecord(n)},ne.prototype.writeField=function(e,t,r,n){var i=j(t,T(r,n));return this.records=te(this.records,i,e)},ne.prototype.getLink=function(e){return s(this.links,e)},ne.prototype.removeLink=function(e){return this.links=re(this.links,e)},ne.prototype.writeLink=function(e,t){return this.links=te(this.links,t,e)},ne.prototype.resolveValueOrLink=function(e){var t=this.getRecord(e);return void 0!==t?t:this.getLink(e)||null},ne.prototype.resolve=function(e,t,r){if("string"==typeof e)return ee(e),this.resolveValueOrLink(j(e,T(t,r)));var n=this.keyOfEntity(e);return null===n?null:(ee(n),this.resolveValueOrLink(j(n,T(t,r))))},ne.prototype.invalidateQuery=function(e,t){!function(e,t){Y(0);var r=N(t.query),n={variables:A(r,t.variables),fragments:F(t.query),store:e,schemaPredicates:e.schemaPredicates};H(n,n.store.getRootKey("query"),w(r)),Z()}(this,{query:e,variables:t})},ne.prototype.hasField=function(e){return void 0!==this.getRecord(e)||void 0!==this.getLink(e)},ne.prototype.updateQuery=function(e,t){var r=t(oe(this,e).data);null!==r&&D(this,e,r)},ne.prototype.writeFragment=function(e,t){B(this,e,t)};var ie=function(e,t,r){Y(0);var n=oe(e,t,r);return Z(),n},oe=function(e,t,r){var n=N(t.query),i=e.getRootKey(n.operation),o=w(n),a={variables:A(n,t.variables),fragments:F(t.query),partial:!1,store:e,schemaPredicates:e.schemaPredicates},u=r||Object.create(null);return u="Query"!==i?ae(a,i,o,u):se(a,i,o,u),{dependencies:$(),partial:void 0!==u&&a.partial,data:void 0===u?null:u}},ae=function(e,t,r,n){if("string"!=typeof n.__typename)return n;var i=Object.create(null);i.__typename=n.__typename;for(var o,a=new M(t,t,r,e);void 0!==(o=a.next());){var u=b(o),s=n[u];i[u]=void 0===o.selectionSet||null===s||Q(s)?s:ue(e,w(o),s)}return i},ue=function(e,t,r){if(Array.isArray(r)){for(var n=new Array(r.length),i=0,o=r.length;i<o;i++)n[i]=ue(e,t,r[i]);return n}if(null===r)return null;var a=e.store.keyOfEntity(r);if(null!==a){var u=Object.create(null),s=se(e,a,t,u);return void 0===s?null:s}return ae(e,r.__typename,t,r)},se=function(e,t,r,n){var i=e.store,o=e.variables,a=e.schemaPredicates,u=t===i.getRootKey("query");u||ee(t);var s=u?t:i.getField(t,"__typename");if("string"==typeof s){n.__typename=s;for(var c,l=new M(s,t,r,e),f=!1,v=!1;void 0!==(c=l.next());){var d=k(c),y=R(c,o),p=b(c),h=j(t,T(d,y)),m=i.getRecord(h);u&&ee(h);var g=void 0,O=i.resolvers[s];if(void 0!==O&&"function"==typeof O[d]){void 0!==m&&(n[p]=m);var _=O[d](n,y||{},i,e);void 0!==c.selectionSet&&(_=ce(e,_,s,d,h,w(c),n[p]));var x=null==_;g=x&&void 0!==a?void 0:x?null:_}else if(void 0===c.selectionSet)g=m;else{var S=w(c),A=i.getLink(h);void 0!==A?g=le(e,A,s,d,S,n[p]):"object"==typeof m&&null!==m&&(g=m)}if(void 0===g&&void 0!==a&&a.isFieldNullable(s,d))v=!0,n[p]=null;else{if(void 0===g)return;f=!0,n[p]=g}}return v&&(e.partial=!0),u&&v&&!f?void 0:n}},ce=function(e,t,r,n,i,o,a){if(Array.isArray(t)){for(var u=e.schemaPredicates,s=void 0!==u&&u.isListNullable(r,n),c=new Array(t.length),l=0,f=t.length;l<f;l++){var v=void 0!==a?a[l]:void 0,d=j(i,""+l),y=ce(e,t[l],r,n,d,o,v);if(void 0===y&&!s)return;t[l]=void 0!==y?y:null}return c}if(null===t)return null;if(fe(t)){var p=void 0===a?Object.create(null):a,h=("string"==typeof t?t:e.store.keyOfEntity(t))||i;return se(e,h,o,p)}},le=function(e,t,r,n,i,o){if(Array.isArray(t)){for(var a=e.schemaPredicates,u=void 0!==a&&a.isListNullable(r,n),s=new Array(t.length),c=0,l=t.length;c<l;c++){var f=le(e,t[c],r,n,i,void 0!==o?o[c]:void 0);if(void 0===f&&!u)return;s[c]=void 0!==f?f:null}return s}if(null===t)return null;var v=void 0===o?Object.create(null):o;return se(e,t,i,v)},fe=function(e){return"string"==typeof e||"object"==typeof e&&null!==e&&"string"==typeof e.__typename},ve=function(e){this.schema=t(e)};ve.prototype.isFieldNullable=function(e,t){var n=de(this.schema,e,t);return void 0!==n&&r(n.type)},ve.prototype.isListNullable=function(e,t){var o=de(this.schema,e,t);if(void 0===o)return!1;var a=n(o.type)?o.type.ofType:o.type;return i(a)&&r(a.ofType)},ve.prototype.isInterfaceOfType=function(e,t){if(!t||!e)return!1;if(t===e)return!0;var r=this.schema.getType(e),n=this.schema.getType(t);return this.schema.isPossibleType(r,n)};var de=function(e,t,r){var n=e.getType(t);if(void 0!==n){var i=n.getFields()[r];if(void 0!==i)return i}},ye=function(e,t){return I({},e,{context:I({},e.context,{meta:I({},e.context.meta,{cacheOutcome:t})})})},pe=function(e){return I({},e,{query:v(e.query)})},he=function(e){return e.context.requestPolicy},me=function(e){return"query"===e.operationName},ge=function(e){var t=he(e);return me(e)&&"network-only"!==t},ke=function(e,t){return I({},e,{context:I({},e.context,{requestPolicy:t})})};function be(e){return ge(e)}function we(e){return"miss"===e.outcome}function Oe(e){return ye(e.operation,e.outcome)}function _e(e){return"miss"!==e.outcome}function xe(e){return!ge(e)}var Se=function(e){return function(t){var r=t.forward,n=t.client;e||(e={});var i=new ne(e.schema?new ve(e.schema):void 0,e.resolvers,e.updates,e.optimistic,e.keys),o=new Set,a=new Map,u=Object.create(null),s=function(e,t){var r=new Set;function i(e){return r.add(e)}t.forEach(function(e){var t=u[e];void 0!==t&&(u[e]=[],t.forEach(i))}),r.forEach(function(t){if(t!==e.key){var r=a.get(t);a.delete(t),n.reexecuteOperation(r)}})},c=function(e){if(a=he(n=e),function(e){return"mutation"===e.operationName}(n)&&"network-only"!==a){var t=e.key,r=C(i,e,t).dependencies;0!==r.size&&(o.add(t),s(e,r))}var n,a},l=function(e,t){t.forEach(function(t){(u[t]||(u[t]=[])).push(e.key),a.has(e.key)||a.set(e.key,"network-only"===e.context.requestPolicy?ke(e,"cache-and-network"):e)})},f=function(e){var t,r=he(e),n=ie(i,e),o=n.data,a=n.partial;return null===o?t="miss":(l(e,n.dependencies),t=a&&"cache-only"!==r?"partial":"hit"),{outcome:t,operation:e,data:o}},v=function(e){var t,r,n=e.operation,a=e.error,u=e.extensions,c=me(n),f=e.data,v=n.key;if(o.has(v)&&(o.delete(v),i.clearOptimistic(v)),null!=f)if(t=V(i,n,f).dependencies,c){var d=ie(i,n);f=d.data,r=d.dependencies}else f=ie(i,n,f).data;return void 0!==t&&s(e.operation,t),c&&void 0!==r&&l(e.operation,r),{data:f,error:a,extensions:u,operation:n}};function k(e){var t=e.operation,r=e.outcome,i=he(t);if("cache-and-network"===i||"cache-first"===i&&"partial"===r){var o=ke(t,"network-only");n.reexecuteOperation(o)}return{operation:ye(t,r),data:e.data,error:e.error,extensions:e.extensions}}return function(e){var t=d(e,y(pe),p(c),h),n=d(t,m(be),y(f),h),i=d(n,m(we),y(Oe)),o=d(n,m(_e),y(k)),a=d(r(g([d(t,m(xe)),i])),y(v));return g([a,o])}}};export{ne as Store,Se as cacheExchange,ie as query,oe as read,V as write,B as writeFragment,C as writeOptimistic};
//# sourceMappingURL=urql-exchange-graphcache.es.min.js.map

@@ -23,15 +23,2 @@ "use strict";

var getOperationName = function(node) {
switch (node.operation) {
case "query":
return "Query";
case "mutation":
return "Mutation";
case "subscription":
return "Subscription";
}
};
var getFieldAlias = function(node) {

@@ -45,2 +32,7 @@ return void 0 !== node.alias ? node.alias.value : getName(node);

var getTypeCondition = function(ref) {
var typeCondition = ref.typeCondition;
return void 0 !== typeCondition ? getName(typeCondition) : null;
};
var isFieldNode = function(node) {

@@ -191,15 +183,2 @@ return node.kind === graphql.Kind.FIELD;

var keyOfEntity = function(data) {
var typeName = data.__typename;
var id = void 0 === data.id ? data._id : data.id;
if (null == typeName) {
return null;
} else if ("Query" === typeName || "Mutation" === typeName || "Subscription" === typeName) {
return typeName;
} else if (null == id) {
return null;
}
return typeName + ":" + id;
};
var keyOfField = function(fieldName, args) {

@@ -227,9 +206,11 @@ return args ? fieldName + "(" + stringify(args) + ")" : fieldName;

var isFragmentMatching = function(node, typename, entityKey, ctx) {
var isFragmentHeuristicallyMatching = function(node, typename, entityKey, ctx) {
if (!typename) {
return !1;
} else if (typename === (void 0 !== (typeCondition = node.typeCondition) ? getName(typeCondition) : null)) {
}
var typeCondition = getTypeCondition(node);
if (typename === typeCondition) {
return !0;
}
var typeCondition;
"production" !== process.env.NODE_ENV && warning(!1, "Heuristic Fragment Matching: A fragment is trying to match against the `%s` type, but the type condition is `%s`. Since GraphQL allows for interfaces `%s` may be aninterface.\nA schema needs to be defined for this match to be deterministic, otherwise the fragment will be matched heuristically!", typename, typeCondition, typeCondition);
return !getSelectionSet(node).some(function(node) {

@@ -268,5 +249,7 @@ if (!isFieldNode(node)) {

var fragmentNode = !isInlineFragment(node) ? this.context.fragments[getName(node)] : node;
if (void 0 !== fragmentNode && isFragmentMatching(fragmentNode, this.typename, this.entityKey, this.context)) {
this.indexStack.push(0);
this.selectionStack.push(getSelectionSet(fragmentNode));
if (void 0 !== fragmentNode) {
if (void 0 !== this.context.schemaPredicates ? this.context.schemaPredicates.isInterfaceOfType(getTypeCondition(fragmentNode), this.typename) : isFragmentHeuristicallyMatching(fragmentNode, this.typename, this.entityKey, this.context)) {
this.indexStack.push(0);
this.selectionStack.push(getSelectionSet(fragmentNode));
}
}

@@ -307,7 +290,8 @@ continue;

result: result,
store: store
store: store,
schemaPredicates: store.schemaPredicates
};
var select = getSelectionSet(operation);
var operationName = getOperationName(operation);
if ("Query" === operationName) {
var operationName = ctx.store.getRootKey(operation.operation);
if (operationName === ctx.store.getRootKey("query")) {
writeSelection(ctx, operationName, select, data);

@@ -330,6 +314,7 @@ } else {

result: result,
store: store
store: store,
schemaPredicates: store.schemaPredicates
};
var operationName = getOperationName(operation);
if ("Mutation" === operationName) {
var operationName = ctx.store.getRootKey(operation.operation);
if (operationName === ctx.store.getRootKey("mutation")) {
var select = getSelectionSet(operation);

@@ -378,3 +363,4 @@ var iter = new SelectionIterator(operationName, operationName, select, ctx);

},
store: store
store: store,
schemaPredicates: store.schemaPredicates
};

@@ -387,3 +373,3 @@ writeSelection(ctx, entityKey, select, writeData);

var variables = ctx.variables;
var isQuery = "Query" === entityKey;
var isQuery = entityKey === ctx.store.getRootKey("query");
var typename = data.__typename;

@@ -450,3 +436,3 @@ if (!isQuery) {

}
if ("Mutation" === typename || "Subscription" === typename) {
if (typename === ctx.store.getRootKey("mutation") || typename === ctx.store.getRootKey("subscription")) {
var updater = ctx.store.updates[typename][fieldName];

@@ -564,6 +550,10 @@ if (void 0 !== updater) {

var Store = function Store(resolvers, updates, optimisticMutations, keys) {
var Store = function Store(schemaPredicates, resolvers, updates, optimisticMutations, keys) {
var obj;
this.records = Pessimism.asMutable(Pessimism.make());
this.links = Pessimism.asMutable(Pessimism.make());
this.resolvers = resolvers || {};
this.optimisticMutations = optimisticMutations || {};
this.keys = keys || {};
this.schemaPredicates = schemaPredicates;
this.updates = {

@@ -573,13 +563,55 @@ Mutation: updates && updates.Mutation || {},

};
this.optimisticMutations = optimisticMutations || {};
this.keys = keys || {};
if (schemaPredicates) {
var schema = schemaPredicates.schema;
var queryType = schema.getQueryType();
var mutationType = schema.getMutationType();
var subscriptionType = schema.getSubscriptionType();
var queryName = queryType ? queryType.name : "Query";
var mutationName = mutationType ? mutationType.name : "Mutation";
var subscriptionName = subscriptionType ? subscriptionType.name : "Subscription";
this.rootFields = {
query: queryName,
mutation: mutationName,
subscription: subscriptionName
};
this.rootNames = ((obj = {})[queryName] = "query", obj[mutationName] = "mutation",
obj[subscriptionName] = "subscription", obj);
} else {
this.rootFields = {
query: "Query",
mutation: "Mutation",
subscription: "Subscription"
};
this.rootNames = {
Query: "query",
Mutation: "mutation",
Subscription: "subscription"
};
}
};
Store.prototype.keyOfEntity = function keyOfEntity$1(data) {
Store.prototype.getRootKey = function getRootKey(name) {
return this.rootFields[name];
};
Store.prototype.keyOfEntity = function keyOfEntity(data) {
var typename = data.__typename;
if (void 0 !== typename && void 0 !== this.keys[typename]) {
return this.keys[typename](data);
var id = data.id;
var _id = data._id;
if (!typename) {
return null;
} else if (this.rootNames[typename]) {
return this.rootNames[typename];
}
var key;
if (this.keys[typename]) {
key = "" + this.keys[typename](data);
} else if (null != id) {
key = "" + id;
} else if (null != _id) {
key = "" + _id;
} else {
return keyOfEntity(data);
return null;
}
return key ? typename + ":" + key : null;
};

@@ -656,5 +688,6 @@

fragments: getFragments(request.query),
store: store
store: store,
schemaPredicates: store.schemaPredicates
};
invalidateSelection(ctx, "Query", getSelectionSet(operation));
invalidateSelection(ctx, ctx.store.getRootKey("query"), getSelectionSet(operation));
clearStoreState();

@@ -672,4 +705,3 @@ }(this, {

Store.prototype.updateQuery = function updateQuery(ctx, updater) {
var ref = startQuery(this, ctx);
var output = updater("EMPTY" === ref.completeness ? null : ref.data);
var output = updater(read(this, ctx).data);
if (null !== output) {

@@ -684,5 +716,5 @@ startWrite(this, ctx, output);

var query = function(store, request) {
var query = function(store, request, data) {
initStoreState(0);
var result = startQuery(store, request);
var result = read(store, request, data);
clearStoreState();

@@ -692,37 +724,20 @@ return result;

var startQuery = function(store, request) {
var read = function(store, request, input) {
var operation = getMainOperation(request.query);
var root = Object.create(null);
var result = {
completeness: "FULL",
dependencies: getCurrentDependencies(),
data: root
};
var rootKey = store.getRootKey(operation.operation);
var rootSelect = getSelectionSet(operation);
var ctx = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result: result,
store: store
partial: !1,
store: store,
schemaPredicates: store.schemaPredicates
};
result.data = readSelection(ctx, "Query", getSelectionSet(operation), root);
return result;
};
var readOperation = function(store, request, data) {
initStoreState(0);
var operation = getMainOperation(request.query);
var result = {
completeness: "FULL",
var data = input || Object.create(null);
data = "Query" !== rootKey ? readRoot(ctx, rootKey, rootSelect, data) : readSelection(ctx, rootKey, rootSelect, data);
return {
dependencies: getCurrentDependencies(),
data: null
partial: void 0 === data ? !1 : ctx.partial,
data: void 0 === data ? null : data
};
var ctx = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result: result,
store: store
};
result.data = readRoot(ctx, getOperationName(operation), getSelectionSet(operation), data);
clearStoreState();
return result;
};

@@ -762,4 +777,5 @@

if (null !== entityKey) {
var data = Object.create(null);
return readSelection(ctx, entityKey, select, data);
var newData$1 = Object.create(null);
var fieldValue = readSelection(ctx, entityKey, select, newData$1);
return void 0 === fieldValue ? null : fieldValue;
} else {

@@ -771,12 +787,12 @@ return readRoot(ctx, originalData.__typename, select, originalData);

var readSelection = function(ctx, entityKey, select, data) {
var isQuery = "Query" === entityKey;
var store = ctx.store;
var variables = ctx.variables;
var schemaPredicates = ctx.schemaPredicates;
var isQuery = entityKey === store.getRootKey("query");
if (!isQuery) {
addDependency(entityKey);
}
var store = ctx.store;
var variables = ctx.variables;
var typename = isQuery ? "Query" : store.getField(entityKey, "__typename");
var typename = isQuery ? entityKey : store.getField(entityKey, "__typename");
if ("string" != typeof typename) {
ctx.result.completeness = "EMPTY";
return null;
return;
}

@@ -786,2 +802,4 @@ data.__typename = typename;

var node;
var hasFields = !1;
var hasPartials = !1;
while (void 0 !== (node = iter.next())) {

@@ -796,4 +814,5 @@ var fieldName = getName(node);

}
var dataFieldValue = void 0;
var resolvers = store.resolvers[typename];
if (void 0 !== resolvers && resolvers.hasOwnProperty(fieldName)) {
if (void 0 !== resolvers && "function" == typeof resolvers[fieldName]) {
if (void 0 !== fieldValue) {

@@ -803,65 +822,77 @@ data[fieldAlias] = fieldValue;

var resolverValue = resolvers[fieldName](data, fieldArgs || {}, store, ctx);
if (void 0 === node.selectionSet) {
data[fieldAlias] = void 0 !== resolverValue ? resolverValue : null;
if (void 0 !== node.selectionSet) {
resolverValue = resolveResolverResult(ctx, resolverValue, typename, fieldName, fieldKey, getSelectionSet(node), data[fieldAlias]);
}
var isNull = null == resolverValue;
if (isNull && void 0 !== schemaPredicates) {
dataFieldValue = void 0;
} else {
var fieldSelect = getSelectionSet(node);
data[fieldAlias] = resolveResolverResult(ctx, resolverValue, fieldKey, fieldSelect, data[fieldAlias]);
dataFieldValue = isNull ? null : resolverValue;
}
} else if (void 0 === node.selectionSet) {
if (void 0 === fieldValue) {
ctx.result.completeness = "EMPTY";
data[fieldAlias] = null;
} else {
data[fieldAlias] = fieldValue;
}
dataFieldValue = fieldValue;
} else {
var fieldSelect$1 = getSelectionSet(node);
var fieldSelect = getSelectionSet(node);
var link = store.getLink(fieldKey);
if (void 0 === link) {
if ("object" == typeof fieldValue && null !== fieldValue) {
data[fieldAlias] = fieldValue;
} else {
ctx.result.completeness = "EMPTY";
data[fieldAlias] = null;
}
} else {
data[fieldAlias] = resolveLink(ctx, link, fieldSelect$1, data[fieldAlias]);
if (void 0 !== link) {
dataFieldValue = resolveLink(ctx, link, typename, fieldName, fieldSelect, data[fieldAlias]);
} else if ("object" == typeof fieldValue && null !== fieldValue) {
dataFieldValue = fieldValue;
}
}
if (void 0 === dataFieldValue && void 0 !== schemaPredicates && schemaPredicates.isFieldNullable(typename, fieldName)) {
hasPartials = !0;
data[fieldAlias] = null;
} else if (void 0 === dataFieldValue) {
return;
} else {
hasFields = !0;
data[fieldAlias] = dataFieldValue;
}
}
return data;
if (hasPartials) {
ctx.partial = !0;
}
return isQuery && hasPartials && !hasFields ? void 0 : data;
};
var resolveResolverResult = function(ctx, result, key, select, prevData) {
var resolveResolverResult = function(ctx, result, typename, fieldName, key, select, prevData) {
if (Array.isArray(result)) {
return result.map(function _ref(childResult, index) {
var data = void 0 !== prevData ? prevData[index] : void 0;
var indexKey = joinKeys(key, "" + index);
return resolveResolverResult(ctx, childResult, indexKey, select, data);
});
var schemaPredicates = ctx.schemaPredicates;
var isListNullable = void 0 !== schemaPredicates && schemaPredicates.isListNullable(typename, fieldName);
var newResult = new Array(result.length);
for (var i = 0, l = result.length; i < l; i++) {
var data = void 0 !== prevData ? prevData[i] : void 0;
var childKey = joinKeys(key, "" + i);
var childResult = resolveResolverResult(ctx, result[i], typename, fieldName, childKey, select, data);
if (void 0 === childResult && !isListNullable) {
return;
} else {
result[i] = void 0 !== childResult ? childResult : null;
}
}
return newResult;
} else if (null === result) {
return null;
} else if (isDataOrKey(result)) {
var data = void 0 === prevData ? Object.create(null) : prevData;
var childKey = ("string" == typeof result ? result : ctx.store.keyOfEntity(result)) || key;
var selectionResult = readSelection(ctx, childKey, select, data);
if (null !== selectionResult && "object" == typeof result) {
for (key in result) {
if ("__typename" !== key && result.hasOwnProperty(key)) {
selectionResult[key] = result[key];
}
}
}
return selectionResult;
var data$1 = void 0 === prevData ? Object.create(null) : prevData;
var childKey$1 = ("string" == typeof result ? result : ctx.store.keyOfEntity(result)) || key;
return readSelection(ctx, childKey$1, select, data$1);
}
"production" !== process.env.NODE_ENV && warning(!1, "Invalid resolver value: The resolver at `%s` returned a scalar (number, boolean, etc), but the GraphQL query expects a selection set for this field.\nIf necessary, use Cache.resolve() to resolve a link or entity from the cache.", key);
ctx.result.completeness = "EMPTY";
return null;
return;
};
var resolveLink = function(ctx, link, select, prevData) {
var resolveLink = function(ctx, link, typename, fieldName, select, prevData) {
if (Array.isArray(link)) {
var schemaPredicates = ctx.schemaPredicates;
var isListNullable = void 0 !== schemaPredicates && schemaPredicates.isListNullable(typename, fieldName);
var newLink = new Array(link.length);
for (var i = 0, l = link.length; i < l; i++) {
newLink[i] = resolveLink(ctx, link[i], select, void 0 !== prevData ? prevData[i] : void 0);
var childLink = resolveLink(ctx, link[i], typename, fieldName, select, void 0 !== prevData ? prevData[i] : void 0);
if (void 0 === childLink && !isListNullable) {
return;
} else {
newLink[i] = void 0 !== childLink ? childLink : null;
}
}

@@ -872,4 +903,4 @@ return newLink;

} else {
var data$1 = void 0 === prevData ? Object.create(null) : prevData;
return readSelection(ctx, link, select, data$1);
var data = void 0 === prevData ? Object.create(null) : prevData;
return readSelection(ctx, link, select, data);
}

@@ -882,17 +913,69 @@ };

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 SchemaPredicates = function SchemaPredicates(schema) {
this.schema = graphql.buildClientSchema(schema);
};
SchemaPredicates.prototype.isFieldNullable = function isFieldNullable(typename, fieldName) {
var field = getField(this.schema, typename, fieldName);
if (void 0 === field) {
return !1;
}
return graphql.isNullableType(field.type);
};
SchemaPredicates.prototype.isListNullable = function isListNullable(typename, fieldName) {
var field = getField(this.schema, typename, fieldName);
if (void 0 === field) {
return !1;
}
var ofType = graphql.isNonNullType(field.type) ? field.type.ofType : field.type;
return graphql.isListType(ofType) && graphql.isNullableType(ofType.ofType);
};
SchemaPredicates.prototype.isInterfaceOfType = function isInterfaceOfType(typeCondition, typename) {
if (!typename || !typeCondition) {
return !1;
}
if (typename === typeCondition) {
return !0;
}
var abstractType = this.schema.getType(typeCondition);
expectAbstractType(abstractType, typeCondition);
var objectType = this.schema.getType(typename);
expectObjectType(objectType, typename);
return this.schema.isPossibleType(abstractType, objectType);
};
var getField = function(schema, typename, fieldName) {
var type = schema.getType(typename);
expectObjectType(type, typename);
var object = type;
if (void 0 === object) {
"production" !== process.env.NODE_ENV && warning(!1, "Invalid type: The type `%s` is not a type in the defined schema, but the GraphQL document expects it to exist.\nTraversal will continue, however this may lead to undefined behavior!", typename);
return;
}
var field = object.getFields()[fieldName];
if (void 0 === field) {
"production" !== process.env.NODE_ENV && warning(!1, "Invalid field: The field `%s` does not exist on `%s`, but the GraphQL document expects it to exist.\nTraversal will continue, however this may lead to undefined behavior!", fieldName, typename);
return;
}
return field;
};
var expectObjectType = function(type, typename) {
"production" !== process.env.NODE_ENV && browser(type instanceof graphql.GraphQLObjectType, "Invalid type: The type `%s` is not an object in the defined schema, but the GraphQL document is traversing it.", typename);
};
var expectAbstractType = function(type, typename) {
"production" !== process.env.NODE_ENV && browser(type instanceof graphql.GraphQLInterfaceType || type instanceof graphql.GraphQLUnionType, "Invalid type: The type `%s` is not an Interface or Union type in the defined schema, but a fragment in the GraphQL document is using it as a type condition.", typename);
};
var addCacheOutcome = function(op, outcome) {
return _extends({}, op, {
context: _extends({}, op.context, {
meta: _extends({}, op.context.meta, {
cacheOutcome: outcome
})
};
};
})
});
};

@@ -932,11 +1015,11 @@

function _ref3(res) {
return "FULL" !== res.completeness;
return "miss" === res.outcome;
}
function _ref4(res) {
return res.operation;
return addCacheOutcome(res.operation, res.outcome);
}
function _ref5(res) {
return "FULL" === res.completeness;
return "miss" !== res.outcome;
}

@@ -957,3 +1040,3 @@

}
var store = new Store(opts.resolvers, opts.updates, opts.optimistic, opts.keys);
var store = new Store(opts.schema ? new SchemaPredicates(opts.schema) : void 0, opts.resolvers, opts.updates, opts.optimistic, opts.keys);
var optimisticKeys = new Set();

@@ -1005,11 +1088,16 @@ var ops = new Map();

var policy = getRequestPolicy(operation);
var res = query(store, operation);
var isComplete = "cache-only" === policy || "FULL" === res.completeness;
if (isComplete) {
updateDependencies(operation, res.dependencies);
var ref = query(store, operation);
var data = ref.data;
var partial = ref.partial;
var cacheOutcome;
if (null === data) {
cacheOutcome = "miss";
} else {
updateDependencies(operation, ref.dependencies);
cacheOutcome = !partial || "cache-only" === policy ? "hit" : "partial";
}
return {
outcome: cacheOutcome,
operation: operation,
completeness: isComplete ? "FULL" : "EMPTY",
data: res.data
data: data
};

@@ -1036,3 +1124,3 @@ };

} else {
data = readOperation(store, operation, data).data;
data = query(store, operation, data).data;
}

@@ -1053,8 +1141,16 @@ }

};
function _ref6(ref) {
var operation = ref.operation;
if ("cache-and-network" === getRequestPolicy(operation)) {
function _ref6(res) {
var operation = res.operation;
var outcome = res.outcome;
var policy = getRequestPolicy(operation);
if ("cache-and-network" === policy || "cache-first" === policy && "partial" === outcome) {
var networkOnly = toRequestPolicy(operation, "network-only");
client.reexecuteOperation(networkOnly);
}
return {
operation: addCacheOutcome(operation, outcome),
data: res.data,
error: res.error,
extensions: res.extensions
};
}

@@ -1065,4 +1161,4 @@ return function(ops$) {

var cacheOps$ = wonka.pipe(cache$, wonka.filter(_ref3), wonka.map(_ref4));
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.map(updateCacheWithResult), wonka.map(addCacheOutcome("miss")));
var cacheResult$ = wonka.pipe(cache$, wonka.filter(_ref5), wonka.map(_ref6));
var result$ = wonka.pipe(forward(wonka.merge([ wonka.pipe(sharedOps$, wonka.filter(_ref7)), cacheOps$ ])), wonka.map(updateCacheWithResult));
return wonka.merge([ result$, cacheResult$ ]);

@@ -1073,7 +1169,5 @@ };

exports.defaultKeyOfEntity = keyOfEntity;
exports.query = query;
exports.readOperation = readOperation;
exports.read = read;

@@ -1080,0 +1174,0 @@ exports.write = write;

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),require("warning");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"),a=function(e){return e.name.value},u=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:a(e)},l=function(e){return void 0!==e.selectionSet?e.selectionSet.selections:[]},c=function(e){return e.kind===t.Kind.FIELD},f=function(e){return e.kind===t.Kind.INLINE_FRAGMENT},v=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:for(var n=new Array(e.values.length),i=0,o=e.values.length;i<o;i++)n[i]=v(e.values[i],r);return n;case t.Kind.OBJECT:for(var u=Object.create(null),s=0,l=e.fields.length;s<l;s++){var c=e.fields[s];u[a(c)]=v(c.value,r)}return u;case t.Kind.VARIABLE:var f=r[a(e)];return void 0!==f?f:null;default:return e.value}},p=function(e,t){if(void 0===e.arguments||0===e.arguments.length)return null;for(var r=Object.create(null),n=0,i=e.arguments.length;n<i;n++){var o=e.arguments[n];r[a(o)]=v(o.value,t)}return r},d=function(e,t){if(void 0===e.variableDefinitions)return{};var r=t||{};return e.variableDefinitions.reduce(function(e,t){var n=a(t.variable),i=r[n];if(void 0===i){if(void 0===t.defaultValue)return e;i=v(t.defaultValue,r)}return e[n]=i,e},Object.create(null))},y=function(e){return e.kind===t.Kind.FRAGMENT_DEFINITION};function h(e){return e.kind===t.Kind.OPERATION_DEFINITION}var m=function(e){return e.definitions.find(h)};function g(e,t){return e[a(t)]=t,e}var k=function(e){return e.definitions.filter(y).reduce(g,{})},b=function(e,t){if(void 0===e.directives)return!0;for(var r=e.directives,n=0,i=r.length;n<i;n++){var o=r[n],u=a(o),s="include"===u;if(s||"skip"===u){var l=o.arguments?o.arguments[0]:null;if(l&&"if"===a(l)){var c=v(l.value,t);if("boolean"==typeof c||null===c)return s?!!c:!c}}}return!0},O=function(e){var t=e.__typename,r=void 0===e.id?e._id:e.id;return null==t?null:"Query"===t||"Mutation"===t||"Subscription"===t?t:null==r?null:t+":"+r},_=function(e,t){return t?e+"("+n(t)+")":e},w=function(e,t){return e+"."+t};function x(){return(x=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 L=function(e,t,r,n){return!(!t||t!==(void 0!==(i=e.typeCondition)?a(i):null)&&l(e).some(function(e){if(!c(e))return!1;var t=a(e),i=p(e,n.variables),o=_(t,i);return!n.store.hasField(w(r,o))}));var i},S=function(e,t,r,n){this.typename=e,this.entityKey=t,this.context=n,this.indexStack=[0],this.selectionStack=[r]};S.prototype.next=function(){for(;0!==this.indexStack.length;){var e=this.indexStack[this.indexStack.length-1]++,t=this.selectionStack[this.selectionStack.length-1];if(e>=t.length)this.indexStack.pop(),this.selectionStack.pop();else{var r=t[e];if(b(r,this.context.variables)){if(c(r)){if("__typename"===a(r))continue;return r}var n=f(r)?r:this.context.fragments[a(r)];void 0!==n&&L(n,this.typename,this.entityKey,this.context)&&(this.indexStack.push(0),this.selectionStack.push(l(n)))}}}};var E=function(e){return Array.isArray(e)?e.some(E):"object"!=typeof e||null!==e&&"string"!=typeof e.__typename},q=function(e,t,r){Q(0);var n=A(e,t,r);return U(),n},A=function(e,t,r){var n=m(t.query),i={dependencies:D()},o={variables:d(n,t.variables),fragments:k(t.query),result:i,store:e},a=l(n),s=u(n);return"Query"===s?j(o,s,a,r):T(o,s,a,r),i},F=function(e,t,r){Q(r);var n=m(t.query),i={dependencies:D()},o={variables:d(n,t.variables),fragments:k(t.query),result:i,store:e},s=u(n);if("Mutation"===s)for(var c,f=l(n),v=new S(s,s,f,o);void 0!==(c=v.next());)if(void 0!==c.selectionSet){var y=a(c),h=o.store.optimisticMutations[y];if(void 0!==h){var g=p(c,o.variables),b=l(c),O=h(g||{},o.store,o);E(O)||I(o,O,b)}}return U(),i},M=function(e,t,r){var n=k(t),i=n[Object.keys(n)[0]];if(void 0!==i){var o=l(i),a=x({},r,{__typename:i.typeCondition.name.value}),u=e.keyOfEntity(a);if(u){var s={variables:{},fragments:n,result:{dependencies:D()},store:e};j(s,u,o,a)}}},j=function(e,t,r,n){var i=e.store,o=e.variables,u="Query"===t,c=n.__typename;u||V(t),i.writeField(u?t:c,t,"__typename");for(var f,v=new S(c,t,r,e);void 0!==(f=v.next());){var d=a(f),y=p(f,o),h=w(t,_(d,y)),m=n[s(f)];if(u&&V(h),void 0===f.selectionSet)i.writeRecord(m,h);else if(E(m))i.writeRecord(m,h);else{var g=l(f),k=R(e,h,g,m);i.writeLink(k,h),i.removeRecord(h)}}},R=function(e,t,r,n){if(Array.isArray(n)){for(var i=new Array(n.length),o=0,a=n.length;o<a;o++){var u=n[o],s=w(t,""+o),l=R(e,s,r,u);i[o]=l}return i}if(null===n)return null;var c=e.store.keyOfEntity(n),f=null!==c?c:t;return j(e,f,r,n),f},T=function(e,t,r,n){for(var i,o=new S(t,t,r,e);void 0!==(i=o.next());){var u=a(i),c=s(i),f=p(i,e.variables),v=n[c];if(void 0!==i.selectionSet&&null!==v&&!E(v)){var d=l(i);I(e,v,d)}if("Mutation"===t||"Subscription"===t){var y=e.store.updates[t][u];void 0!==y&&y(n,f||{},e.store,e)}}},I=function(e,t,r){if(Array.isArray(t)){for(var n=new Array(t.length),i=0,o=t.length;i<o;i++)n[i]=I(e,t[i],r);return n}if(null!==t){var a=e.store.keyOfEntity(t);null!==a?j(e,a,r,t):T(e,t.__typename,r,t)}},P=function(e,t,r){var n,i=e.store,o=e.variables,u="Query"===t;if(u)n=t;else{if(V(t),"string"!=typeof(n=i.getField(t,"__typename")))return;i.removeRecord(w(t,_("__typename")))}for(var s,c=new S(n,t,r,e);void 0!==(s=c.next());){var f=a(s),v=p(s,o),d=w(t,_(f,v));if(u&&V(d),void 0===s.selectionSet)i.removeRecord(d);else{var y=l(s),h=i.getLink(d);if(i.removeLink(d),void 0===h)void 0!==i.getRecord(d)&&i.removeRecord(d);else if(Array.isArray(h))for(var m=0,g=h.length;m<g;m++){var k=h[m];null!==k&&P(e,k,y)}else null!==h&&P(e,h,y)}}},K={current:null},N={current:null},Q=function(e){K.current=new Set,N.current=e},U=function(){K.current=null,N.current=null},D=function(){return K.current},V=function(e){K.current.add(e)},Y=function(e,t,n){return r.setOptimistic(e,t,n,N.current||0)},C=function(e,t){var n=N.current||0;return n?r.setOptimistic(e,t,void 0,n):r.remove(e,t)},B=function(e,t,n,i){this.records=r.asMutable(r.make()),this.links=r.asMutable(r.make()),this.resolvers=e||{},this.updates={Mutation:t&&t.Mutation||{},Subscription:t&&t.Subscription||{}},this.optimisticMutations=n||{},this.keys=i||{}};B.prototype.keyOfEntity=function(e){var t=e.__typename;return void 0!==t&&void 0!==this.keys[t]?this.keys[t](e):O(e)},B.prototype.clearOptimistic=function(e){this.records=r.clearOptimistic(this.records,e),this.links=r.clearOptimistic(this.links,e)},B.prototype.getRecord=function(e){return r.get(this.records,e)},B.prototype.removeRecord=function(e){return this.records=C(this.records,e)},B.prototype.writeRecord=function(e,t){return this.records=Y(this.records,t,e)},B.prototype.getField=function(e,t,r){var n=w(e,_(t,r));return this.getRecord(n)},B.prototype.writeField=function(e,t,r,n){var i=w(t,_(r,n));return this.records=Y(this.records,i,e)},B.prototype.getLink=function(e){return r.get(this.links,e)},B.prototype.removeLink=function(e){return this.links=C(this.links,e)},B.prototype.writeLink=function(e,t){return this.links=Y(this.links,t,e)},B.prototype.resolveValueOrLink=function(e){var t=this.getRecord(e);return void 0!==t?t:this.getLink(e)||null},B.prototype.resolve=function(e,t,r){if("string"==typeof e)return V(e),this.resolveValueOrLink(w(e,_(t,r)));var n=this.keyOfEntity(e);return null===n?null:(V(n),this.resolveValueOrLink(w(n,_(t,r))))},B.prototype.invalidateQuery=function(e,t){!function(e,t){Q(0);var r=m(t.query),n={variables:d(r,t.variables),fragments:k(t.query),store:e};P(n,"Query",l(r)),U()}(this,{query:e,variables:t})},B.prototype.hasField=function(e){return void 0!==this.getRecord(e)||void 0!==this.getLink(e)},B.prototype.updateQuery=function(e,t){var r=z(this,e),n=t("EMPTY"===r.completeness?null:r.data);null!==n&&A(this,e,n)},B.prototype.writeFragment=function(e,t){M(this,e,t)};var G=function(e,t){Q(0);var r=z(e,t);return U(),r},z=function(e,t){var r=m(t.query),n=Object.create(null),i={completeness:"FULL",dependencies:D(),data:n},o={variables:d(r,t.variables),fragments:k(t.query),result:i,store:e};return i.data=X(o,"Query",l(r),n),i},J=function(e,t,r){Q(0);var n=m(t.query),i={completeness:"FULL",dependencies:D(),data:null},o={variables:d(n,t.variables),fragments:k(t.query),result:i,store:e};return i.data=H(o,u(n),l(n),r),U(),i},H=function(e,t,r,n){if("string"!=typeof n.__typename)return n;var i=Object.create(null);i.__typename=n.__typename;for(var o,a=new S(t,t,r,e);void 0!==(o=a.next());){var u=s(o),c=n[u];i[u]=void 0===o.selectionSet||null===c||E(c)?c:W(e,l(o),c)}return i},W=function(e,t,r){if(Array.isArray(r)){for(var n=new Array(r.length),i=0,o=r.length;i<o;i++)n[i]=W(e,t,r[i]);return n}if(null===r)return null;var a=e.store.keyOfEntity(r);if(null!==a){var u=Object.create(null);return X(e,a,t,u)}return H(e,r.__typename,t,r)},X=function(e,t,r,n){var i="Query"===t;i||V(t);var o=e.store,u=e.variables,c=i?"Query":o.getField(t,"__typename");if("string"!=typeof c)return e.result.completeness="EMPTY",null;n.__typename=c;for(var f,v=new S(c,t,r,e);void 0!==(f=v.next());){var d=a(f),y=p(f,u),h=s(f),m=w(t,_(d,y)),g=o.getRecord(m);i&&V(m);var k=o.resolvers[c];if(void 0!==k&&k.hasOwnProperty(d)){void 0!==g&&(n[h]=g);var b=k[d](n,y||{},o,e);if(void 0===f.selectionSet)n[h]=void 0!==b?b:null;else{var O=l(f);n[h]=Z(e,b,m,O,n[h])}}else if(void 0===f.selectionSet)void 0===g?(e.result.completeness="EMPTY",n[h]=null):n[h]=g;else{var x=l(f),L=o.getLink(m);void 0===L?"object"==typeof g&&null!==g?n[h]=g:(e.result.completeness="EMPTY",n[h]=null):n[h]=$(e,L,x,n[h])}}return n},Z=function(e,t,r,n,i){if(Array.isArray(t))return t.map(function(t,o){var a=void 0!==i?i[o]:void 0,u=w(r,""+o);return Z(e,t,u,n,a)});if(null===t)return null;if(ee(t)){var o=void 0===i?Object.create(null):i,a=("string"==typeof t?t:e.store.keyOfEntity(t))||r,u=X(e,a,n,o);if(null!==u&&"object"==typeof t)for(r in t)"__typename"!==r&&t.hasOwnProperty(r)&&(u[r]=t[r]);return u}return e.result.completeness="EMPTY",null},$=function(e,t,r,n){if(Array.isArray(t)){for(var i=new Array(t.length),o=0,a=t.length;o<a;o++)i[o]=$(e,t[o],r,void 0!==n?n[o]:void 0);return i}if(null===t)return null;var u=void 0===n?Object.create(null):n;return X(e,t,r,u)},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:x({},t.operation,{context:x({},t.operation.context,{meta:x({},t.operation.context.meta,{cacheOutcome:e})})})}}},re=function(e){return x({},e,{query:i.formatDocument(e.query)})},ne=function(e){return e.context.requestPolicy},ie=function(e){return"query"===e.operationName},oe=function(e){var t=ne(e);return ie(e)&&"network-only"!==t},ae=function(e,t){return x({},e,{context:x({},e.context,{requestPolicy:t})})};function ue(e){return oe(e)}function se(e){return"FULL"!==e.completeness}function le(e){return e.operation}function ce(e){return"FULL"===e.completeness}function fe(e){return!oe(e)}exports.Store=B,exports.cacheExchange=function(e){return function(t){var r=t.forward,n=t.client;e||(e={});var i=new B(e.resolvers,e.updates,e.optimistic,e.keys),a=new Set,u=new Map,s=Object.create(null),l=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=u.get(t);u.delete(t),n.reexecuteOperation(r)}})},c=function(e){if(o=ne(n=e),function(e){return"mutation"===e.operationName}(n)&&"network-only"!==o){var t=e.key,r=F(i,e,t).dependencies;0!==r.size&&(a.add(t),l(e,r))}var n,o},f=function(e,t){t.forEach(function(t){(s[t]||(s[t]=[])).push(e.key),u.has(e.key)||u.set(e.key,"network-only"===e.context.requestPolicy?ae(e,"cache-and-network"):e)})},v=function(e){var t=ne(e),r=G(i,e),n="cache-only"===t||"FULL"===r.completeness;return n&&f(e,r.dependencies),{operation:e,completeness:n?"FULL":"EMPTY",data:r.data}},p=function(e){var t,r,n=e.operation,o=e.error,u=e.extensions,s=ie(n),c=e.data,v=n.key;if(a.has(v)&&(a.delete(v),i.clearOptimistic(v)),null!=c)if(t=q(i,n,c).dependencies,s){var p=G(i,n);c=p.data,r=p.dependencies}else c=J(i,n,c).data;return void 0!==t&&l(e.operation,t),s&&void 0!==r&&f(e.operation,r),{data:c,error:o,extensions:u,operation:n}};function d(e){var t=e.operation;if("cache-and-network"===ne(t)){var r=ae(t,"network-only");n.reexecuteOperation(r)}}return function(e){var t=o.pipe(e,o.map(re),o.tap(c),o.share),n=o.pipe(t,o.filter(ue),o.map(v),o.share),i=o.pipe(n,o.filter(se),o.map(le)),a=o.pipe(n,o.filter(ce),o.tap(d),o.map(te("hit"))),u=o.pipe(r(o.merge([o.pipe(t,o.filter(fe)),i])),o.map(p),o.map(te("miss")));return o.merge([u,a])}}},exports.defaultKeyOfEntity=O,exports.query=G,exports.readOperation=J,exports.write=q,exports.writeFragment=M,exports.writeOptimistic=F;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),require("warning");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"),a=function(e){return e.name.value},u=function(e){return void 0!==e.alias?e.alias.value:a(e)},s=function(e){return void 0!==e.selectionSet?e.selectionSet.selections:[]},c=function(e){var t=e.typeCondition;return void 0!==t?a(t):null},l=function(e){return e.kind===t.Kind.FIELD},f=function(e){return e.kind===t.Kind.INLINE_FRAGMENT},v=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:for(var n=new Array(e.values.length),i=0,o=e.values.length;i<o;i++)n[i]=v(e.values[i],r);return n;case t.Kind.OBJECT:for(var u=Object.create(null),s=0,c=e.fields.length;s<c;s++){var l=e.fields[s];u[a(l)]=v(l.value,r)}return u;case t.Kind.VARIABLE:var f=r[a(e)];return void 0!==f?f:null;default:return e.value}},d=function(e,t){if(void 0===e.arguments||0===e.arguments.length)return null;for(var r=Object.create(null),n=0,i=e.arguments.length;n<i;n++){var o=e.arguments[n];r[a(o)]=v(o.value,t)}return r},p=function(e,t){if(void 0===e.variableDefinitions)return{};var r=t||{};return e.variableDefinitions.reduce(function(e,t){var n=a(t.variable),i=r[n];if(void 0===i){if(void 0===t.defaultValue)return e;i=v(t.defaultValue,r)}return e[n]=i,e},Object.create(null))},y=function(e){return e.kind===t.Kind.FRAGMENT_DEFINITION};function h(e){return e.kind===t.Kind.OPERATION_DEFINITION}var m=function(e){return e.definitions.find(h)};function g(e,t){return e[a(t)]=t,e}var k=function(e){return e.definitions.filter(y).reduce(g,{})},b=function(e,t){if(void 0===e.directives)return!0;for(var r=e.directives,n=0,i=r.length;n<i;n++){var o=r[n],u=a(o),s="include"===u;if(s||"skip"===u){var c=o.arguments?o.arguments[0]:null;if(c&&"if"===a(c)){var l=v(c.value,t);if("boolean"==typeof l||null===l)return s?!!l:!l}}}return!0},O=function(e,t){return t?e+"("+n(t)+")":e},x=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,t,r,n){return!(!t||t!==c(e)&&s(e).some(function(e){if(!l(e))return!1;var t=a(e),i=d(e,n.variables),o=O(t,i);return!n.store.hasField(x(r,o))}))},q=function(e,t,r,n){this.typename=e,this.entityKey=t,this.context=n,this.indexStack=[0],this.selectionStack=[r]};q.prototype.next=function(){for(;0!==this.indexStack.length;){var e=this.indexStack[this.indexStack.length-1]++,t=this.selectionStack[this.selectionStack.length-1];if(e>=t.length)this.indexStack.pop(),this.selectionStack.pop();else{var r=t[e];if(b(r,this.context.variables)){if(l(r)){if("__typename"===a(r))continue;return r}var n=f(r)?r:this.context.fragments[a(r)];void 0!==n&&(void 0!==this.context.schemaPredicates?this.context.schemaPredicates.isInterfaceOfType(c(n),this.typename):_(n,this.typename,this.entityKey,this.context))&&(this.indexStack.push(0),this.selectionStack.push(s(n)))}}}};var S=function(e){return Array.isArray(e)?e.some(S):"object"!=typeof e||null!==e&&"string"!=typeof e.__typename},R=function(e,t,r){M(0);var n=A(e,t,r);return Q(),n},A=function(e,t,r){var n=m(t.query),i={dependencies:D()},o={variables:p(n,t.variables),fragments:k(t.query),result:i,store:e,schemaPredicates:e.schemaPredicates},a=s(n),u=o.store.getRootKey(n.operation);return u===o.store.getRootKey("query")?K(o,u,a,r):P(o,u,a,r),i},N=function(e,t,r){M(r);var n=m(t.query),i={dependencies:D()},o={variables:p(n,t.variables),fragments:k(t.query),result:i,store:e,schemaPredicates:e.schemaPredicates},u=o.store.getRootKey(n.operation);if(u===o.store.getRootKey("mutation"))for(var c,l=s(n),f=new q(u,u,l,o);void 0!==(c=f.next());)if(void 0!==c.selectionSet){var v=a(c),y=o.store.optimisticMutations[v];if(void 0!==y){var h=d(c,o.variables),g=s(c),b=y(h||{},o.store,o);S(b)||F(o,b,g)}}return Q(),i},T=function(e,t,r){var n=k(t),i=n[Object.keys(n)[0]];if(void 0!==i){var o=s(i),a=w({},r,{__typename:i.typeCondition.name.value}),u=e.keyOfEntity(a);if(u){var c={variables:{},fragments:n,result:{dependencies:D()},store:e,schemaPredicates:e.schemaPredicates};K(c,u,o,a)}}},K=function(e,t,r,n){var i=e.store,o=e.variables,c=t===e.store.getRootKey("query"),l=n.__typename;c||V(t),i.writeField(c?t:l,t,"__typename");for(var f,v=new q(l,t,r,e);void 0!==(f=v.next());){var p=a(f),y=d(f,o),h=x(t,O(p,y)),m=n[u(f)];if(c&&V(h),void 0===f.selectionSet)i.writeRecord(m,h);else if(S(m))i.writeRecord(m,h);else{var g=s(f),k=L(e,h,g,m);i.writeLink(k,h),i.removeRecord(h)}}},L=function(e,t,r,n){if(Array.isArray(n)){for(var i=new Array(n.length),o=0,a=n.length;o<a;o++){var u=n[o],s=x(t,""+o),c=L(e,s,r,u);i[o]=c}return i}if(null===n)return null;var l=e.store.keyOfEntity(n),f=null!==l?l:t;return K(e,f,r,n),f},P=function(e,t,r,n){for(var i,o=new q(t,t,r,e);void 0!==(i=o.next());){var c=a(i),l=u(i),f=d(i,e.variables),v=n[l];if(void 0!==i.selectionSet&&null!==v&&!S(v)){var p=s(i);F(e,v,p)}if(t===e.store.getRootKey("mutation")||t===e.store.getRootKey("subscription")){var y=e.store.updates[t][c];void 0!==y&&y(n,f||{},e.store,e)}}},F=function(e,t,r){if(Array.isArray(t)){for(var n=new Array(t.length),i=0,o=t.length;i<o;i++)n[i]=F(e,t[i],r);return n}if(null!==t){var a=e.store.keyOfEntity(t);null!==a?K(e,a,r,t):P(e,t.__typename,r,t)}},E=function(e,t,r){var n,i=e.store,o=e.variables,u="Query"===t;if(u)n=t;else{if(V(t),"string"!=typeof(n=i.getField(t,"__typename")))return;i.removeRecord(x(t,O("__typename")))}for(var c,l=new q(n,t,r,e);void 0!==(c=l.next());){var f=a(c),v=d(c,o),p=x(t,O(f,v));if(u&&V(p),void 0===c.selectionSet)i.removeRecord(p);else{var y=s(c),h=i.getLink(p);if(i.removeLink(p),void 0===h)void 0!==i.getRecord(p)&&i.removeRecord(p);else if(Array.isArray(h))for(var m=0,g=h.length;m<g;m++){var k=h[m];null!==k&&E(e,k,y)}else null!==h&&E(e,h,y)}}},j={current:null},I={current:null},M=function(e){j.current=new Set,I.current=e},Q=function(){j.current=null,I.current=null},D=function(){return j.current},V=function(e){j.current.add(e)},C=function(e,t,n){return r.setOptimistic(e,t,n,I.current||0)},B=function(e,t){var n=I.current||0;return n?r.setOptimistic(e,t,void 0,n):r.remove(e,t)},G=function(e,t,n,i,o){var a;if(this.records=r.asMutable(r.make()),this.links=r.asMutable(r.make()),this.resolvers=t||{},this.optimisticMutations=i||{},this.keys=o||{},this.schemaPredicates=e,this.updates={Mutation:n&&n.Mutation||{},Subscription:n&&n.Subscription||{}},e){var u=e.schema,s=u.getQueryType(),c=u.getMutationType(),l=u.getSubscriptionType(),f=s?s.name:"Query",v=c?c.name:"Mutation",d=l?l.name:"Subscription";this.rootFields={query:f,mutation:v,subscription:d},this.rootNames=((a={})[f]="query",a[v]="mutation",a[d]="subscription",a)}else this.rootFields={query:"Query",mutation:"Mutation",subscription:"Subscription"},this.rootNames={Query:"query",Mutation:"mutation",Subscription:"subscription"}};G.prototype.getRootKey=function(e){return this.rootFields[e]},G.prototype.keyOfEntity=function(e){var t,r=e.__typename,n=e.id,i=e._id;if(!r)return null;if(this.rootNames[r])return this.rootNames[r];if(this.keys[r])t=""+this.keys[r](e);else if(null!=n)t=""+n;else{if(null==i)return null;t=""+i}return t?r+":"+t:null},G.prototype.clearOptimistic=function(e){this.records=r.clearOptimistic(this.records,e),this.links=r.clearOptimistic(this.links,e)},G.prototype.getRecord=function(e){return r.get(this.records,e)},G.prototype.removeRecord=function(e){return this.records=B(this.records,e)},G.prototype.writeRecord=function(e,t){return this.records=C(this.records,t,e)},G.prototype.getField=function(e,t,r){var n=x(e,O(t,r));return this.getRecord(n)},G.prototype.writeField=function(e,t,r,n){var i=x(t,O(r,n));return this.records=C(this.records,i,e)},G.prototype.getLink=function(e){return r.get(this.links,e)},G.prototype.removeLink=function(e){return this.links=B(this.links,e)},G.prototype.writeLink=function(e,t){return this.links=C(this.links,t,e)},G.prototype.resolveValueOrLink=function(e){var t=this.getRecord(e);return void 0!==t?t:this.getLink(e)||null},G.prototype.resolve=function(e,t,r){if("string"==typeof e)return V(e),this.resolveValueOrLink(x(e,O(t,r)));var n=this.keyOfEntity(e);return null===n?null:(V(n),this.resolveValueOrLink(x(n,O(t,r))))},G.prototype.invalidateQuery=function(e,t){!function(e,t){M(0);var r=m(t.query),n={variables:p(r,t.variables),fragments:k(t.query),store:e,schemaPredicates:e.schemaPredicates};E(n,n.store.getRootKey("query"),s(r)),Q()}(this,{query:e,variables:t})},G.prototype.hasField=function(e){return void 0!==this.getRecord(e)||void 0!==this.getLink(e)},G.prototype.updateQuery=function(e,t){var r=t(J(this,e).data);null!==r&&A(this,e,r)},G.prototype.writeFragment=function(e,t){T(this,e,t)};var z=function(e,t,r){M(0);var n=J(e,t,r);return Q(),n},J=function(e,t,r){var n=m(t.query),i=e.getRootKey(n.operation),o=s(n),a={variables:p(n,t.variables),fragments:k(t.query),partial:!1,store:e,schemaPredicates:e.schemaPredicates},u=r||Object.create(null);return u="Query"!==i?U(a,i,o,u):W(a,i,o,u),{dependencies:D(),partial:void 0!==u&&a.partial,data:void 0===u?null:u}},U=function(e,t,r,n){if("string"!=typeof n.__typename)return n;var i=Object.create(null);i.__typename=n.__typename;for(var o,a=new q(t,t,r,e);void 0!==(o=a.next());){var c=u(o),l=n[c];i[c]=void 0===o.selectionSet||null===l||S(l)?l:H(e,s(o),l)}return i},H=function(e,t,r){if(Array.isArray(r)){for(var n=new Array(r.length),i=0,o=r.length;i<o;i++)n[i]=H(e,t,r[i]);return n}if(null===r)return null;var a=e.store.keyOfEntity(r);if(null!==a){var u=Object.create(null),s=W(e,a,t,u);return void 0===s?null:s}return U(e,r.__typename,t,r)},W=function(e,t,r,n){var i=e.store,o=e.variables,c=e.schemaPredicates,l=t===i.getRootKey("query");l||V(t);var f=l?t:i.getField(t,"__typename");if("string"==typeof f){n.__typename=f;for(var v,p=new q(f,t,r,e),y=!1,h=!1;void 0!==(v=p.next());){var m=a(v),g=d(v,o),k=u(v),b=x(t,O(m,g)),w=i.getRecord(b);l&&V(b);var _=void 0,S=i.resolvers[f];if(void 0!==S&&"function"==typeof S[m]){void 0!==w&&(n[k]=w);var R=S[m](n,g||{},i,e);void 0!==v.selectionSet&&(R=X(e,R,f,m,b,s(v),n[k]));var A=null==R;_=A&&void 0!==c?void 0:A?null:R}else if(void 0===v.selectionSet)_=w;else{var N=s(v),T=i.getLink(b);void 0!==T?_=Y(e,T,f,m,N,n[k]):"object"==typeof w&&null!==w&&(_=w)}if(void 0===_&&void 0!==c&&c.isFieldNullable(f,m))h=!0,n[k]=null;else{if(void 0===_)return;y=!0,n[k]=_}}return h&&(e.partial=!0),l&&h&&!y?void 0:n}},X=function(e,t,r,n,i,o,a){if(Array.isArray(t)){for(var u=e.schemaPredicates,s=void 0!==u&&u.isListNullable(r,n),c=new Array(t.length),l=0,f=t.length;l<f;l++){var v=void 0!==a?a[l]:void 0,d=x(i,""+l),p=X(e,t[l],r,n,d,o,v);if(void 0===p&&!s)return;t[l]=void 0!==p?p:null}return c}if(null===t)return null;if(Z(t)){var y=void 0===a?Object.create(null):a,h=("string"==typeof t?t:e.store.keyOfEntity(t))||i;return W(e,h,o,y)}},Y=function(e,t,r,n,i,o){if(Array.isArray(t)){for(var a=e.schemaPredicates,u=void 0!==a&&a.isListNullable(r,n),s=new Array(t.length),c=0,l=t.length;c<l;c++){var f=Y(e,t[c],r,n,i,void 0!==o?o[c]:void 0);if(void 0===f&&!u)return;s[c]=void 0!==f?f:null}return s}if(null===t)return null;var v=void 0===o?Object.create(null):o;return W(e,t,i,v)},Z=function(e){return"string"==typeof e||"object"==typeof e&&null!==e&&"string"==typeof e.__typename},$=function(e){this.schema=t.buildClientSchema(e)};$.prototype.isFieldNullable=function(e,r){var n=ee(this.schema,e,r);return void 0!==n&&t.isNullableType(n.type)},$.prototype.isListNullable=function(e,r){var n=ee(this.schema,e,r);if(void 0===n)return!1;var i=t.isNonNullType(n.type)?n.type.ofType:n.type;return t.isListType(i)&&t.isNullableType(i.ofType)},$.prototype.isInterfaceOfType=function(e,t){if(!t||!e)return!1;if(t===e)return!0;var r=this.schema.getType(e),n=this.schema.getType(t);return this.schema.isPossibleType(r,n)};var ee=function(e,t,r){var n=e.getType(t);if(void 0!==n){var i=n.getFields()[r];if(void 0!==i)return i}},te=function(e,t){return w({},e,{context:w({},e.context,{meta:w({},e.context.meta,{cacheOutcome:t})})})},re=function(e){return w({},e,{query:i.formatDocument(e.query)})},ne=function(e){return e.context.requestPolicy},ie=function(e){return"query"===e.operationName},oe=function(e){var t=ne(e);return ie(e)&&"network-only"!==t},ae=function(e,t){return w({},e,{context:w({},e.context,{requestPolicy:t})})};function ue(e){return oe(e)}function se(e){return"miss"===e.outcome}function ce(e){return te(e.operation,e.outcome)}function le(e){return"miss"!==e.outcome}function fe(e){return!oe(e)}exports.Store=G,exports.cacheExchange=function(e){return function(t){var r=t.forward,n=t.client;e||(e={});var i=new G(e.schema?new $(e.schema):void 0,e.resolvers,e.updates,e.optimistic,e.keys),a=new Set,u=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=u.get(t);u.delete(t),n.reexecuteOperation(r)}})},l=function(e){if(o=ne(n=e),function(e){return"mutation"===e.operationName}(n)&&"network-only"!==o){var t=e.key,r=N(i,e,t).dependencies;0!==r.size&&(a.add(t),c(e,r))}var n,o},f=function(e,t){t.forEach(function(t){(s[t]||(s[t]=[])).push(e.key),u.has(e.key)||u.set(e.key,"network-only"===e.context.requestPolicy?ae(e,"cache-and-network"):e)})},v=function(e){var t,r=ne(e),n=z(i,e),o=n.data,a=n.partial;return null===o?t="miss":(f(e,n.dependencies),t=a&&"cache-only"!==r?"partial":"hit"),{outcome:t,operation:e,data:o}},d=function(e){var t,r,n=e.operation,o=e.error,u=e.extensions,s=ie(n),l=e.data,v=n.key;if(a.has(v)&&(a.delete(v),i.clearOptimistic(v)),null!=l)if(t=R(i,n,l).dependencies,s){var d=z(i,n);l=d.data,r=d.dependencies}else l=z(i,n,l).data;return void 0!==t&&c(e.operation,t),s&&void 0!==r&&f(e.operation,r),{data:l,error:o,extensions:u,operation:n}};function p(e){var t=e.operation,r=e.outcome,i=ne(t);if("cache-and-network"===i||"cache-first"===i&&"partial"===r){var o=ae(t,"network-only");n.reexecuteOperation(o)}return{operation:te(t,r),data:e.data,error:e.error,extensions:e.extensions}}return function(e){var t=o.pipe(e,o.map(re),o.tap(l),o.share),n=o.pipe(t,o.filter(ue),o.map(v),o.share),i=o.pipe(n,o.filter(se),o.map(ce)),a=o.pipe(n,o.filter(le),o.map(p)),u=o.pipe(r(o.merge([o.pipe(t,o.filter(fe)),i])),o.map(d));return o.merge([u,a])}}},exports.query=z,exports.read=J,exports.write=R,exports.writeFragment=T,exports.writeOptimistic=N;
//# sourceMappingURL=urql-exchange-graphcache.min.js.map
{
"name": "@urql/exchange-graphcache",
"version": "1.0.0-rc.0",
"version": "1.0.0-rc.1",
"description": "A normalized and configurable cache exchange for urql",

@@ -5,0 +5,0 @@ "repository": "https://github.com/FormidableLabs/urql-exchange-graphcache",

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

You can also pass your introspected GraphQL schema to the `cacheExchange`, which enables
it to deliver partial results and match fragments deterministically!
`urql` is already quite a comprehensive GraphQL client. However in several cases it may be

@@ -73,11 +76,33 @@ desirable to have data update across the entirety of an app when a response updates some

- [ ] Schema awareness and deterministic fragment matching
- [x] Schema awareness and deterministic fragment matching
- [ ] Basic offline and persistence support
- [ ] Partial query results with `cache-only` and `cache-and-network` policies
- [x] Partial query results
Schema awareness is important so that we can offer safe offline results that
are partially cached instead of fully. The schema is also necessary to know
how to match interface or enum fragments correctly. **Currently a heuristic
is in place that matches if all fields of the fragment are present in the cache**
This cache defaults to **delivering safe results** by marking results as incomplete
when any field is missing, triggering a `network-only` operation (a request), when
it encounters uncached fields.
Furthermore there's one case in caching where only having the `__typename` field
leads to potentially unsafe behaviour: **interfaces**. When the cache encounters a
fragment that tries to get data for an interface, it can't tell whether the
cached type matches the interface. In this case we resort to a heuristic
by default. When all fields of the fragment are on the target type, then the
fragment matches successfully and we log a warning.
Schema awareness has been introduced to the cache to improve this behaviour.
When you pass your API's GraphQL schema to the cache, it becomes able to
deliver **partial results**. When the cache has enough information so that
only **optional fields** in a given query are missing, then it delivers
a partial result from the cached data. Subsequently it still issues a network
request (like with `cache-and-network`) to ensure that all information will
still be delivered eventually.
With a schema the cache can also match fragments that refer to interfaces
**deterministically**, since it can look at the schema to match fragments
against types.
Schema awareness is also an important stepping stone for offline support.
Without partial results it becomes difficult to deliver an offline UI
safely, when just some bits of information are missing.
## Usage

@@ -91,2 +116,4 @@

- `keys`: A `__typename` map of functions to generate keys with
- `schema`: An introspected GraphQL schema in JSON format. When it's passed the cache will
deliver partial results and enable deterministic fragment matching.

@@ -125,4 +152,10 @@ > Note that you don't need any of these options to get started

### Schema
Our way to see what your backend schema looks like, this offers additional functionality.
[Read more](./docs/schema.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.

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

FieldNode,
OperationDefinitionNode,
FragmentDefinitionNode,

@@ -19,13 +18,2 @@ Kind,

export const getOperationName = (node: OperationDefinitionNode) => {
switch (node.operation) {
case 'query':
return 'Query';
case 'mutation':
return 'Mutation';
case 'subscription':
return 'Subscription';
}
};
export const getFragmentTypeName = (node: FragmentDefinitionNode): string =>

@@ -32,0 +20,0 @@ node.typeCondition.name.value;

@@ -500,2 +500,3 @@ import gql from 'graphql-tag';

id: '123',
book: null,
name: '[REDACTED ONLINE]',

@@ -523,2 +524,3 @@ },

name: '[REDACTED ONLINE]',
book: null,
},

@@ -542,4 +544,3 @@ {

return { operation: queryOperation, data: queryData };
}
if (forwardOp.key === 2) {
} else if (forwardOp.key === 2) {
return { operation: mutationOperation, data: mutationData };

@@ -628,1 +629,147 @@ }

});
it('reexecutes query and returns data on partial result', () => {
jest.useFakeTimers();
const client = createClient({ url: '' });
const [ops$, next] = makeSubject<Operation>();
const reexec = jest
.spyOn(client, 'reexecuteOperation')
// Empty mock to avoid going in an endless loop, since we would again return
// partial data.
.mockImplementation(() => {});
const initialQuery = gql`
query {
todos {
id
text
__typename
}
}
`;
const query = gql`
query {
todos {
id
text
complete
author {
id
name
__typename
}
__typename
}
}
`;
const initialQueryOperation = client.createRequestOperation('query', {
key: 1,
query: initialQuery,
});
const queryOperation = client.createRequestOperation('query', {
key: 2,
query,
});
const queryData = {
__typename: 'Query',
todos: [
{
__typename: 'Todo',
id: '123',
text: 'Learn',
},
{
__typename: 'Todo',
id: '456',
text: 'Teach',
},
],
};
const response = jest.fn(
(forwardOp: Operation): OperationResult => {
if (forwardOp.key === 1) {
return { operation: initialQueryOperation, data: queryData };
} else if (forwardOp.key === 2) {
return { operation: queryOperation, data: queryData };
}
return undefined as any;
}
);
const result = jest.fn();
const forward: ExchangeIO = ops$ =>
pipe(
ops$,
delay(1),
map(response)
);
pipe(
cacheExchange({
// eslint-disable-next-line
schema: require('./test-utils/simple_schema.json'),
})({ forward, client })(ops$),
tap(result),
publish
);
next(initialQueryOperation);
jest.runAllTimers();
expect(response).toHaveBeenCalledTimes(1);
expect(reexec).toHaveBeenCalledTimes(0);
expect(result.mock.calls[0][0].data).toEqual({
__typename: 'Query',
todos: [
{
__typename: 'Todo',
id: '123',
text: 'Learn',
},
{
__typename: 'Todo',
id: '456',
text: 'Teach',
},
],
});
expect(result.mock.calls[0][0]).toHaveProperty(
'operation.context.meta',
undefined
);
next(queryOperation);
jest.runAllTimers();
expect(result).toHaveBeenCalledTimes(2);
expect(reexec).toHaveBeenCalledTimes(1);
expect(result.mock.calls[1][0].data).toEqual({
__typename: 'Query',
todos: [
{
__typename: 'Todo',
author: null,
complete: null,
id: '123',
text: 'Learn',
},
{
__typename: 'Todo',
author: null,
complete: null,
id: '456',
text: 'Teach',
},
],
});
expect(result.mock.calls[1][0]).toHaveProperty(
'operation.context.meta.cacheOutcome',
'partial'
);
});

@@ -11,7 +11,7 @@ import {

import { filter, map, merge, pipe, share, tap } from 'wonka';
import { query, write, writeOptimistic, readOperation } from './operations';
import { query, write, writeOptimistic } from './operations';
import { SchemaPredicates } from './ast/schemaPredicates';
import { Store } from './store';
import {
Completeness,
UpdatesConfig,

@@ -24,3 +24,3 @@ ResolverConfig,

type OperationResultWithMeta = OperationResult & {
completeness: Completeness;
outcome: CacheOutcome;
};

@@ -35,14 +35,9 @@

// 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,
},
const addCacheOutcome = (op: Operation, outcome: CacheOutcome): Operation => ({
...op,
context: {
...op.context,
meta: {
...op.context.meta,
cacheOutcome: outcome,
},

@@ -98,2 +93,3 @@ },

keys?: KeyingConfig;
schema?: object;
}

@@ -106,3 +102,5 @@

if (!opts) opts = {};
const store = new Store(
opts.schema ? new SchemaPredicates(opts.schema) : undefined,
opts.resolvers,

@@ -113,2 +111,3 @@ opts.updates,

);
const optimisticKeys = new Set();

@@ -180,12 +179,16 @@ const ops: OperationMap = new Map();

const policy = getRequestPolicy(operation);
const res = query(store, operation);
const isComplete = policy === 'cache-only' || res.completeness === 'FULL';
if (isComplete) {
updateDependencies(operation, res.dependencies);
const { data, dependencies, partial } = query(store, operation);
let cacheOutcome: CacheOutcome;
if (data === null) {
cacheOutcome = 'miss';
} else {
updateDependencies(operation, dependencies);
cacheOutcome = !partial || policy === 'cache-only' ? 'hit' : 'partial';
}
return {
outcome: cacheOutcome,
operation,
completeness: isComplete ? 'FULL' : 'EMPTY',
data: res.data,
data,
};

@@ -216,3 +219,3 @@ };

} else {
data = readOperation(store, operation, data).data;
data = query(store, operation, data).data;
}

@@ -253,4 +256,4 @@ }

cache$,
filter(res => res.completeness !== 'FULL'),
map(res => res.operation)
filter(res => res.outcome === 'miss'),
map(res => addCacheOutcome(res.operation, res.outcome))
);

@@ -262,11 +265,23 @@

cache$,
filter(res => res.completeness === 'FULL'),
tap(({ operation }) => {
const policy = getRequestPolicy(operation);
if (policy === 'cache-and-network') {
const networkOnly = toRequestPolicy(operation, 'network-only');
client.reexecuteOperation(networkOnly);
filter(res => res.outcome !== 'miss'),
map(
(res: OperationResultWithMeta): OperationResult => {
const { operation, outcome } = res;
const policy = getRequestPolicy(operation);
if (
policy === 'cache-and-network' ||
(policy === 'cache-first' && outcome === 'partial')
) {
const networkOnly = toRequestPolicy(operation, 'network-only');
client.reexecuteOperation(networkOnly);
}
return {
operation: addCacheOutcome(operation, outcome),
data: res.data,
error: res.error,
extensions: res.extensions,
};
}
}),
map(addCacheOutcome('hit'))
)
);

@@ -286,4 +301,3 @@

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

@@ -290,0 +304,0 @@

import stringify from 'fast-json-stable-stringify';
import { Variables, KeyGenerator, Data } from '../types';
import { Variables } from '../types';
export const keyOfEntity: KeyGenerator = (data: Data): null | string => {
const { __typename: typeName } = data;
const id = data.id === undefined ? data._id : data.id;
if (typeName === undefined || typeName === null) {
return null;
} else if (
typeName === 'Query' ||
typeName === 'Mutation' ||
typeName === 'Subscription'
) {
return typeName;
} else if (id === null || id === undefined) {
return null;
}
return `${typeName}:${id}`;
};
export const keyOfField = (fieldName: string, args?: null | Variables) =>

@@ -24,0 +5,0 @@ args ? `${fieldName}(${stringify(args)})` : fieldName;

export * from './operations';
export * from './types';
export { keyOfEntity as defaultKeyOfEntity } from './helpers';
export { Store } from './store';
export { cacheExchange } from './exchange';

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

export { query, readOperation } from './query';
export { query, read } from './query';
export { write, writeOptimistic, writeFragment } from './write';

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

import { joinKeys, keyOfField } from '../helpers';
import { SchemaPredicates } from '../ast/schemaPredicates';

@@ -25,2 +26,3 @@ interface Context {

fragments: Fragments;
schemaPredicates?: SchemaPredicates;
}

@@ -36,5 +38,10 @@

store,
schemaPredicates: store.schemaPredicates,
};
invalidateSelection(ctx, 'Query', getSelectionSet(operation));
invalidateSelection(
ctx,
ctx.store.getRootKey('query'),
getSelectionSet(operation)
);

@@ -41,0 +48,0 @@ clearStoreState();

@@ -11,3 +11,2 @@ import warning from 'warning';

getFieldAlias,
getOperationName,
} from '../ast';

@@ -22,3 +21,2 @@

SelectionSet,
Completeness,
OperationRequest,

@@ -38,6 +36,7 @@ NullArray,

import { joinKeys, keyOfField } from '../helpers';
import { SchemaPredicates } from '../ast/schemaPredicates';
export interface QueryResult {
completeness: Completeness;
dependencies: Set<string>;
partial: boolean;
data: null | Data;

@@ -47,13 +46,16 @@ }

interface Context {
result: QueryResult;
partial: boolean;
store: Store;
variables: Variables;
fragments: Fragments;
schemaPredicates?: SchemaPredicates;
}
/** Reads a request entirely from the store */
export const query = (store: Store, request: OperationRequest): QueryResult => {
export const query = (
store: Store,
request: OperationRequest,
data?: Data
): QueryResult => {
initStoreState(0);
const result = startQuery(store, request);
const result = read(store, request, data);
clearStoreState();

@@ -63,10 +65,10 @@ return result;

export const startQuery = (store: Store, request: OperationRequest) => {
export const read = (
store: Store,
request: OperationRequest,
input?: Data
): QueryResult => {
const operation = getMainOperation(request.query);
const root: Data = Object.create(null);
const result: QueryResult = {
completeness: 'FULL',
dependencies: getCurrentDependencies(),
data: root,
};
const rootKey = store.getRootKey(operation.operation);
const rootSelect = getSelectionSet(operation);

@@ -76,45 +78,21 @@ const ctx: Context = {

fragments: getFragments(request.query),
result,
partial: false,
store,
schemaPredicates: store.schemaPredicates,
};
result.data = readSelection(ctx, 'Query', getSelectionSet(operation), root);
let data = input || Object.create(null);
data =
rootKey !== 'Query'
? readRoot(ctx, rootKey, rootSelect, data)
: readSelection(ctx, rootKey, rootSelect, data);
return result;
};
export const readOperation = (
store: Store,
request: OperationRequest,
data: Data
) => {
initStoreState(0);
const operation = getMainOperation(request.query);
const result: QueryResult = {
completeness: 'FULL',
return {
dependencies: getCurrentDependencies(),
data: null,
partial: data === undefined ? false : ctx.partial,
data: data === undefined ? null : data,
};
const ctx: Context = {
variables: normalizeVariables(operation, request.variables),
fragments: getFragments(request.query),
result,
store,
};
result.data = readRoot(
ctx,
getOperationName(operation),
getSelectionSet(operation),
data
);
clearStoreState();
return result;
};
export const readRoot = (
const readRoot = (
ctx: Context,

@@ -170,4 +148,7 @@ entityKey: string,

if (entityKey !== null) {
const data: Data = Object.create(null);
return readSelection(ctx, entityKey, select, data);
// We assume that since this is used for result data this can never be undefined,
// since the result data has already been written to the cache
const newData = Object.create(null);
const fieldValue = readSelection(ctx, entityKey, select, newData);
return fieldValue === undefined ? null : fieldValue;
} else {

@@ -184,20 +165,21 @@ const typename = originalData.__typename;

data: Data
): Data | null => {
const isQuery = entityKey === 'Query';
): Data | undefined => {
const { store, variables, schemaPredicates } = ctx;
const isQuery = entityKey === store.getRootKey('query');
if (!isQuery) addDependency(entityKey);
const { store, variables } = ctx;
// Get the __typename field for a given entity to check that it exists
const typename = isQuery ? 'Query' : store.getField(entityKey, '__typename');
const typename = isQuery
? entityKey
: store.getField(entityKey, '__typename');
if (typeof typename !== 'string') {
ctx.result.completeness = 'EMPTY';
return null;
return undefined;
}
data.__typename = typename;
const iter = new SelectionIterator(typename, entityKey, select, ctx);
let node;
let hasFields = false;
let hasPartials = false;
while ((node = iter.next()) !== undefined) {

@@ -213,9 +195,15 @@ // Derive the needed data from our node.

// We temporarily store the data field in here, but undefined
// means that the value is missing from the cache
let dataFieldValue: void | DataField;
const resolvers = store.resolvers[typename];
if (resolvers !== undefined && resolvers.hasOwnProperty(fieldName)) {
if (resolvers !== undefined && typeof resolvers[fieldName] === 'function') {
// We have a resolver for this field.
// Prepare the actual fieldValue, so that the resolver can use it
if (fieldValue !== undefined) {
data[fieldAlias] = fieldValue;
}
const resolverValue = resolvers[fieldName](
let resolverValue: DataField | undefined = resolvers[fieldName](
data,

@@ -227,52 +215,72 @@ fieldArgs || {},

if (node.selectionSet === undefined) {
// If it doesn't have a selection set we have resolved a property.
// We assume that a resolver for scalar values implies that this
// field is always present, so completeness won't be set to EMPTY here
data[fieldAlias] = resolverValue !== undefined ? resolverValue : null;
} else {
if (node.selectionSet !== undefined) {
// When it has a selection set we are resolving an entity with a
// subselection. This can either be a list or an object.
const fieldSelect = getSelectionSet(node);
data[fieldAlias] = resolveResolverResult(
resolverValue = resolveResolverResult(
ctx,
resolverValue,
typename,
fieldName,
fieldKey,
fieldSelect,
getSelectionSet(node),
data[fieldAlias] as Data | Data[]
);
}
// When we have a schema we check for a user's resolver whether the field is nullable
// Otherwise we trust the resolver and assume that it is
const isNull = resolverValue === undefined || resolverValue === null;
if (isNull && schemaPredicates !== undefined) {
dataFieldValue = undefined;
} else {
dataFieldValue = isNull ? null : resolverValue;
}
} else if (node.selectionSet === undefined) {
// The field is a scalar and can be retrieved directly
if (fieldValue === undefined) {
// Cache Incomplete: A missing field means it wasn't cached
ctx.result.completeness = 'EMPTY';
data[fieldAlias] = null;
} else {
// Not dealing with undefined means it's a cached field
data[fieldAlias] = fieldValue;
}
dataFieldValue = fieldValue;
} else {
// null values mean that a field might be linked to other entities
// We have a selection set which means that we'll be checking for links
const fieldSelect = getSelectionSet(node);
const link = store.getLink(fieldKey);
// Cache Incomplete: A missing link for a field means it's not cached
if (link === undefined) {
if (typeof fieldValue === 'object' && fieldValue !== null) {
// The entity on the field was invalid and can still be recovered
data[fieldAlias] = fieldValue;
} else {
ctx.result.completeness = 'EMPTY';
data[fieldAlias] = null;
}
} else {
if (link !== undefined) {
const prevData = data[fieldAlias] as Data;
data[fieldAlias] = resolveLink(ctx, link, fieldSelect, prevData);
dataFieldValue = resolveLink(
ctx,
link,
typename,
fieldName,
fieldSelect,
prevData
);
} else if (typeof fieldValue === 'object' && fieldValue !== null) {
// The entity on the field was invalid but can still be recovered
dataFieldValue = fieldValue;
}
}
// Now that dataFieldValue has been retrieved it'll be set on data
// If it's uncached (undefined) but nullable we can continue assembling
// a partial query result
if (
dataFieldValue === undefined &&
schemaPredicates !== undefined &&
schemaPredicates.isFieldNullable(typename, fieldName)
) {
// The field is uncached but we have a schema that says it's nullable
// Set the field to null and continue
hasPartials = true;
data[fieldAlias] = null;
} else if (dataFieldValue === undefined) {
// The field is uncached and not nullable; return undefined
return undefined;
} else {
// Otherwise continue as usual
hasFields = true;
data[fieldAlias] = dataFieldValue;
}
}
return data;
if (hasPartials) ctx.partial = true;
return isQuery && hasPartials && !hasFields ? undefined : data;
};

@@ -283,14 +291,35 @@

result: DataField,
typename: string,
fieldName: string,
key: string,
select: SelectionSet,
prevData: void | Data | Data[]
) => {
): DataField | undefined => {
// When we are dealing with a list we have to call this method again.
if (Array.isArray(result)) {
// @ts-ignore: Link cannot be expressed as a recursive type
return result.map((childResult, index) => {
const data = prevData !== undefined ? prevData[index] : undefined;
const indexKey = joinKeys(key, `${index}`);
return resolveResolverResult(ctx, childResult, indexKey, select, data);
});
const { schemaPredicates } = ctx;
const isListNullable =
schemaPredicates !== undefined &&
schemaPredicates.isListNullable(typename, fieldName);
const newResult = new Array(result.length);
for (let i = 0, l = result.length; i < l; i++) {
const data = prevData !== undefined ? prevData[i] : undefined;
const childKey = joinKeys(key, `${i}`);
const childResult = resolveResolverResult(
ctx,
result[i],
typename,
fieldName,
childKey,
select,
data
);
if (childResult === undefined && !isListNullable) {
return undefined;
} else {
result[i] = childResult !== undefined ? childResult : null;
}
}
return newResult;
} else if (result === null) {

@@ -305,13 +334,4 @@ return null;

key;
const selectionResult = readSelection(ctx, childKey, select, data);
if (selectionResult !== null && typeof result === 'object') {
for (key in result) {
if (key !== '__typename' && result.hasOwnProperty(key)) {
selectionResult[key] = result[key];
}
}
}
return selectionResult;
// TODO: Copy over fields from result but check against schema whether that's safe
return readSelection(ctx, childKey, select, data);
}

@@ -327,4 +347,3 @@

ctx.result.completeness = 'EMPTY';
return null;
return undefined;
};

@@ -335,10 +354,28 @@

link: Link | Link[],
typename: string,
fieldName: string,
select: SelectionSet,
prevData: void | Data | Data[]
): null | Data | Data[] => {
): DataField | undefined => {
if (Array.isArray(link)) {
const { schemaPredicates } = ctx;
const isListNullable =
schemaPredicates !== undefined &&
schemaPredicates.isListNullable(typename, fieldName);
const newLink = new Array(link.length);
for (let i = 0, l = link.length; i < l; i++) {
const data = prevData !== undefined ? prevData[i] : undefined;
newLink[i] = resolveLink(ctx, link[i], select, data);
const innerPrevData = prevData !== undefined ? prevData[i] : undefined;
const childLink = resolveLink(
ctx,
link[i],
typename,
fieldName,
select,
innerPrevData
);
if (childLink === undefined && !isListNullable) {
return undefined;
} else {
newLink[i] = childLink !== undefined ? childLink : null;
}
}

@@ -345,0 +382,0 @@

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

import warning from 'warning';
import { FieldNode, InlineFragmentNode, FragmentDefinitionNode } from 'graphql';

@@ -15,2 +16,3 @@ import { Fragments, Variables, SelectionSet, Scalar } from '../types';

} from '../ast';
import { SchemaPredicates } from '../ast/schemaPredicates';

@@ -21,5 +23,6 @@ interface Context {

fragments: Fragments;
schemaPredicates?: SchemaPredicates;
}
const isFragmentMatching = (
const isFragmentHeuristicallyMatching = (
node: InlineFragmentNode | FragmentDefinitionNode,

@@ -30,9 +33,17 @@ typename: void | string,

) => {
if (!typename) {
return false;
} else if (typename === getTypeCondition(node)) {
return true;
}
if (!typename) return false;
const typeCondition = getTypeCondition(node);
if (typename === typeCondition) return true;
// This is a heuristic for now, but temporary until schema awareness becomes a thing
warning(
false,
'Heuristic Fragment Matching: A fragment is trying to match against the `%s` type, ' +
'but the type condition is `%s`. Since GraphQL allows for interfaces `%s` may be an' +
'interface.\nA schema needs to be defined for this match to be deterministic, ' +
'otherwise the fragment will be matched heuristically!',
typename,
typeCondition,
typeCondition
);
return !getSelectionSet(node).some(node => {

@@ -84,13 +95,21 @@ if (!isFieldNode(node)) return false;

: node;
if (
fragmentNode !== undefined &&
isFragmentMatching(
fragmentNode,
this.typename,
this.entityKey,
this.context
)
) {
this.indexStack.push(0);
this.selectionStack.push(getSelectionSet(fragmentNode));
if (fragmentNode !== undefined) {
const isMatching =
this.context.schemaPredicates !== undefined
? this.context.schemaPredicates.isInterfaceOfType(
getTypeCondition(fragmentNode),
this.typename
)
: isFragmentHeuristicallyMatching(
fragmentNode,
this.typename,
this.entityKey,
this.context
);
if (isMatching) {
this.indexStack.push(0);
this.selectionStack.push(getSelectionSet(fragmentNode));
}
}

@@ -97,0 +116,0 @@

@@ -12,3 +12,2 @@ import warning from 'warning';

getName,
getOperationName,
getFieldArguments,

@@ -37,2 +36,3 @@ } from '../ast';

import { joinKeys, keyOfField } from '../helpers';
import { SchemaPredicates } from '../ast/schemaPredicates';

@@ -48,2 +48,3 @@ export interface WriteResult {

fragments: Fragments;
schemaPredicates?: SchemaPredicates;
}

@@ -79,8 +80,9 @@

store,
schemaPredicates: store.schemaPredicates,
};
const select = getSelectionSet(operation);
const operationName = getOperationName(operation);
const operationName = ctx.store.getRootKey(operation.operation);
if (operationName === 'Query') {
if (operationName === ctx.store.getRootKey('query')) {
writeSelection(ctx, operationName, select, data);

@@ -109,6 +111,7 @@ } else {

store,
schemaPredicates: store.schemaPredicates,
};
const operationName = getOperationName(operation);
if (operationName === 'Mutation') {
const operationName = ctx.store.getRootKey(operation.operation);
if (operationName === ctx.store.getRootKey('mutation')) {
const select = getSelectionSet(operation);

@@ -178,2 +181,3 @@ const iter = new SelectionIterator(

store,
schemaPredicates: store.schemaPredicates,
};

@@ -191,3 +195,3 @@

const { store, variables } = ctx;
const isQuery = entityKey === 'Query';
const isQuery = entityKey === ctx.store.getRootKey('query');
const typename = data.__typename;

@@ -302,3 +306,6 @@ if (!isQuery) addDependency(entityKey);

if (typename === 'Mutation' || typename === 'Subscription') {
if (
typename === ctx.store.getRootKey('mutation') ||
typename === ctx.store.getRootKey('subscription')
) {
// We run side-effect updates after the default, normalized updates

@@ -305,0 +312,0 @@ // so that the data is already available in-store if necessary

@@ -42,3 +42,3 @@ import gql from 'graphql-tag';

it('generates keys from custom keying function', () => {
const store = new Store(undefined, undefined, undefined, {
const store = new Store(undefined, undefined, undefined, undefined, {
User: () => 'me',

@@ -52,3 +52,3 @@ });

expect(store.keyOfEntity({ __typename: 'Any' })).toBe(null);
expect(store.keyOfEntity({ __typename: 'User' })).toBe('me');
expect(store.keyOfEntity({ __typename: 'User' })).toBe('User:me');
});

@@ -61,3 +61,3 @@ });

beforeEach(() => {
store = new Store(undefined, undefined, {
store = new Store(undefined, undefined, undefined, {
addTodo: variables => {

@@ -145,3 +145,3 @@ return {

({ data } = query(store, { query: Todos }));
expect((data as any).todos).toEqual(null);
expect(data).toBe(null);
expect(store.getRecord('Todo:0.text')).toBe(undefined);

@@ -182,3 +182,3 @@ });

}));
expect((data as any).appointment).toEqual(null);
expect(data).toBe(null);
expect(store.getRecord('Appointment:1.info')).toBe(undefined);

@@ -244,3 +244,5 @@ });

const { data: result } = query(store, { query: Todos });
const { data: result } = query(store, {
query: Todos,
});
expect(result).toEqual({

@@ -247,0 +249,0 @@ __typename: 'Query',

@@ -17,6 +17,7 @@ import invariant from 'invariant';

import { keyOfEntity, joinKeys, keyOfField } from './helpers';
import { startQuery } from './operations/query';
import { joinKeys, keyOfField } from './helpers';
import { read } from './operations/query';
import { writeFragment, startWrite } from './operations/write';
import { invalidate } from './operations/invalidate';
import { SchemaPredicates } from './ast/schemaPredicates';

@@ -74,2 +75,4 @@ interface Ref<T> {

type RootField = 'query' | 'mutation' | 'subscription';
export class Store {

@@ -83,4 +86,9 @@ records: Pessimism.Map<EntityField>;

keys: KeyingConfig;
schemaPredicates?: SchemaPredicates;
rootFields: { query: string; mutation: string; subscription: string };
rootNames: { [name: string]: RootField };
constructor(
schemaPredicates?: SchemaPredicates,
resolvers?: ResolverConfig,

@@ -94,2 +102,6 @@ updates?: Partial<UpdatesConfig>,

this.resolvers = resolvers || {};
this.optimisticMutations = optimisticMutations || {};
this.keys = keys || {};
this.schemaPredicates = schemaPredicates;
this.updates = {

@@ -99,13 +111,65 @@ Mutation: (updates && updates.Mutation) || {},

} as UpdatesConfig;
this.optimisticMutations = optimisticMutations || {};
this.keys = keys || {};
if (schemaPredicates) {
const { schema } = schemaPredicates;
const queryType = schema.getQueryType();
const mutationType = schema.getMutationType();
const subscriptionType = schema.getSubscriptionType();
const queryName = queryType ? queryType.name : 'Query';
const mutationName = mutationType ? mutationType.name : 'Mutation';
const subscriptionName = subscriptionType
? subscriptionType.name
: 'Subscription';
this.rootFields = {
query: queryName,
mutation: mutationName,
subscription: subscriptionName,
};
this.rootNames = {
[queryName]: 'query',
[mutationName]: 'mutation',
[subscriptionName]: 'subscription',
};
} else {
this.rootFields = {
query: 'Query',
mutation: 'Mutation',
subscription: 'Subscription',
};
this.rootNames = {
Query: 'query',
Mutation: 'mutation',
Subscription: 'subscription',
};
}
}
getRootKey(name: RootField) {
return this.rootFields[name];
}
keyOfEntity(data: Data) {
const { __typename: typename } = data;
if (typename !== undefined && this.keys[typename] !== undefined) {
return this.keys[typename](data);
const { __typename: typename, id, _id } = data;
if (!typename) {
return null;
} else if (this.rootNames[typename]) {
return this.rootNames[typename];
}
let key;
if (this.keys[typename]) {
key = `${this.keys[typename](data)}`;
} else if (id !== undefined && id !== null) {
key = `${id}`;
} else if (_id !== undefined && _id !== null) {
key = `${_id}`;
} else {
return keyOfEntity(data);
return null;
}
return key ? `${typename}:${key}` : null;
}

@@ -199,5 +263,3 @@

): void {
const { data, completeness } = startQuery(this, ctx);
const input = completeness === 'EMPTY' ? null : data;
const output = updater(input);
const output = updater(read(this, ctx).data);
if (output !== null) {

@@ -204,0 +266,0 @@ startWrite(this, ctx, output);

@@ -46,3 +46,3 @@ import gql from 'graphql-tag';

it('passes the "getting-started" example', () => {
const store = new Store();
const store = new Store(undefined);
const todosData = {

@@ -66,3 +66,3 @@ __typename: 'Query',

expect(queryRes.dependencies).toEqual(writeRes.dependencies);
expect(queryRes.completeness).toBe('FULL');
expect(queryRes.partial).toBe(false);

@@ -87,3 +87,3 @@ const mutatedTodo = {

expect(queryRes.completeness).toBe('FULL');
expect(queryRes.partial).toBe(false);
expect(queryRes.data).toEqual({

@@ -115,3 +115,3 @@ ...todosData,

expect(queryRes.completeness).toBe('FULL');
expect(queryRes.partial).toBe(false);
expect(queryRes.data).toEqual({

@@ -124,3 +124,5 @@ ...todosData,

it('respects property-level resolvers when given', () => {
const store = new Store({ Todo: { text: () => 'hi' } });
const store = new Store(undefined, {
Todo: { text: () => 'hi' },
});
const todosData = {

@@ -151,3 +153,3 @@ __typename: 'Query',

expect(queryRes.dependencies).toEqual(writeRes.dependencies);
expect(queryRes.completeness).toBe('FULL');
expect(queryRes.partial).toBe(false);

@@ -172,3 +174,3 @@ const mutatedTodo = {

expect(queryRes.completeness).toBe('FULL');
expect(queryRes.partial).toBe(false);
expect(queryRes.data).toEqual({

@@ -185,3 +187,3 @@ ...todosData,

it('Respects property-level resolvers when given', () => {
const store = new Store(undefined, {
const store = new Store(undefined, undefined, {
Mutation: {

@@ -257,3 +259,3 @@ toggleTodo: function toggleTodo(result, _, cache) {

expect(queryRes.completeness).toBe('FULL');
expect(queryRes.partial).toBe(false);
expect(queryRes.data).toEqual({

@@ -260,0 +262,0 @@ ...todosData,

@@ -17,4 +17,5 @@ import { DocumentNode } from 'graphql';

const queryRes = query(store, request);
expect(queryRes.data).not.toBe(null);
expect(queryRes.data).toEqual(testcase.data);
expect(queryRes.completeness).toBe('FULL');
expect(queryRes.partial).toBe(false);
expect(queryRes.dependencies).toEqual(writeRes.dependencies);

@@ -21,0 +22,0 @@ };

@@ -100,4 +100,1 @@ import { DocumentNode, FragmentDefinitionNode, SelectionNode } from 'graphql';

}
// Completeness of the query result
export type Completeness = 'EMPTY' | 'PARTIAL' | 'FULL';

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