Last active
August 15, 2016 14:00
-
-
Save jwdotjs/5c2c16fbcdedaa8c21ab59535cd7d905 to your computer and use it in GitHub Desktop.
GraphQL HOC
This file contains 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 {gql} from 'components/graphql-container'; | |
export class Welcome extends React.Component { | |
static propTypes = { | |
id: React.PropTypes.number, | |
delayedUser: React.PropTypes.func.isRequired, | |
params: React.PropTypes.object.isRequired, | |
createAuditLog: React.PropTypes.object.isRequired, | |
} | |
state = { | |
id: Number(this.props.params.id), | |
} | |
onFetchDelayedUser() { | |
this.setState({id: this.state.id + 1}, this.props.delayedUser.fetch(this.state.id)); | |
} | |
render() { | |
return ( | |
<div> | |
<h1>Welcome</h1> | |
<button onClick={e => this.onFetchDelayedUser()}> | |
Click To fetch new user | |
</button> | |
<br /><br /> | |
<button onClick={e => this.props.createAuditLog.commit({note: 'Hello Phoenix ReactJS', sentiment: 'success'})}> | |
Click to create new audit log record | |
</button> | |
</div> | |
); | |
} | |
} | |
Welcome = gql(Welcome, { | |
queries: (props) => ({ | |
// This query is a function, so it won't make a network request until the user triggers it | |
delayedUser: (id) => ` | |
user(id: ${id}) { | |
id, | |
nickname | |
} | |
`, | |
// Returns this.props.users on mount | |
users: ` | |
users(limit: ${props.params.id}) { | |
count, | |
records { | |
id, | |
nickname | |
} | |
} | |
`, | |
// Returns this.props.auditLogs on mount | |
auditLogs: ` | |
auditLogs(limit: ${props.params.id}) { | |
count, | |
records { | |
id, | |
note | |
} | |
} | |
`, | |
}), | |
mutations: (props) => ({ | |
createAuditLog: (input) => `{ | |
id, | |
note, | |
sentiment | |
}`, | |
}), | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment