Created
September 26, 2015 10:42
-
-
Save miracle2k/f39aaaccbc0d287b2ddb to your computer and use it in GitHub Desktop.
Simplify relay using decorators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@relayRoot | |
@relayContainer({ | |
client: () => Relay.QL` | |
fragment on Client { | |
id, | |
menuitems(first: 50) { | |
} | |
} | |
` | |
}) | |
export default class Items extends Component { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import Relay from 'react-relay'; | |
var Spinner = require('react-spinkit'); | |
/** | |
* For most cases, our queries are rooted in the client query, | |
* so we can re-use the same route. | |
*/ | |
export class CurrentClientRoute extends Relay.Route { | |
static queries = { | |
client: () => Relay.QL` | |
query { client } | |
` | |
}; | |
static paramDefinitions = {}; | |
static routeName = 'CurrentClientRoute'; | |
} | |
/** | |
* Wrap a component in a relay RootContainer, showing a spinner. | |
*/ | |
export function relayRoot(component) { | |
return (props) => ( | |
<Relay.RootContainer | |
Component={component} | |
route={new CurrentClientRoute} | |
renderLoading={() => <Spinner spinnerName='wordpress' />} | |
/> | |
) | |
} | |
/** | |
* Wrap a component in a relay container. | |
* | |
* Use this as a decorator. | |
*/ | |
export function relayContainer(fragments) { | |
return function decorator(component) { | |
return Relay.createContainer(component, {fragments: fragments}); | |
} | |
} |
Thank you! This makes it so much DRY'er and easier to grasp what's going on.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@miracle2k Have you been able to use this with Relay 0.6? When I attempt this I'm getting:
Invariant Violation: RelayQueryNode: Abstract class cannot be instantiated.