Last active
December 4, 2017 10:10
-
-
Save ShockiTV/6575161d97a38e827d8f4f9b0cd61119 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { check } from 'graphql-anywhere'; | |
function PropTypeError(message) { | |
this.message = message; | |
this.stack = ''; | |
} | |
// Make `instanceof Error` still work for returned errors. | |
PropTypeError.prototype = Error.prototype; | |
export const apolloDataType = apolloObject => { | |
const returnFunction = (props, propName, componentName) => { | |
if (!props[propName] || !Object.prototype.hasOwnProperty.call(props[propName], "loading")) { | |
return new PropTypeError(`No loading prop found in component ${componentName}`); | |
} | |
if (!props[propName].loading) { | |
try { | |
check(apolloObject, props[propName]); | |
return null; | |
} catch (e) { | |
return e; | |
} | |
} | |
return null; | |
}; | |
// hack so we can call .isRequired | |
// to not encounter eslint react/require-default-props | |
returnFunction.isRequired = returnFunction; | |
return returnFunction; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment