2017-02-02 11:56:52 +09:00
|
|
|
import gql from 'graphql-tag';
|
|
|
|
|
2017-03-23 16:27:59 +09:00
|
|
|
export const Fragments = {}; // will be populated on startup (see vulcan:routing)
|
2017-01-30 19:46:48 +09:00
|
|
|
|
2017-06-17 15:24:53 +09:00
|
|
|
export const FragmentsExtensions = {}; // will be used on startup
|
|
|
|
|
2017-02-02 11:56:52 +09:00
|
|
|
export const registerFragment = fragmentText => {
|
2017-02-02 13:24:29 +09:00
|
|
|
|
2017-02-02 11:56:52 +09:00
|
|
|
// extract name from fragment text
|
|
|
|
const fragmentName = fragmentText.match(/fragment (.*) on/)[1];
|
2017-02-02 13:24:29 +09:00
|
|
|
|
2017-02-02 11:56:52 +09:00
|
|
|
// extract subFragments from text
|
|
|
|
const matchedSubFragments = fragmentText.match(/\.\.\.(.*)/g) || [];
|
|
|
|
const subFragments = _.unique(matchedSubFragments.map(f => f.replace('...', '')));
|
|
|
|
|
2017-02-02 13:24:29 +09:00
|
|
|
// console.log('// registerFragment: ', fragmentName, subFragments)
|
2017-02-02 11:56:52 +09:00
|
|
|
|
|
|
|
// register fragment
|
|
|
|
Fragments[fragmentName] = {
|
|
|
|
fragmentText,
|
|
|
|
subFragments
|
|
|
|
}
|
2017-01-30 19:46:48 +09:00
|
|
|
};
|
|
|
|
|
2017-02-02 13:24:29 +09:00
|
|
|
// extend a fragment with additional properties
|
|
|
|
export const extendFragment = (fragmentName, newProperties) => {
|
2017-06-17 15:24:53 +09:00
|
|
|
FragmentsExtensions[fragmentName] = newProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform fragment extension
|
|
|
|
export const extendFragmentWithProperties = (fragmentName, newProperties) => {
|
2017-02-02 13:24:29 +09:00
|
|
|
const fragment = Fragments[fragmentName];
|
|
|
|
const fragmentEndPosition = fragment.fragmentText.lastIndexOf('}');
|
|
|
|
const newFragmentText =[fragment.fragmentText.slice(0, fragmentEndPosition), newProperties, fragment.fragmentText.slice(fragmentEndPosition)].join('');
|
|
|
|
registerFragment(newFragmentText);
|
|
|
|
}
|
|
|
|
|
2017-05-14 11:08:07 +09:00
|
|
|
// remove a property from a fragment
|
|
|
|
export const removeFromFragment = (fragmentName, propertyName) => {
|
|
|
|
const fragment = Fragments[fragmentName];
|
|
|
|
const newFragmentText = fragment.fragmentText.replace(propertyName, '');
|
|
|
|
registerFragment(newFragmentText);
|
|
|
|
}
|
|
|
|
|
2017-06-20 10:25:34 +09:00
|
|
|
// get fragment name from fragment object
|
|
|
|
export const getFragmentName = fragment => fragment && fragment.definitions[0] && fragment.definitions[0].name.value;
|
|
|
|
|
2017-06-19 10:19:05 +09:00
|
|
|
// get actual gql fragment
|
|
|
|
export const getFragment = fragmentName => {
|
2017-01-30 19:46:48 +09:00
|
|
|
|
2017-06-19 10:19:05 +09:00
|
|
|
// get entire fragment as stored
|
2017-02-02 13:24:29 +09:00
|
|
|
const fragment = Fragments[fragmentName];
|
|
|
|
|
2017-02-17 20:45:11 +09:00
|
|
|
if (!fragment) {
|
|
|
|
throw new Error(`Fragment "${fragmentName}" not registered.`)
|
|
|
|
}
|
2017-06-19 10:19:05 +09:00
|
|
|
if (!fragment.fragmentObject) {
|
2017-06-20 10:25:34 +09:00
|
|
|
console.log('// !fragment.fragmentObject: '+fragmentName)
|
|
|
|
initializeFragment(fragmentName);
|
|
|
|
// throw new Error(`Fragment "${fragmentName}" not initialized.`)
|
2017-06-19 10:19:05 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// return fragment object created by gql
|
|
|
|
return fragment.fragmentObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create gql fragment from fragment name, text, and subfragments
|
|
|
|
export const initializeFragment = (fragmentName) => {
|
|
|
|
|
|
|
|
const fragmentItem = Fragments[fragmentName];
|
2017-02-17 20:45:11 +09:00
|
|
|
|
2017-02-02 13:24:29 +09:00
|
|
|
// pad the literals array with line returns for each subFragments
|
2017-06-19 10:19:05 +09:00
|
|
|
const literals = [fragmentItem.fragmentText, ...fragmentItem.subFragments.map(x => '\n')];
|
2017-02-02 13:24:29 +09:00
|
|
|
|
|
|
|
// the gql function expects an array of literals as first argument, and then sub-fragments as other arguments
|
2017-06-19 10:19:05 +09:00
|
|
|
const gqlArguments = [literals, ...fragmentItem.subFragments.map(subFragmentName => {
|
|
|
|
// if subfragment hasn't been initialized yet, do it now
|
|
|
|
if (!(Fragments[subFragmentName] && Fragments[subFragmentName].fragmentObject)) {
|
|
|
|
initializeFragment(subFragmentName);
|
|
|
|
}
|
|
|
|
// return subfragment's gql fragment
|
|
|
|
return Fragments[subFragmentName].fragmentObject;
|
|
|
|
})];
|
2017-02-02 13:24:29 +09:00
|
|
|
|
2017-06-19 10:19:05 +09:00
|
|
|
fragmentItem.fragmentObject = gql.apply(null, gqlArguments);
|
2017-06-17 15:24:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export const initializeFragments = () => {
|
2017-06-22 16:19:23 +09:00
|
|
|
// extend fragment text if fragment exists
|
2017-06-17 15:24:53 +09:00
|
|
|
_.forEach(FragmentsExtensions, (newProperties, fragmentName) => {
|
2017-06-22 16:19:23 +09:00
|
|
|
if (Fragments[fragmentName]) {
|
|
|
|
extendFragmentWithProperties(fragmentName, newProperties);
|
|
|
|
}
|
2017-06-17 15:24:53 +09:00
|
|
|
});
|
2017-06-19 10:19:05 +09:00
|
|
|
// initialize fragments to create gql fragment object
|
|
|
|
_.forEach(Fragments, (fragmentItem, fragmentName) => {
|
|
|
|
initializeFragment(fragmentName);
|
|
|
|
})
|
2017-01-30 19:46:48 +09:00
|
|
|
}
|