Last active
June 19, 2018 04:12
-
-
Save paulcpk/6dbf7ba856dd2b80335520f8f348a108 to your computer and use it in GitHub Desktop.
This is a custom ApolloLink which we use to clean the "__typename" field to prevent sending it to the GraphQL server. omitDeep based on this gist: https://gist.github.com/Billy-/d94b65998501736bfe6521eadc1ab538
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
import { ApolloLink } from 'apollo-link'; | |
export class CleanTypenameLink extends ApolloLink { | |
request (operation, forward) { | |
if (operation.variables) { | |
operation.variables = omitDeep(operation.variables, '__typename'); | |
} | |
return forward(operation); | |
} | |
} | |
export const omitDeep = (obj, key) => { | |
const keys = Object.keys(obj) | |
const newObj = {} | |
keys.forEach((i) => { | |
if (i !== key) { | |
const val = obj[i] | |
if (Array.isArray(val)) newObj[i] = omitDeepArrayWalk(val, key) | |
else if (typeof val === 'object' && val !== null) newObj[i] = omitDeep(val, key) | |
else newObj[i] = val | |
} | |
}) | |
return newObj | |
} | |
export const omitDeepArrayWalk = (arr, key) => { | |
return arr.map((val) => { | |
if (Array.isArray(val)) return omitDeepArrayWalk(val, key) | |
else if (typeof val === 'object') return omitDeep(val, key) | |
return val | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment