@apollo/client
Version:
A fully-featured caching GraphQL client.
82 lines • 3.48 kB
JavaScript
import { isReference } from "@apollo/client/utilities";
import { compact, createFragmentMap, DeepMerger, getFragmentDefinitions, isArray, isField, isNonNullObject, resultKeyNameFromField, shouldInclude, } from "@apollo/client/utilities/internal";
export const { hasOwnProperty: hasOwn } = Object.prototype;
export function defaultDataIdFromObject({ __typename, id, _id }, context) {
if (typeof __typename === "string") {
if (context) {
context.keyObject =
id != null ? { id }
: _id != null ? { _id }
: void 0;
}
// If there is no object.id, fall back to object._id.
if (id == null && _id != null) {
id = _id;
}
if (id != null) {
return `${__typename}:${typeof id === "number" || typeof id === "string" ?
id
: JSON.stringify(id)}`;
}
}
}
const defaultConfig = {
dataIdFromObject: defaultDataIdFromObject,
resultCaching: true,
};
export function normalizeConfig(config) {
return compact(defaultConfig, config);
}
export function getTypenameFromStoreObject(store, objectOrReference) {
return isReference(objectOrReference) ?
store.get(objectOrReference.__ref, "__typename")
: objectOrReference && objectOrReference.__typename;
}
export const TypeOrFieldNameRegExp = /^[_a-z][_0-9a-z]*/i;
export function fieldNameFromStoreName(storeFieldName) {
const match = storeFieldName.match(TypeOrFieldNameRegExp);
return match ? match[0] : storeFieldName;
}
export function selectionSetMatchesResult(selectionSet, result, variables) {
if (isNonNullObject(result)) {
return isArray(result) ?
result.every((item) => selectionSetMatchesResult(selectionSet, item, variables))
: selectionSet.selections.every((field) => {
if (isField(field) && shouldInclude(field, variables)) {
const key = resultKeyNameFromField(field);
return (hasOwn.call(result, key) &&
(!field.selectionSet ||
selectionSetMatchesResult(field.selectionSet, result[key], variables)));
}
// If the selection has been skipped with @skip(true) or
// @include(false), it should not count against the matching. If
// the selection is not a field, it must be a fragment (inline or
// named). We will determine if selectionSetMatchesResult for that
// fragment when we get to it, so for now we return true.
return true;
});
}
return false;
}
export function storeValueIsStoreObject(value) {
return isNonNullObject(value) && !isReference(value) && !isArray(value);
}
export function makeProcessedFieldsMerger() {
return new DeepMerger();
}
export function extractFragmentContext(document, fragments) {
// FragmentMap consisting only of fragments defined directly in document, not
// including other fragments registered in the FragmentRegistry.
const fragmentMap = createFragmentMap(getFragmentDefinitions(document));
return {
fragmentMap,
lookupFragment(name) {
let def = fragmentMap[name];
if (!def && fragments) {
def = fragments.lookup(name);
}
return def || null;
},
};
}
//# sourceMappingURL=helpers.js.map