Created
October 17, 2024 00:50
-
-
Save colonelpanic8/3d99323348fcac77ca213c2412fb4f8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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