Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.use( '/graphql', expressGraphQL(req => ({
- schema,
- graphiql: false,
- rootValue: { request: req },
- pretty: __DEV__,
- }))
- );
- import React from 'react';
- import PropTypes, { func } from 'prop-types';
- import withStyles from 'isomorphic-style-loader/lib/withStyles';
- import s from './Register.css';
- import fetch from 'node-fetch';
- let postURL = 'http://localhost:3000/graphql/';
- class Register extends React.Component {
- static propTypes = {
- title: PropTypes.string.isRequired,
- };
- constructor(props) {
- super(props);
- this.state = {
- schoolName: '',
- userName: '',
- password: '',
- };
- this.onSchoolNameChange = this.onSchoolNameChange.bind(this);
- this.onUserNameChange = this.onUserNameChange.bind(this);
- this.onPasswordChange = this.onPasswordChange.bind(this);
- }
- onSchoolNameChange(event) {
- this.setState({ schoolName: event.target.value });
- }
- onUserNameChange(event) {
- this.setState({ userName: event.target.value });
- }
- onPasswordChange(event) {
- this.setState({ password: event.target.value });
- }
- handleSubmit = () => {
- // eslint-disable-next-line no-unused-vars
- const { schoolName, userName, password } = this.state;
- console.log(postURL);
- fetch(postURL, {
- mode: 'no-cors',
- method: "POST",
- headers: {
- "Content-Type": "multipart/form-data",
- "Accept": "application/json",
- "type": "formData"
- },
- body: this.state
- }).then((response) => {
- console.log(response);
- alert("Submitted!");
- }).catch((error) => {
- console.log(error);
- })
- };
- render() {
- const { schoolName, userName, password } = this.state;
- const isEnabled =
- schoolName.length > 0 && userName.length > 0 && password.length > 0;
- return (
- <div className={s.root}>
- <div className={s.container}>
- <h1>{this.props.title}</h1>
- <form action="/graphql" method="post" onSubmit={this.handleSubmit}>
- <div className={s.formGroup}>
- <label className={s.label} htmlFor="schoolName">
- School Name:
- <input
- className={s.input}
- id="schoolName"
- type="text"
- name="schoolName"
- value={this.state.schoolName}
- onChange={this.onSchoolNameChange}
- autoFocus // eslint-disable-line jsx-a11y/no-autofocus
- />
- </label>
- </div>
- <div className={s.formGroup}>
- <label className={s.label} htmlFor="userName">
- User Name:
- <input
- className={s.input}
- id="userName"
- type="text"
- name="userName"
- value={this.state.userName}
- onChange={this.onUserNameChange}
- />
- </label>
- </div>
- <div className={s.formGroup}>
- <label className={s.label} htmlFor="password">
- Password:
- <input
- className={s.input}
- id="password"
- type="password"
- name="password"
- value={this.state.password}
- onChange={this.onPasswordChange}
- />
- </label>
- </div>
- <div className={s.formGroup}>
- <button disabled={!isEnabled} className={s.button} type="submit">
- Submit
- </button>
- </div>
- </form>
- </div>
- </div>
- );
- }
- }
- export default withStyles(s)(Register);
- import {
- GraphQLSchema as Schema,
- GraphQLObjectType as ObjectType,
- } from 'graphql';
- import mutation from './mutations/mutation'
- const schema = new Schema({
- mutation: mutation
- });
- export default schema;
- import Sequelize from 'sequelize';
- import config from '../config';
- const sequelize = new Sequelize(config.databaseUrl, {
- dialect: 'sqlite',
- storage: './database.sqlite',
- define: {
- freezeTableName: true,
- },
- });
- export default sequelize;
- import {GraphQLObjectType, GraphQLString} from 'graphql';
- import SchoolType from '../types/SchoolType';
- const Mutation = new GraphQLObjectType({
- name: 'SchoolMutation',
- fields: {
- registerSchool: {
- type: SchoolType,
- description: 'Register a new school',
- args: {
- id: {
- name: 'id',
- type: GraphQLString
- },
- userName: {
- name: 'userName',
- type: GraphQLString
- },
- password: {
- name: 'password',
- type: GraphQLString
- }
- },
- resolve: (root, {id, userName, password}) => {
- //do something??? call db?
- }
- }
- }
- });
- export default Mutation;
- import {
- GraphQLObjectType as ObjectType,
- GraphQLString as StringType,
- GraphQLNonNull as NonNull,
- } from 'graphql';
- const SchoolType = new ObjectType({
- name: 'School',
- fields: {
- id: { type: new NonNull(StringType) },
- userName: { type: new NonNull(StringType) },
- password: { type: StringType },
- },
- });
- export default SchoolType;
Add Comment
Please, Sign In to add comment