Skip to content

Commit

Permalink
feat: Support Relay v2.x (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
xdissent authored and taion committed Feb 5, 2019
1 parent 7f625bb commit 97fb738
Show file tree
Hide file tree
Showing 6 changed files with 604 additions and 185 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"lint": "eslint src test",
"precommit": "lint-staged",
"prepublish": "npm run build",
"relay-compiler": "relay-compiler --src test --schema test/fixtures/schema.graphql",
"relay-compiler": "relay-compiler --watchman false --src test --schema test/fixtures/schema.graphql",
"tdd": "jest --watch",
"test": "npm run build-fixtures && npm run lint && npm run testonly -- --coverage",
"testonly": "jest --runInBand --verbose",
Expand Down Expand Up @@ -71,16 +71,16 @@
},
"peerDependencies": {
"found": ">=0.3.8",
"react": "^0.14.9 || >=15.3.0",
"react-relay": ">=1.4.0"
"react": ">=16.5.0",
"react-relay": "^2.0.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.6",
"babel-jest": "^23.6.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-dev-expression": "^0.2.1",
"babel-plugin-relay": "^1.6.2",
"babel-plugin-relay": "^2.0.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
Expand All @@ -98,18 +98,18 @@
"eslint-plugin-react": "^7.11.1",
"farce": "^0.2.6",
"found": "^0.4.0-alpha.4",
"graphql": "^0.13.2",
"graphql": "^14.1.0",
"husky": "^0.14.3",
"jest": "^23.6.0",
"lint-staged": "^7.2.2",
"p-defer": "^1.0.0",
"prettier": "^1.14.2",
"react": "^16.5.0",
"react-dom": "^16.5.0",
"react-relay": "^1.6.2",
"relay-compiler": "^1.6.2",
"react-relay": "^2.0.0",
"relay-compiler": "^2.0.0",
"relay-local-schema": "^0.7.0",
"relay-runtime": "^1.6.2",
"relay-runtime": "^2.0.0",
"rimraf": "^2.6.2"
}
}
11 changes: 2 additions & 9 deletions src/modern/QuerySubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@ export default class QuerySubscription {
this.dataFrom = dataFrom;

const {
createOperationSelector,
createOperationDescriptor,
getRequest,
getOperation,
} = this.environment.unstable_internal;

// FIXME: Use getRequest directly when only supporting relay >=1.5.0.
const getRequestOrOperation = getRequest || getOperation;

this.operation = createOperationSelector(
getRequestOrOperation(query),
variables,
);
this.operation = createOperationDescriptor(getRequest(query), variables);

this.fetchPromise = null;
this.selectionReference = null;
Expand Down
34 changes: 8 additions & 26 deletions src/modern/ReadyStateRenderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PropTypes from 'prop-types';
import elementType from 'prop-types-extra/lib/elementType';
import React from 'react';
import RelayPropTypes from 'react-relay/lib/RelayPropTypes';
import { ReactRelayContext } from 'react-relay';
import warning from 'warning';

import getQueryName from './getQueryName';
Expand All @@ -22,13 +22,9 @@ const propTypes = {
fetched: PropTypes.bool.isRequired,
};

const childContextTypes = {
relay: RelayPropTypes.Relay,
};

class ReadyStateRenderer extends React.Component {
constructor(props, context) {
super(props, context);
constructor(props) {
super(props);

const { element, querySubscription } = props;

Expand All @@ -37,15 +33,6 @@ class ReadyStateRenderer extends React.Component {
};

this.selectionReference = querySubscription.retain();

this.relayContext = {};
this.updateRelayContext(querySubscription);
}

getChildContext() {
return {
relay: this.relayContext,
};
}

componentDidMount() {
Expand All @@ -65,8 +52,6 @@ class ReadyStateRenderer extends React.Component {

this.props.querySubscription.unsubscribe(this.onUpdate);
querySubscription.subscribe(this.onUpdate);

this.updateRelayContext(querySubscription);
}
}

Expand Down Expand Up @@ -102,12 +87,6 @@ class ReadyStateRenderer extends React.Component {
this.setState({ element: element || null });
};

updateRelayContext(querySubscription) {
// XXX: Relay v1.6.0 adds an assumption that context.relay is mutated
// in-place, so we need to do that here.
Object.assign(this.relayContext, querySubscription.relayContext);
}

render() {
const { element } = this.state;
if (!element) {
Expand Down Expand Up @@ -144,11 +123,14 @@ class ReadyStateRenderer extends React.Component {
});
}

return React.cloneElement(element, ownProps);
return (
<ReactRelayContext.Provider value={querySubscription.relayContext}>
{React.cloneElement(element, ownProps)}
</ReactRelayContext.Provider>
);
}
}

ReadyStateRenderer.propTypes = propTypes;
ReadyStateRenderer.childContextTypes = childContextTypes;

export default ReadyStateRenderer;
4 changes: 0 additions & 4 deletions src/modern/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export default class Resolver {
constructor(environment) {
this.environment = environment;

this.lastQueries = [];
this.lastRouteVariables = [];
this.lastQuerySubscriptions = [];
}

Expand Down Expand Up @@ -141,8 +139,6 @@ export default class Resolver {
}
});

this.lastQueries = queries;
this.lastRouteVariables = routeVariables;
this.lastQuerySubscriptions = querySubscriptions;

return querySubscriptions;
Expand Down
4 changes: 3 additions & 1 deletion src/modern/getQueryName.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export default function getQueryName(route) {
}

if (typeof query === 'function') {
return query().name;
query = query();
if (query.name) return query.name;
if (query.params && query.params.name) return query.params.name;
}
}

Expand Down
Loading

0 comments on commit 97fb738

Please sign in to comment.