Better API for slot fragments

This commit is contained in:
Chi Vinh Le
2017-04-26 00:44:02 +07:00
parent 15d410ba12
commit f79af56468
2 changed files with 18 additions and 7 deletions
@@ -9,9 +9,9 @@ export default withFragments({
root: gql`
fragment Comment_root on RootQuery {
__typename
${pluginFragments.root && pluginFragments.root.names}
${pluginFragments.spreads('root')}
}
${pluginFragments.root && pluginFragments.root.definitions}
${pluginFragments.definitions('root')}
`,
comment: gql`
fragment Comment_comment on Comment {
@@ -33,8 +33,8 @@ export default withFragments({
id
}
}
${pluginFragments.comment && pluginFragments.comment.names}
${pluginFragments.spreads('comment')}
}
${pluginFragments.comment && pluginFragments.comment.definitions}
${pluginFragments.definitions('comment')}
`,
})(Comment);
+14 -3
View File
@@ -30,15 +30,18 @@ function getComponentFragments(components) {
.reduce((res, fragments) => {
Object.keys(fragments).forEach(key => {
if (!(key in res)) {
res[key] = {names: '', definitions: ''};
res[key] = {spreads: '', definitions: ''};
}
res[key].names += `...${fragments[key].definitions[0].name.value}\n`;
res[key].spreads += `...${fragments[key].definitions[0].name.value}\n`;
res[key].definitions = gql`${res[key].definitions}${fragments[key]}`;
});
return res;
}, {});
}
/**
* @returns {[key]: {spreads: string, definitions: AST}}
*/
export function getSlotsFragments(slots) {
if (!Array.isArray(slots)) {
slots = [slots];
@@ -49,6 +52,14 @@ export function getSlotsFragments(slots) {
.map(o => o.module.slots[slot]);
})));
return getComponentFragments(components);
const fragments = getComponentFragments(components);
return {
spreads(key) {
return fragments[key] && fragments[key].spreads;
},
definitions(key) {
return fragments[key] && fragments[key].definitions;
},
};
}