Skip to content

Instantly share code, notes, and snippets.

@colonelpanic8
Created October 17, 2024 00:50
Show Gist options
  • Save colonelpanic8/3d99323348fcac77ca213c2412fb4f8d to your computer and use it in GitHub Desktop.
Save colonelpanic8/3d99323348fcac77ca213c2412fb4f8d to your computer and use it in GitHub Desktop.
function computeClientFieldWith<F, T>(
query: string,
getVariables: (field: F) => { [key: string]: any },
getValue: (field: F, fetchedValue: T) => any,
) {
return async (field: F, _args: any, { cache, client }) => {
const variables = getVariables(field);
try {
var data = cache.readQuery({
query,
variables,
});
} catch (e) {
// Cache miss, proceed to a network query
}
try {
var { data } = await client.query({
query,
variables,
fetchPolicy: "network-only",
});
} catch (e) {
// Retrieval error
}
return getValue(field, data);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment