-
-
Save stephenasamoah/e1194a193f8504a4bbaf53ae95149668 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
// METHOD #1 - More performant/preferred | |
/** | |
* Apollo Terminating link | |
* The split will skip Batching if an | |
* operation's context contains hasUpload: true | |
*/ | |
const httpLink = ApolloLink.split( | |
operation => operation.getContext().hasUpload, | |
createUploadLink(OPTS), | |
new BatchHttpLink(OPTS) | |
) | |
/** | |
* Then within a mutation component, specify the context | |
* with hasUpload to activate the Upload link. | |
*/ | |
const { data } = await uploadMutation({ | |
variables: { file }, | |
context: { | |
hasUpload: true, // activate Upload link | |
} | |
}) | |
// METHOD #2 - Doesn't require dev interaction, but less performant. | |
const isObject = node => typeof node === 'object' && node !== null | |
const hasFiles = (node, isRecursive) => { | |
if (!isRecursive) { | |
if (typeof File === undefined || typeof Blob === undefined) return false | |
} | |
const keys = Object.keys(node) | |
let i = 0 | |
let exit | |
let found | |
try { | |
while (!found && !exit && i < keys.length) { | |
if (!isObject(node[keys[i]])) { | |
i++ | |
} else if (node[keys[i]] instanceof File || node[keys[i]] instanceof Blob) { | |
found = true | |
} else if (hasFiles(node[keys[i]], true)) { | |
found = true | |
} else { | |
i++ | |
} | |
} | |
} catch (e) { | |
exit = true | |
} | |
return found | |
} | |
const httpLink = ApolloLink.split( | |
({ variables }) => hasFiles(variables), | |
createUploadLink(UPLOAD_OPTS), | |
ApolloLink.from([createOmitTypenameLink(), new BatchHttpLink(BATCH_OPTS)]) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment