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

move from string ref to callback ref #908

Merged
merged 4 commits into from
Aug 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change log

### vNext
- Replace string refs with callback refs [#908](https://github.com/apollographql/react-apollo/pull/908)

### 1.4.9
- Fix: fix matching types with exports for flow and ts
Expand Down
14 changes: 11 additions & 3 deletions src/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,15 @@ export default function graphql<
// the element to render
private renderedElement: any;

// wrapped instance
private wrappedInstance: any;

constructor(props, context) {
super(props, context);
this.version = version;
this.type = operation.type;
this.dataForChildViaMutation = this.dataForChildViaMutation.bind(this);
this.setWrappedInstance = this.setWrappedInstance.bind(this);
}

componentWillMount() {
Expand Down Expand Up @@ -481,7 +485,11 @@ export default function graphql<
`{ withRef: true } in the options`,
);

return (this.refs as any).wrappedInstance;
return this.wrappedInstance;
}

setWrappedInstance(ref) {
this.wrappedInstance = ref;
}

dataForChildViaMutation(mutationOpts: MutationOpts) {
Expand Down Expand Up @@ -563,7 +571,7 @@ export default function graphql<
if (operationOptions.withRef) {
return createElement(
WrappedComponent,
assign({}, this.props, { ref: 'wrappedInstance' }),
assign({}, this.props, { ref: this.setWrappedInstance }),
);
}
return createElement(WrappedComponent, this.props);
Expand All @@ -585,7 +593,7 @@ export default function graphql<
const mergedPropsAndData = assign({}, props, clientProps);

if (operationOptions.withRef)
mergedPropsAndData.ref = 'wrappedInstance';
mergedPropsAndData.ref = this.setWrappedInstance;
this.renderedElement = createElement(
WrappedComponent,
mergedPropsAndData,
Expand Down
12 changes: 10 additions & 2 deletions src/withApollo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ export function withApollo<TProps, TResult>(
// data storage
private client: ApolloClient; // apollo client

// wrapped instance
private wrappedInstance: any;

constructor(props, context) {
super(props, context);
this.client = context.client;
this.setWrappedInstance = this.setWrappedInstance.bind(this);

invariant(
!!this.client,
Expand All @@ -70,13 +74,17 @@ export function withApollo<TProps, TResult>(
`{ withRef: true } in the options`,
);

return (this.refs as any).wrappedInstance;
return this.wrappedInstance;
}

setWrappedInstance(ref) {
this.wrappedInstance = ref;
}

render() {
const props = assign({}, this.props);
props.client = this.client;
if (operationOptions.withRef) props.ref = 'wrappedInstance';
if (operationOptions.withRef) props.ref = this.setWrappedInstance;
return createElement(WrappedComponent, props);
}
}
Expand Down
20 changes: 8 additions & 12 deletions test/react-web/client/graphql/shared-operations.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ describe('shared operations', () => {
expect((decorated as any).getWrappedInstance().someMethod()).toEqual(
testData,
);
expect((decorated as any).refs.wrappedInstance.someMethod()).toEqual(
testData,
);
expect((decorated as any).wrappedInstance.someMethod()).toEqual(testData);

const DecoratedWithSkip = withApollo(Container, {
withRef: true,
Expand All @@ -88,9 +86,9 @@ describe('shared operations', () => {
expect(
(decoratedWithSkip as any).getWrappedInstance().someMethod(),
).toEqual(testData);
expect(
(decoratedWithSkip as any).refs.wrappedInstance.someMethod(),
).toEqual(testData);
expect((decoratedWithSkip as any).wrappedInstance.someMethod()).toEqual(
testData,
);
});
});

Expand Down Expand Up @@ -301,9 +299,7 @@ describe('shared operations', () => {
expect((decorated as any).getWrappedInstance().someMethod()).toEqual(
testData,
);
expect((decorated as any).refs.wrappedInstance.someMethod()).toEqual(
testData,
);
expect((decorated as any).wrappedInstance.someMethod()).toEqual(testData);

const DecoratedWithSkip = graphql(query, { withRef: true, skip: true })(
Container,
Expand All @@ -324,9 +320,9 @@ describe('shared operations', () => {
expect(
(decoratedWithSkip as any).getWrappedInstance().someMethod(),
).toEqual(testData);
expect(
(decoratedWithSkip as any).refs.wrappedInstance.someMethod(),
).toEqual(testData);
expect((decoratedWithSkip as any).wrappedInstance.someMethod()).toEqual(
testData,
);
});

it('allows options to take an object', done => {
Expand Down