Created
February 24, 2023 11:30
-
-
Save sibelius/5ed15d3faa2efa908f7fd705f59a2e10 to your computer and use it in GitHub Desktop.
missingFieldHandler
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 { ROOT_TYPE } from 'relay-runtime'; | |
import type { MissingFieldHandler } from 'relay-runtime/lib/store/RelayStoreTypes'; | |
export const missingScalarDebug = { | |
kind: 'scalar', | |
handle(field, record, args, store) { | |
// eslint-disable-next-line | |
console.log('missing scalar', { | |
field, | |
record, | |
args, | |
store, | |
}); | |
return undefined; | |
}, | |
}; | |
export const missingLinkedDebug = { | |
kind: 'linked', | |
handle(field, record, args, store) { | |
if ( | |
record != null && | |
record.__typename === ROOT_TYPE && | |
field.name === 'node' && | |
args.id | |
) { | |
// eslint-disable-next-line | |
console.log('missing node', { | |
field, | |
record, | |
args, | |
store, | |
}); | |
return args.id | |
} | |
// eslint-disable-next-line | |
console.log('missing linked', { | |
field, | |
record, | |
args, | |
store, | |
}); | |
return undefined; | |
}, | |
}; | |
export const missingPluralDebug = { | |
kind: 'pluralLinked', | |
handle(field, record, args, store) { | |
return undefined; | |
}, | |
}; | |
export const missingLinked = { | |
kind: 'linked', | |
handle(field, record, args, store) { | |
if ( | |
record != null && | |
record.__typename === ROOT_TYPE && | |
field.name === 'node' && | |
args.id | |
) { | |
// eslint-disable-next-line | |
console.log('missing node', { | |
field, | |
record, | |
args, | |
store, | |
}); | |
return args.id | |
} | |
return undefined; | |
}, | |
}; | |
export const missingFieldHandlersDebug: ReadonlyArray<MissingFieldHandler> = [ | |
missingScalarDebug, | |
missingLinkedDebug, | |
missingPluralDebug, | |
]; | |
export const missingFieldHandlers: ReadonlyArray<MissingFieldHandler> = [ | |
missingLinked, | |
]; | |
const IS_SERVER = typeof window === typeof undefined; | |
const isDev = process.env.NODE_ENV === 'development'; | |
const CLIENT_DEBUG = false; | |
const SERVER_DEBUG = false; | |
const showLog = isDev && ((IS_SERVER && SERVER_DEBUG) || (!IS_SERVER && CLIENT_DEBUG)); | |
export const getMissingFieldHandlers = () => { | |
if (showLog) { | |
return { | |
missingFieldHandlers: missingFieldHandlersDebug, | |
}; | |
} | |
return { | |
missingFieldHandlers, | |
}; | |
}; |
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
const env = new Environment({ | |
network, | |
store, | |
...getMissingFieldHandlers() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment