Skip to content

Commit

Permalink
added User types and models
Browse files Browse the repository at this point in the history
  • Loading branch information
Markkop committed Sep 6, 2019
1 parent 377d9bb commit 97533ea
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 9 deletions.
21 changes: 18 additions & 3 deletions packages/server/data/schema.graphql
Expand Up @@ -5,11 +5,16 @@ type Event {
title: String
date: String
description: String
author: String
author: User
}

type EventAddSubscription {
input EventAddSubscriptionInput {
clientSubscriptionId: String
}

type EventAddSubscriptionPayload {
subscription: Event
clientSubscriptionId: String
}

input EventCreateInput {
Expand All @@ -32,8 +37,18 @@ type Mutation {
type Query {
event(id: ID!): Event
events: [Event]
users(id: ID!): User
}

type Subscription {
EventAddSubscription: EventAddSubscription
EventAddSubscription(input: EventAddSubscriptionInput!): EventAddSubscriptionPayload
}

type User {
"""The ID of an object"""
id: ID!
_id: String
name: String
email: String
active: String
}
160 changes: 155 additions & 5 deletions packages/server/data/schema.json
Expand Up @@ -58,6 +58,33 @@
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "users",
"description": null,
"args": [
{
"name": "id",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
},
"defaultValue": null
}
],
"type": {
"kind": "OBJECT",
"name": "User",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
Expand Down Expand Up @@ -149,8 +176,8 @@
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"kind": "OBJECT",
"name": "User",
"ofType": null
},
"isDeprecated": false,
Expand All @@ -172,6 +199,81 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "User",
"description": null,
"fields": [
{
"name": "id",
"description": "The ID of an object",
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "_id",
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "name",
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "email",
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "active",
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "Mutation",
Expand Down Expand Up @@ -316,10 +418,25 @@
{
"name": "EventAddSubscription",
"description": null,
"args": [],
"args": [
{
"name": "input",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "INPUT_OBJECT",
"name": "EventAddSubscriptionInput",
"ofType": null
}
},
"defaultValue": null
}
],
"type": {
"kind": "OBJECT",
"name": "EventAddSubscription",
"name": "EventAddSubscriptionPayload",
"ofType": null
},
"isDeprecated": false,
Expand All @@ -331,9 +448,30 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INPUT_OBJECT",
"name": "EventAddSubscriptionInput",
"description": null,
"fields": null,
"inputFields": [
{
"name": "clientSubscriptionId",
"description": null,
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
}
],
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "EventAddSubscription",
"name": "EventAddSubscriptionPayload",
"description": null,
"fields": [
{
Expand All @@ -347,6 +485,18 @@
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "clientSubscriptionId",
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/modules/event/EventType.js
@@ -1,5 +1,6 @@
const graphql = require("graphql");
const globalIdField = require("graphql-relay").globalIdField;
const UserType = require("./UserType");

const { GraphQLObjectType, GraphQLString } = graphql;

Expand All @@ -11,7 +12,7 @@ const EventType = new GraphQLObjectType({
title: { type: GraphQLString },
date: { type: GraphQLString },
description: { type: GraphQLString },
author: { type: GraphQLString }
author: { type: UserType }
})
});

Expand Down
10 changes: 10 additions & 0 deletions packages/server/src/modules/event/UserModel.js
@@ -0,0 +1,10 @@
var mongoose = require("mongoose");

const UserSchema = new mongoose.Schema({
name: String,
password: String,
email: String,
active: Boolean
});

module.exports = mongoose.model("User", UserSchema);
17 changes: 17 additions & 0 deletions packages/server/src/modules/event/UserType.js
@@ -0,0 +1,17 @@
const graphql = require("graphql");
const globalIdField = require("graphql-relay").globalIdField;

const { GraphQLObjectType, GraphQLString } = graphql;

const UserType = new GraphQLObjectType({
name: "User",
fields: () => ({
id: globalIdField("users"),
_id: { type: GraphQLString },
name: { type: GraphQLString },
email: { type: GraphQLString },
active: { type: GraphQLString }
})
});

module.exports = UserType;
@@ -0,0 +1,51 @@
import { GraphQLString, GraphQLNonNull } from "graphql";
import { mutationWithClientMutationId } from "graphql-relay";

import { generateToken } from "../../../auth";

import UserModel from "../UserModel";

export default mutationWithClientMutationId({
name: "UserLoginWithEmail",
inputFields: {
email: {
type: new GraphQLNonNull(GraphQLString)
},
password: {
type: new GraphQLNonNull(GraphQLString)
}
},
mutateAndGetPayload: async ({ email, password }) => {
const user = await UserModel.findOne({ email: email.toLowerCase() });

const defaultErrorMessage = "Invalid password";

if (!user) {
return {
error: defaultErrorMessage
};
}

const correctPassword = user.authenticate(password);

if (!correctPassword) {
return {
error: defaultErrorMessage
};
}

return {
token: generateToken(user)
};
},
outputFields: {
token: {
type: GraphQLString,
resolve: ({ token }) => token
},
error: {
type: GraphQLString,
resolve: ({ error }) => error
}
}
});
@@ -0,0 +1,56 @@
import { GraphQLString, GraphQLNonNull } from "graphql";
import { mutationWithClientMutationId } from "graphql-relay";

import { generateToken } from "../../../auth";
// import pubSub, { EVENTS } from '../../../pubSub';

import UserModel from "../UserModel";

export default mutationWithClientMutationId({
name: "UserRegisterWithEmail",
inputFields: {
name: {
type: new GraphQLNonNull(GraphQLString)
},
email: {
type: new GraphQLNonNull(GraphQLString)
},
password: {
type: new GraphQLNonNull(GraphQLString)
}
},
mutateAndGetPayload: async ({ name, email, password }) => {
let user = await UserModel.findOne({ email: email.toLowerCase() });

if (user) {
return {
error: "Email already in use"
};
}

user = new UserModel({
name,
email,
password
});

await user.save();

// Subscription not working
// await pubSub.publish(EVENTS.USER.ADDED, { UserAdded: { user } });

return {
token: generateToken(user)
};
},
outputFields: {
token: {
type: GraphQLString,
resolve: ({ token }) => token
},
error: {
type: GraphQLString,
resolve: ({ error }) => error
}
}
});

0 comments on commit 97533ea

Please sign in to comment.