Skip to content

Instantly share code, notes, and snippets.

@ShockiTV
Last active December 4, 2017 10:10
Show Gist options
  • Save ShockiTV/6575161d97a38e827d8f4f9b0cd61119 to your computer and use it in GitHub Desktop.
Save ShockiTV/6575161d97a38e827d8f4f9b0cd61119 to your computer and use it in GitHub Desktop.
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