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

Commit

Permalink
Merge pull request #31 from apollostack/remove-result-key
Browse files Browse the repository at this point in the history
remove result in favor of the key's returned by the query
  • Loading branch information
Sashko Stubailo committed Apr 26, 2016
2 parents 071c75c + 9c7f8ca commit 6bc5027
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
34 changes: 33 additions & 1 deletion Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,41 @@

Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 1 or 2 months), so that we can take advantage of SemVer to signify breaking changes from that point on.

### v0.2.0

**Breaking change:**

Remove `result` key in favor of dynamic key matching root fields of the query or mutation. (https://github.com/apollostack/react-apollo/pull/31)

```js
{
loading: false,
result: {
posts: []
}
}
```

becomes

```js
{
loading: false,
posts: []
}
```

### v0.1.5

Get state directly from redux store internally

### v0.1.4

Fix bug with willReceiveProps

### v0.1.2

Adjust loading lifecycle marker to better match the behavoir of apollo-client (https://github.com/apollostack/react-apollo/pull/11)
Adjust loading lifecycle marker to better match the behavior of apollo-client (https://github.com/apollostack/react-apollo/pull/11)

### v0.1.1

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-apollo",
"version": "0.1.6",
"version": "0.2.0",
"description": "React data container for Apollo Client",
"main": "./lib/src/index.js",
"scripts": {
Expand Down
46 changes: 33 additions & 13 deletions src/connect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const defaultMapMutationsToProps = opts => ({ });
const defaultQueryData = {
loading: true,
errors: null,
result: null,
};
const defaultMutationData = assign({}, defaultQueryData);

Expand Down Expand Up @@ -87,7 +86,7 @@ export default function connect(opts?: ConnectOptions) {
{
loading: boolean,
errors: Errors,
result: GraphQLResult,
[key: String]: result
}
*/
Expand Down Expand Up @@ -214,11 +213,10 @@ export default function connect(opts?: ConnectOptions) {
variables,
});

queryData = {
queryData = assign({
errors: null,
loading: false,
result,
};
}, result);
} catch (e) {/* tslint */}

this.data[key] = queryData;
Expand Down Expand Up @@ -262,13 +260,24 @@ export default function connect(opts?: ConnectOptions) {
};
};

const forceRender = ({ errors, data }: any) => {
this.data[key] = {
const forceRender = ({ errors, data = {} }: any) => {
const resultKeyConflict: boolean = (
'errors' in data ||
'loading' in data ||
'refetch' in data
);

invariant(!resultKeyConflict,
`the result of the '${key}' query contains keys that ` +
`conflict with the return object. 'errors', 'loading', and 'refetch' cannot be ` +
`returned keys`
);

this.data[key] = assign({
loading: false,
result: data || null,
errors,
refetch: refetch, // copy over refetch method
};
}, data);

this.hasQueryDataChanged = true;

Expand Down Expand Up @@ -318,12 +327,23 @@ export default function connect(opts?: ConnectOptions) {

// middleware to update the props to send data to wrapped component
// when the mutation is done
const forceRender = ({ errors, data }: GraphQLResult): GraphQLResult => {
this.data[key] = {
const forceRender = ({ errors, data = {} }: GraphQLResult): GraphQLResult => {
const resultKeyConflict: boolean = (
'errors' in data ||
'loading' in data ||
'refetch' in data
);

invariant(!resultKeyConflict,
`the result of the '${key}' mutation contains keys that ` +
`conflict with the return object. 'errors', 'loading', and 'refetch' cannot be ` +
`returned keys`
);

this.data[key] = assign({
loading: false,
result: data,
errors,
};
}, data);

this.hasMutationDataChanged = true;

Expand Down
14 changes: 7 additions & 7 deletions test/connect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ describe('connect', () => {

if (this.props.people.loading) {
expect(this.props.people.loading).to.be.true;
expect(this.props.people.result).to.exist;
expect(this.props.people.allPeople).to.exist;
done();
}

Expand Down Expand Up @@ -1050,7 +1050,7 @@ describe('connect', () => {
class Container extends React.Component<any, any> {
componentDidUpdate(prevProps) {
expect(prevProps.luke.loading).to.be.true;
expect(this.props.luke.result).to.deep.equal(data);
expect(this.props.luke.luke).to.deep.equal(data.luke);
done();
}
render() {
Expand Down Expand Up @@ -1131,7 +1131,7 @@ describe('connect', () => {

expect(props.people).to.exist;
expect(props.people.loading).to.be.false;
expect(props.people.result).to.deep.equal(data);
expect(props.people.allPeople).to.deep.equal(data.allPeople);
done();
});
});
Expand Down Expand Up @@ -1435,7 +1435,7 @@ describe('connect', () => {
// wait until finished loading
if (!this.props.makeListPrivate.loading) {
expect(prevProps.makeListPrivate.loading).to.be.true;
expect(this.props.makeListPrivate.result).to.deep.equal(data);
expect(this.props.makeListPrivate.makeListPrivate).to.be.true;
done();
}
}
Expand Down Expand Up @@ -1506,7 +1506,7 @@ describe('connect', () => {
componentDidUpdate(prevProps) {
if (!this.props.makeListPrivate.loading) {
expect(prevProps.makeListPrivate.loading).to.be.true;
expect(this.props.makeListPrivate.result).to.deep.equal(data);
expect(this.props.makeListPrivate.makeListPrivate).to.be.true;
done();
}
}
Expand Down Expand Up @@ -1577,7 +1577,7 @@ describe('connect', () => {
componentDidUpdate(prevProps) {
if (!this.props.makeListPrivate.loading) {
expect(prevProps.makeListPrivate.loading).to.be.true;
expect(this.props.makeListPrivate.result).to.deep.equal(data);
expect(this.props.makeListPrivate.makeListPrivate).to.be.true;
done();
}
}
Expand Down Expand Up @@ -1644,7 +1644,7 @@ describe('connect', () => {
componentDidUpdate(prevProps) {
if (!this.props.makeListPrivate.loading) {
expect(prevProps.makeListPrivate.loading).to.be.true;
expect(this.props.makeListPrivate.result).to.deep.equal(data);
expect(this.props.makeListPrivate.makeListPrivate).to.be.true;
done();
}
}
Expand Down

0 comments on commit 6bc5027

Please sign in to comment.