mirror of
https://github.com/wassname/talk.git
synced 2026-07-20 12:40:47 +08:00
Better API for slot fragments
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user