diff --git a/client/src/App.js b/client/src/App.js index 0d3f9066e..9864ce2e0 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -5,6 +5,7 @@ import {ApolloProvider} from 'react-apollo'; // Components import UserList from './components/UserList'; +import AddUser from './components/AddUser'; // Apollo Client Setup @@ -18,7 +19,8 @@ class App extends Component {

Advanced Algos - Users Module

- + +
); diff --git a/client/src/components/AddUser.js b/client/src/components/AddUser.js new file mode 100644 index 000000000..ca162d521 --- /dev/null +++ b/client/src/components/AddUser.js @@ -0,0 +1,68 @@ +import React, { Component } from 'react'; +import {graphql, compose} from 'react-apollo'; +import {getRolesQuery, addUserMutation, getUsersQuery} from '../queries/queries'; + +class AddUser extends Component { + constructor(props){ + super(props); + this.state = { + alias: '', + firstName: '', + lastName: '', + idRole: '' + }; + } + displayRoles(){ + var data = this.props.getRolesQuery; // When there is more than one query binded to a single componente 'data' is replaced by thename of the query given below at the binding operation. + if(data.loading){ + return( ); + } else { + return data.roles.map(role => { + return( ); + }); + } + } + submitForm(e){ + e.preventDefault(); + this.props.addUserMutation({ + variables: { + alias: this.state.alias, + firstName: this.state.firstName, + lastName: this.state.lastName, + roleId: this.state.roleId + }, + refetchQueries: [{ query: getUsersQuery}] // This allow us to re run whatever queries are necesary after the mutation. + }); + } + render(){ + return( +
+
+ + this.setState({ alias:e.target.value})}/> +
+
+ + this.setState({ firstName:e.target.value})}/> +
+
+ + this.setState({ lastName:e.target.value})}/> +
+
+ + +
+ + +
+ ); + } +} + +export default compose( + graphql(getRolesQuery, {name: "getRolesQuery"}), + graphql(addUserMutation, {name: "addUserMutation"}) +) (AddUser); // This technique binds more than one query to a single component. diff --git a/client/src/components/UserList.js b/client/src/components/UserList.js index e3b4d5b2d..984bd2a70 100644 --- a/client/src/components/UserList.js +++ b/client/src/components/UserList.js @@ -1,15 +1,30 @@ import React, { Component } from 'react'; +import {graphql} from 'react-apollo'; +import {getUsersQuery} from '../queries/queries'; class UserList extends Component { + displayUsers(){ + let data = this.props.data; + + if(data.loading){ + return (
Loading Users...
); + } else { + return data.users.map(user => { + return ( +
  • {user.alias}
  • + ) + }); + } + } render() { return (
    -
    ); } } -export default UserList; +export default graphql(getUsersQuery)(UserList); // This binds the querty to the component diff --git a/client/src/queries/queries.js b/client/src/queries/queries.js new file mode 100644 index 000000000..d13c1fbcb --- /dev/null +++ b/client/src/queries/queries.js @@ -0,0 +1,31 @@ +import {gql} from 'apollo-boost'; + +const getUsersQuery = gql` +{ + users { + id + alias + firstName + lastName + roleId + } +} +` +const getRolesQuery = gql` +{ + roles { + id + name + } +} +` + +const addUserMutation = gql` +mutation($alias: String!, $firstName:String, $lastName:String, $roleId:String!) { + addUser (alias: $alias, firstName:$firstName, lastName: $lastName, roleId: $roleId){ + id + alias + } +} +` +export {getUsersQuery, getRolesQuery, addUserMutation} diff --git a/server/app.js b/server/app.js index ae55191cc..b4976f271 100644 --- a/server/app.js +++ b/server/app.js @@ -2,9 +2,13 @@ const express = require('express'); const graphqlHTTP = require('express-graphql'); const schema = require('./schema/schema'); const mongoose = require('mongoose'); +const cors = require('cors'); const app = express(); +// Allow crosss-origin requests +app.use(cors()); + // Connect to the daabase mongoose.connect('mongodb://users-module-graphql-server:dsadTRYUtrsgg34@ds141952.mlab.com:41952/users', { useNewUrlParser: true }); mongoose.connection.once('open', () => { diff --git a/server/package-lock.json b/server/package-lock.json index f901fa6dc..1297e60cd 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -78,6 +78,15 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" }, + "cors": { + "version": "2.8.4", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.4.tgz", + "integrity": "sha1-K9OB8usgECAQXNUOpZ2mMJBpRoY=", + "requires": { + "object-assign": "4.1.1", + "vary": "1.1.2" + } + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -388,6 +397,11 @@ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz", "integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk=" }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" + }, "on-finished": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", diff --git a/server/package.json b/server/package.json index d6c6b475c..dc5830754 100644 --- a/server/package.json +++ b/server/package.json @@ -9,6 +9,7 @@ "author": "", "license": "ISC", "dependencies": { + "cors": "^2.8.4", "express": "^4.16.3", "express-graphql": "^0.6.12", "graphql": "^14.0.0", diff --git a/server/schema/schema.js b/server/schema/schema.js index da44d2421..8fbda90c6 100644 --- a/server/schema/schema.js +++ b/server/schema/schema.js @@ -27,6 +27,7 @@ const UserType = new GraphQLObjectType({ alias: {type: GraphQLString}, firstName: {type: GraphQLString}, lastName: {type: GraphQLString}, + roleId: {type: GraphQLString}, role: { type: RoleType, resolve(parent, args) {