Skip to content

Instantly share code, notes, and snippets.

@idkjs
Created January 16, 2017 15:19
Show Gist options
  • Save idkjs/f0269dcb8c180b8bcf555ff5792f1788 to your computer and use it in GitHub Desktop.
Save idkjs/f0269dcb8c180b8bcf555ff5792f1788 to your computer and use it in GitHub Desktop.
/* eslint-disable no-class-assign */
/* original code: https://github.com/jscomplete/learning-graphql-and-relay/blob/chapter7/js/app.js*/
import React from 'react';
import ReactDOM from 'react-dom';
import Relay from 'react-relay';
import { debounce } from 'lodash';
import SearchForm from './search-form';
import Quote from './quote';
class QuotesLibrary extends React.Component {
constructor(props) {
super(props)
this.search = debounce(this.search.bind(this), 300)
}
search(searchTerm) {
this.props.relay.setVariables({ searchTerm });
}
render() {
return (
<div className="quotes-library">
<SearchForm searchAction={this.search} />
<div className="quotes-list">
{this.props.library.quotesConnection.edges.map(edge =>
<Quote key={edge.node.id} quote={edge.node} />
)}
</div>
</div>
)
}
}
QuotesLibrary = Relay.createContainer(QuotesLibrary, {
initialVariables: {
searchTerm: '',
},
fragments: {
library: () => Relay.QL `
fragment on User {
quotesConnection(first:100, searchTerm: $searchTerm) {
edges {
node {
id
${Quote.getFragment('quote')}
}
}
}
}
`,
},
});
class AppRoute extends Relay.Route {
static routeName = 'AppRoute';
static queries = {
library: (Component) => Relay.QL`
query QuotesQuery {
viewer {
${Component.getFragment('library')}
}
}
`,
}
}
ReactDOM.render(
<Relay.RootContainer
Component={QuotesLibrary}
route={new AppRoute()}
/>,
document.getElementById('react')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment