UserCreate.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import React from 'react'
  2. import gql from 'graphql-tag'
  3. import { Mutation } from 'react-apollo'
  4. const USER_CREATE = gql`
  5. mutation USER_CREATE($email: String!, $name: String!, $abbreviation: String!, $password: String!) {
  6. createUser(email: $email, name: $name, abbreviation: $abbreviation, password: $password) {
  7. id
  8. name
  9. email
  10. abbreviation
  11. }
  12. }
  13. `
  14. class UserCreate extends React.Component {
  15. state = {
  16. name: '',
  17. email: '',
  18. abbreviation: '',
  19. password: ''
  20. }
  21. handleInput = event => {
  22. this.setState({ [event.target.name]: event.target.value })
  23. }
  24. render() {
  25. <Mutation
  26. mutation={USER_CREATE}
  27. variables={this.state}
  28. refetchQueries={[{ query: USER_CREATE }]}
  29. >
  30. {(userCreate, { error, loading }) => (
  31. <form
  32. method="POST"
  33. onSubmit={async event => {
  34. event.preventDefault()
  35. await userCreate()
  36. this.setState({ name: '', email: '', abbreviation: '', password: '' })
  37. }}
  38. >
  39. <input type="text" name="name" id="name" value={this.state.name} onChange={this.handleInput} />
  40. <label htmlFor="name"></label>
  41. <input type="text" name="abbreviation" id="abbreviation" value={this.state.abbreviation} onChange={this.handleInput} />
  42. <label htmlFor="abbreviation"></label>
  43. <input type="text" name="email" id="email" value={this.state.email} onChange={this.handleInput} />
  44. <label htmlFor="email"></label>
  45. <input type="password" name="password" id="password" value={this.state.password} onChange={this.handleInput} />
  46. <label htmlFor="password"></label>
  47. <button type="submit">Create</button>
  48. </form>
  49. )}
  50. </Mutation>
  51. }
  52. }
  53. export default UserCreate