Skip to content

Commit 630bd26

Browse files
committed
feat(withFragment): withFragment HOC
1 parent 849fcc5 commit 630bd26

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/withFragment.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { compose, withProps } from 'recompose';
2+
import { withApollo } from 'react-apollo';
3+
4+
export default function withFragment(fragmentDocument, optionsObject) {
5+
const name = (optionsObject && optionsObject.name) || 'data';
6+
return compose(
7+
withApollo,
8+
withProps(({ client, ...rest }) => {
9+
const fragment = client.readFragment({
10+
fragment: fragmentDocument,
11+
id: optionsObject && optionsObject.getFragmentId(rest),
12+
});
13+
14+
return {
15+
[name]: fragment,
16+
};
17+
})
18+
);
19+
}

src/withQueryComponent.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { graphql } from 'react-apollo';
2+
3+
function RenderChild({ data, children }) {
4+
return children(data);
5+
}
6+
7+
export default function withQueryComponent(queryDocument, optionsObject) {
8+
const queryData = graphql(queryDocument, optionsObject);
9+
return queryData(RenderChild);
10+
}

0 commit comments

Comments
 (0)