2018-10-29 22:08:41 +01:00
|
|
|
/** Helpers to get values depending on name
|
|
|
|
* E.g. retrieving a collection and its name when only one value is provided
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { getCollection } from './collections';
|
|
|
|
import { getFragment, getFragmentName } from './fragments';
|
2018-09-05 16:00:07 +02:00
|
|
|
/**
|
|
|
|
* Extract collectionName from collection
|
|
|
|
* or collection from collectionName
|
|
|
|
* @param {*} param0
|
|
|
|
*/
|
|
|
|
export const extractCollectionInfo = ({ collectionName, collection }) => {
|
2018-09-12 11:59:00 +09:00
|
|
|
if (!(collectionName || collection)) throw new Error('Please specify either collection or collectionName');
|
2018-09-05 16:00:07 +02:00
|
|
|
const _collectionName = collectionName || collection.options.collectionName;
|
|
|
|
const _collection = collection || getCollection(collectionName);
|
|
|
|
return { collection: _collection, collectionName: _collectionName };
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Extract fragmentName from fragment
|
|
|
|
* or fragment from fragmentName
|
|
|
|
*/
|
|
|
|
export const extractFragmentInfo = ({ fragment, fragmentName }, collectionName) => {
|
|
|
|
if (!(fragment || fragmentName || collectionName))
|
2018-09-12 11:59:00 +09:00
|
|
|
throw new Error('Please specify either fragment or fragmentName, or pass a collectionName');
|
2018-09-05 16:00:07 +02:00
|
|
|
if (fragment) {
|
|
|
|
return {
|
|
|
|
fragment,
|
|
|
|
fragmentName: fragmentName || getFragmentName(fragment)
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const _fragmentName = fragmentName || `${collectionName}DefaultFragment`;
|
|
|
|
return {
|
|
|
|
fragment: getFragment(_fragmentName),
|
|
|
|
fragmentName: _fragmentName
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|