Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

v2.1 Implemented the Query Component #1398

Merged
merged 16 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions src/Query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,13 @@ export interface QueryProps {
skip?: Boolean;
loading?: () => React.ReactNode;
error?: (error: ApolloError) => React.ReactNode;
render?: ((result: QueryRenderProp) => React.ReactNode);
children?: ((result: QueryRenderProp) => React.ReactNode) | React.ReactNode;
children?: (result: QueryRenderProp) => React.ReactNode;
}

export interface QueryState {
result: any;
}

const isEmptyChildren = children => React.Children.count(children) === 0;

function observableQueryFields(observable) {
const fields = pick(
observable,
Expand Down Expand Up @@ -180,7 +177,7 @@ class Query extends React.Component<QueryProps, QueryState> {
};

render() {
const { render, loading, error, children } = this.props;
const { loading, error, children } = this.props;
const result = this.getRenderProps();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just put getRenderProps implementation into render. Alternatively you could keep it and rename it to getResult to be consistent.


if (result.loading && loading) {
Expand All @@ -191,18 +188,8 @@ class Query extends React.Component<QueryProps, QueryState> {
return error(result.error);
}

if (render) {
return render(result);
}

if (typeof children === 'function') {
return children(result);
}

if (children && !isEmptyChildren(children))
return React.Children.only(children);

return null;
const renderedChildren = children(result);
return renderedChildren && React.Children.only(renderedChildren);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still have to have this limitation with React 16? We could allow more than one resulting child since now React allows it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with limiting to 16+ - but I'm on the progressive side. Not sure how other users would feel about it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say yes, since this will be part of a new version, a new API. And even then, you can still use it in previous versions, only that it will fail with a less clear error when you attempt to render multiple children than the one React.Children.only gives you

}
}

Expand Down
Loading