Guest User

Untitled

a guest
Jan 31st, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. app.use( '/graphql', expressGraphQL(req => ({
  2. schema,
  3. graphiql: false,
  4. rootValue: { request: req },
  5. pretty: __DEV__,
  6. }))
  7. );
  8.  
  9. import React from 'react';
  10. import PropTypes, { func } from 'prop-types';
  11. import withStyles from 'isomorphic-style-loader/lib/withStyles';
  12. import s from './Register.css';
  13. import fetch from 'node-fetch';
  14.  
  15. let postURL = 'http://localhost:3000/graphql/';
  16.  
  17. class Register extends React.Component {
  18. static propTypes = {
  19. title: PropTypes.string.isRequired,
  20. };
  21.  
  22. constructor(props) {
  23. super(props);
  24. this.state = {
  25. schoolName: '',
  26. userName: '',
  27. password: '',
  28. };
  29. this.onSchoolNameChange = this.onSchoolNameChange.bind(this);
  30. this.onUserNameChange = this.onUserNameChange.bind(this);
  31. this.onPasswordChange = this.onPasswordChange.bind(this);
  32. }
  33.  
  34. onSchoolNameChange(event) {
  35. this.setState({ schoolName: event.target.value });
  36. }
  37.  
  38. onUserNameChange(event) {
  39. this.setState({ userName: event.target.value });
  40. }
  41.  
  42. onPasswordChange(event) {
  43. this.setState({ password: event.target.value });
  44. }
  45.  
  46. handleSubmit = () => {
  47. // eslint-disable-next-line no-unused-vars
  48. const { schoolName, userName, password } = this.state;
  49. console.log(postURL);
  50. fetch(postURL, {
  51. mode: 'no-cors',
  52. method: "POST",
  53. headers: {
  54. "Content-Type": "multipart/form-data",
  55. "Accept": "application/json",
  56. "type": "formData"
  57. },
  58. body: this.state
  59. }).then((response) => {
  60. console.log(response);
  61. alert("Submitted!");
  62. }).catch((error) => {
  63. console.log(error);
  64. })
  65. };
  66.  
  67. render() {
  68. const { schoolName, userName, password } = this.state;
  69. const isEnabled =
  70. schoolName.length > 0 && userName.length > 0 && password.length > 0;
  71. return (
  72. <div className={s.root}>
  73. <div className={s.container}>
  74. <h1>{this.props.title}</h1>
  75. <form action="/graphql" method="post" onSubmit={this.handleSubmit}>
  76. <div className={s.formGroup}>
  77. <label className={s.label} htmlFor="schoolName">
  78. School Name:
  79. <input
  80. className={s.input}
  81. id="schoolName"
  82. type="text"
  83. name="schoolName"
  84. value={this.state.schoolName}
  85. onChange={this.onSchoolNameChange}
  86. autoFocus // eslint-disable-line jsx-a11y/no-autofocus
  87. />
  88. </label>
  89. </div>
  90. <div className={s.formGroup}>
  91. <label className={s.label} htmlFor="userName">
  92. User Name:
  93. <input
  94. className={s.input}
  95. id="userName"
  96. type="text"
  97. name="userName"
  98. value={this.state.userName}
  99. onChange={this.onUserNameChange}
  100. />
  101. </label>
  102. </div>
  103. <div className={s.formGroup}>
  104. <label className={s.label} htmlFor="password">
  105. Password:
  106. <input
  107. className={s.input}
  108. id="password"
  109. type="password"
  110. name="password"
  111. value={this.state.password}
  112. onChange={this.onPasswordChange}
  113. />
  114. </label>
  115. </div>
  116. <div className={s.formGroup}>
  117. <button disabled={!isEnabled} className={s.button} type="submit">
  118. Submit
  119. </button>
  120. </div>
  121. </form>
  122. </div>
  123. </div>
  124. );
  125. }
  126. }
  127.  
  128. export default withStyles(s)(Register);
  129.  
  130. import {
  131. GraphQLSchema as Schema,
  132. GraphQLObjectType as ObjectType,
  133. } from 'graphql';
  134.  
  135. import mutation from './mutations/mutation'
  136.  
  137. const schema = new Schema({
  138. mutation: mutation
  139. });
  140.  
  141. export default schema;
  142.  
  143. import Sequelize from 'sequelize';
  144. import config from '../config';
  145.  
  146. const sequelize = new Sequelize(config.databaseUrl, {
  147. dialect: 'sqlite',
  148. storage: './database.sqlite',
  149. define: {
  150. freezeTableName: true,
  151. },
  152. });
  153.  
  154. export default sequelize;
  155.  
  156. import {GraphQLObjectType, GraphQLString} from 'graphql';
  157. import SchoolType from '../types/SchoolType';
  158.  
  159. const Mutation = new GraphQLObjectType({
  160. name: 'SchoolMutation',
  161. fields: {
  162. registerSchool: {
  163. type: SchoolType,
  164. description: 'Register a new school',
  165. args: {
  166. id: {
  167. name: 'id',
  168. type: GraphQLString
  169. },
  170. userName: {
  171. name: 'userName',
  172. type: GraphQLString
  173. },
  174. password: {
  175. name: 'password',
  176. type: GraphQLString
  177. }
  178. },
  179. resolve: (root, {id, userName, password}) => {
  180. //do something??? call db?
  181. }
  182. }
  183. }
  184. });
  185.  
  186. export default Mutation;
  187.  
  188. import {
  189. GraphQLObjectType as ObjectType,
  190. GraphQLString as StringType,
  191. GraphQLNonNull as NonNull,
  192. } from 'graphql';
  193.  
  194. const SchoolType = new ObjectType({
  195. name: 'School',
  196. fields: {
  197. id: { type: new NonNull(StringType) },
  198. userName: { type: new NonNull(StringType) },
  199. password: { type: StringType },
  200. },
  201. });
  202.  
  203. export default SchoolType;
Add Comment
Please, Sign In to add comment