fuse
Version:
The magical GraphQL framework
94 lines (90 loc) • 4.39 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
// src/client.ts
import { createClient as create, fetchExchange } from "urql";
// src/exchanges/cache.ts
import { cacheExchange as urqlCacheExchange } from "@urql/core";
import { pipe, tap } from "wonka";
var cacheExchange = ({ forward, client, dispatchDebug }) => {
const cache$ = urqlCacheExchange({ client, dispatchDebug, forward });
const warned = /* @__PURE__ */ new Set();
return (ops$) => {
return pipe(
cache$(ops$),
tap((result) => {
if (false)
return;
if (warned.has(result.operation.key))
return;
if (result.operation.kind === "mutation" && !result.operation.context.additionalTypenames) {
Object.entries(result.data).forEach(([key, value]) => {
if (value != null && typeof value !== "object") {
warned.add(result.operation.key);
console.warn(
`Saw return of type "${typeof value}" for "${key}", returning scalar values can lead to stale cache-entires. Consider using "additionalTypenames" to correctly evict cache-entries affected by this mutation.`
);
}
});
} else if (result.operation.kind === "query" && !result.operation.context.additionalTypenames) {
Object.entries(result.data).forEach(([key, value]) => {
if (value == null) {
warned.add(result.operation.key);
console.warn(
`Saw return value of "null" for "${key}", we aren't able to derive an associated type for this key, this means that a mutation won't be able to evict this query. Consider using "additionalTypenames" to correctly evict this cache-entry.`
);
} else if (Array.isArray(value) && value.length === 0) {
warned.add(result.operation.key);
console.warn(
`Saw an empty array for "${key}", we aren't able to derive an associated type for members of this list, this means that a mutation won't be able to evict this query. Consider using "additionalTypenames" to correctly evict this cache-entry.`
);
} else if (value != null && typeof value === "object") {
const data = value;
if (data.nodes && Array.isArray(data.nodes) && data.nodes.length === 0) {
warned.add(result.operation.key);
console.warn(
`Saw an empty array for "${key}.nodes", we aren't able to derive an associated type for members of this list, this means that a mutation won't be able to evict this query. Consider using "additionalTypenames" to correctly evict this cache-entry.`
);
} else if (data.edges && Array.isArray(data.edges) && data.edges.length === 0) {
warned.add(result.operation.key);
console.warn(
`Saw an empty array for "${key}.edges", we aren't able to derive an associated type for members of this list, this means that a mutation won't be able to evict this query. Consider using "additionalTypenames" to correctly evict this cache-entry.`
);
}
}
});
}
})
);
};
};
// src/client.ts
export * from "urql";
var createClient = (opts) => {
var _a;
const options = __spreadProps(__spreadValues({}, opts), {
suspense: (_a = opts.suspense) != null ? _a : true,
exchanges: opts.exchanges || [cacheExchange, fetchExchange]
});
return create(options);
};
export {
cacheExchange,
createClient
};