@graphql-mesh/transport-rest
Version:
26 lines (25 loc) • 941 B
JavaScript
import { isListType, isNonNullType } from 'graphql';
function isOriginallyListType(type) {
if (isNonNullType(type)) {
return isOriginallyListType(type.ofType);
}
return isListType(type);
}
export function processResolveRootFieldAnnotations(field, propertyName) {
if (!field.resolve || field.resolve.name === 'defaultFieldResolver') {
field.resolve = (root, args, context, info) => {
const actualFieldObj = root[propertyName];
if (actualFieldObj != null) {
const isArray = Array.isArray(actualFieldObj);
const isListType = isOriginallyListType(info.returnType);
if (isListType && !isArray) {
return [actualFieldObj];
}
else if (!isListType && isArray) {
return actualFieldObj[0];
}
}
return actualFieldObj;
};
}
}