Skip to content

Commit

Permalink
Added some db calls and such
Browse files Browse the repository at this point in the history
  • Loading branch information
elevenpassin committed Dec 24, 2017
1 parent 524d88d commit 4bb19c3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
23 changes: 10 additions & 13 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { find, filter } = require('lodash');
const express = require('express');
const mongoose = require('mongoose');
const { ObjectID } = require('mongodb');
const assert = require('assert');
const bodyParser = require('body-parser');
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
Expand All @@ -14,7 +15,7 @@ mongoose.connect(CONNECTION_URL, {
useMongoClient: true
});

const Posts = require('./models/posts');
const { Post, User } = require('./models');
const db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'))
Expand All @@ -24,24 +25,20 @@ db.on('error', console.error.bind(console, 'connection error:'))
=====
`));


const resolvers = {
User: {
posts: (user) => filter(posts, { userid: user.userid }),
posts: async (user) => await Post.find({ userid: user._id }),
comments: (user) => filter(comments, { userid: user.userid })
},
Comment: {
user: (comment) => find(users, { userid: comment.userid })
},
Post: {
comments: (post) => filter(comments, { postid: post.postid }),
user: (post) => find(users, { userid: post.userid })
comments: (post) => filter(comments, { postid: post._id }),
user: async (post) => await User.findOne({ _id: post.userid })
},
Query: {
allPosts: async () => await Posts.find({}),
getPost: (_, { postid }) => {
return find(posts, { postid })
}
allPosts: async () => await Post.find({}),
}
}

Expand Down Expand Up @@ -107,9 +104,8 @@ const comments = [

const posts = [
{
title: "Post title",
userid: 0,
postid: 0,
title: "Save water!",
userid: "5a3f88fecd9c9406101645d6",
content: `
Lorem ipsum dolor sit amet consectetur adipiscing elit fringilla,
massa scelerisque mus quam nulla lobortis litora,
Expand All @@ -120,7 +116,8 @@ const posts = [
Mus at curae tempor blandit suspendisse molestie tortor eget ornare aliquam,
aliquet eros luctus nisi suscipit venenatis platea senectus posuere,
interdum proin a in primis rhoncus tellus dapibus litora.
`
`,
comments: []
},
{
title: "Another title",
Expand Down
7 changes: 7 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Post = require('./post');
const User = require('./user');

module.exports = {
Post,
User
};
12 changes: 4 additions & 8 deletions models/posts.js → models/post.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
const mongoose = require('mongoose');
mongoose.Promise = Promise;

const { ObjectID } = require('mongodb');
const Schema = mongoose.Schema;

const postSchema = new Schema({
postid: {
type: String,
required: true
},
title: {
type: String,
required: true
Expand All @@ -21,8 +15,10 @@ const postSchema = new Schema({
type: String,
required: true
},
comments: []
}, {
collection: 'posts', timestamps: true
collection: 'posts',
timestamps: true
});

module.exports = mongoose.model('posts', postSchema);
module.exports = mongoose.model('post', postSchema);
23 changes: 23 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const mongoose = require('mongoose');
mongoose.Promise = Promise;

const Schema = mongoose.Schema;

const userSchema = new Schema({
name: {
type: String,
required: true
},
posts: {
type: [String],
},
comments: {
type: [String]
}
}, {
collection: 'users',
timestamps: true
});

module.exports = mongoose.model('user', userSchema);

3 changes: 1 addition & 2 deletions typeDefs.gql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = `
type User {
userid: String!
_id: String!
name: String!
posts: [Post]
comments: [Comment]
Expand All @@ -14,7 +14,6 @@ module.exports = `

type Post {
_id: String!
postid: String!
title: String!
userid: String!
user: User!
Expand Down

0 comments on commit 4bb19c3

Please sign in to comment.