From e86aa526d561d7d79989e062b6ba7d1d14a2fdf2 Mon Sep 17 00:00:00 2001 From: ev-dev Date: Thu, 9 Nov 2017 08:16:03 -0500 Subject: [PATCH 1/2] Feat: Comment resolvers now propegate children, updates to user resolvers --- src/server/api/index.js | 2 +- src/server/api/resolvers/comments.js | 41 ++++++++++++++++++---------- src/server/api/resolvers/examples.js | 16 +++++------ src/server/api/resolvers/users.js | 31 +++++++++++++++------ src/server/api/types/_Comment.js | 1 + src/server/api/types/_User.js | 2 +- src/server/app/dev.js | 20 +++++++------- 7 files changed, 70 insertions(+), 43 deletions(-) diff --git a/src/server/api/index.js b/src/server/api/index.js index 11422ca..fe66ae8 100644 --- a/src/server/api/index.js +++ b/src/server/api/index.js @@ -15,7 +15,7 @@ const schema = makeExecutableSchema({ export default Router() .use(bodyParser.urlencoded({ extended: true })) .use(bodyParser.json()) - .use('/mdn', ) // RESTFUL + // .use('/mdn', ) // RESTFUL .use('/graphql', graphqlExpress({ schema })) .use('/graphiql', graphiqlExpress({ endpointURL: '/graphql', diff --git a/src/server/api/resolvers/comments.js b/src/server/api/resolvers/comments.js index f025da1..f94d6a8 100644 --- a/src/server/api/resolvers/comments.js +++ b/src/server/api/resolvers/comments.js @@ -1,14 +1,17 @@ import { Op } from 'sequelize' -import { Comment, User, Example } from '../../db/models' -const Query = {}, Mutation = {}, Resolver = { Query, Mutation } +import { _Comment, _User, _Example } from '../../db' +const Comment = {} + , Query = {} + , Mutation = {} + , Resolver = { Query, Mutation, Comment } // ------ Comment Queries ------ // -Query.comment = async (_, { id }) => Comment.findById(+id) +Query.comment = async (_, { id }) => _Comment.findById(+id) Query.commentTree = async (_, { rootCommentId, levels }) => { let rootComment, rootChildren, deepChildren try { - rootComment = await Comment.findById(+rootCommentId) + rootComment = await _Comment.findById(+rootCommentId) rootChildren = await rootComment.getChildren() deepChildren = await rootChildren.map(child => child.dataValues.id) } catch (err) { @@ -22,8 +25,8 @@ Query.commentTree = async (_, { rootCommentId, levels }) => { Query.commentWithChildren = async (_, { id }) => await Promise.all([ - Comment.findById(+id), - Comment.findAll({ + _Comment.findById(+id), + _Comment.findAll({ where: { parentId: +id } @@ -31,29 +34,37 @@ Query.commentWithChildren = async (_, { id }) => ]) Query.commentsByExample = async (_, { exampleId }) => - Comment.findAll({ + _Comment.findAll({ where: { exampleId: +exampleId }, - include: [{ model: User, as: 'author' }] + include: [{ model: _User, as: 'author' }] }) Query.commentsByAuthor = async (_, { authorId }) => - Comment.findAll({ + _Comment.findAll({ where: { authorId: +authorId }, include: { model: Example } }) Query.childComments = async (_, { parentId }) => - Comment.findAll({ + _Comment.findAll({ where: { parentId: +parentId }, - include: [{ model: User, as: 'author' }] + include: [{ model: _User, as: 'author' }] }) + +// ------ Comment Type Resolvers ------ // +Comment.children = async comment => + _Comment.findAll({ where: { parentId: comment.id } }) + +Comment.author = async comment => + _User.findById(+comment.authorId) + // ------ Comment Mutations ------ // Mutation.createComment = async (_, ...commentFields) => - Comment.create({ ...commentFields }), + _Comment.create({ ...commentFields }), Mutation.deleteComment = async (_, { commentId }) => - Comment.destroy({ + _Comment.destroy({ where: { id: commentId }, returning: true }) @@ -62,9 +73,9 @@ export default Resolver // NOTE! Possible Define individual fields of _Comment type // this could allow the "children" field to resolve -// to an array of Comments +// to an array of _Comments - // Comment: { + // _Comment: { // children: ({ parentId }) => Comment.findAll({ // where: { parentId } // }) diff --git a/src/server/api/resolvers/examples.js b/src/server/api/resolvers/examples.js index 126daa8..bc91c7d 100644 --- a/src/server/api/resolvers/examples.js +++ b/src/server/api/resolvers/examples.js @@ -1,10 +1,10 @@ import { Op } from 'sequelize' -import { Example, User, Comment } from '../../db/models' +import { _Example, _User, _Comment } from '../../db' const Query = {}, Mutation = {}, Resolver = { Query, Mutation } // ------ Example Queries ------ // Query.example = async (_, { id }) => - Example.findById(+id, { + _Example.findById(+id, { include: [{ model: User, as: 'coder' @@ -15,20 +15,20 @@ Query.example = async (_, { id }) => Query.examples = async (_, { limit, offset, include, tag }) => { if (include) - return Example.scope(include).findAll({ limit, offset, tag }) + return _Example.scope(include).findAll({ limit, offset, tag }) else if (tag) - return Example.findAll({ limit, offset, where: { tag } }) + return _Example.findAll({ limit, offset, where: { tag } }) else - return Example.findAll({ limit, offset }) + return _Example.findAll({ limit, offset }) } Query.examplesByCoder = async (_, { coderId }) => - Example.scope('coder').findAll({ + _Example.scope('coder').findAll({ where: { coderId } }) Query.searchExamples = async (_, { query }) => - Example.findAll({ + _Example.findAll({ where: { [Op.or]: { snippet: { @@ -46,6 +46,6 @@ Query.searchExamples = async (_, { query }) => // ------ Example Mutations ------ // Mutation.createExample = async (_, { snippet, coderId }) => - Example.create({ snippet, coderId }) + _Example.create({ snippet, coderId }) export default Resolver \ No newline at end of file diff --git a/src/server/api/resolvers/users.js b/src/server/api/resolvers/users.js index 430568b..c611d27 100644 --- a/src/server/api/resolvers/users.js +++ b/src/server/api/resolvers/users.js @@ -1,19 +1,34 @@ import { Op } from 'sequelize' -import { User } from '../../db/models' -const Query = {}, Mutation = {}, Resolver = { Query, Mutation } +import { _User, _Example, _Comment } from '../../db' +const User = {} + , Query = {} + , Mutation = {} + , Resolver = { Query, Mutation, User } // ------ User Queries ------ // -Query.user = async (_, { id }) => User.scope('comments').findById(id) +Query.user = async (_, { id }) => + _User.findById(+id) -Query.users = async () => User.findAll() +Query.users = async () => + _User.findAll() Query.userByUsername = async (_, { username }) => - User.scope('full').findOne({ + _User.findOne({ where: { username } }) -// ------ Example Mutations ------ // -Mutation.createUser = async (_, ...userFields) => - User.create({ ...userFields }) +// ------ User Type Resolvers ------ // +User.examples = async user => + _Example.findAll({ where: { coderId: user.id } }) + +User.comments = async user => + _Comment.findAll({ where: { authorId: id } }) + + +// // ------ Example Mutations ------ // +Mutation.createUser = async (_, ...userFields) => + _User.create({ ...userFields }) + + export default Resolver \ No newline at end of file diff --git a/src/server/api/types/_Comment.js b/src/server/api/types/_Comment.js index fd76362..854fe3c 100644 --- a/src/server/api/types/_Comment.js +++ b/src/server/api/types/_Comment.js @@ -7,6 +7,7 @@ const _Comment = gql` author: User example: Example parent: Comment + children: [Comment] content: String! } diff --git a/src/server/api/types/_User.js b/src/server/api/types/_User.js index da3c996..ffe3c3d 100644 --- a/src/server/api/types/_User.js +++ b/src/server/api/types/_User.js @@ -1,5 +1,5 @@ import gql from 'graphql-tag' -import { _Example, _Comment } from '.' +import { _Example, _Comment } from './index' const _User = gql` type User { diff --git a/src/server/app/dev.js b/src/server/app/dev.js index 318f90a..db3a727 100644 --- a/src/server/app/dev.js +++ b/src/server/app/dev.js @@ -8,17 +8,17 @@ export default Router() .use(logger) /* --- Serve React App --- */ - .get('/bundle.js', (req, res) => { - res.sendFile(path.join(__dirname, '..', '..', '..', 'dist', 'bundle.js')) - }) + // .get('/bundle.js', (req, res) => { + // res.sendFile(path.join(__dirname, '..', '..', '..', 'dist', 'bundle.js')) + // }) /* --- Serve Assets --- */ - .use((req, res, next) => { - if (path.extname(req.path).length > 0) res.status(404).end() - else next(null) - }) + // .use((req, res, next) => { + // if (path.extname(req.path).length > 0) res.status(404).end() + // else next(null) + // }) /* --- Serve Root HTML --- */ - .get('/*', (req, res) => { - res.sendFile(path.join(__dirname, '..', '..', '..', 'dist', 'index.html')) - }) + // .get('/*', (req, res) => { + // res.sendFile(path.join(__dirname, '..', '..', '..', 'dist', 'index.html')) + // }) From a358ae1aecff9444310fffd369194c93253dd829 Mon Sep 17 00:00:00 2001 From: ev-dev Date: Thu, 9 Nov 2017 18:21:21 -0500 Subject: [PATCH 2/2] Feat: User resolvers now resolve comments and examples, example resolver does the same --- src/server/api/resolvers/comments.js | 17 ++++++++-------- src/server/api/resolvers/examples.js | 29 ++++++++++++++++++---------- src/server/api/resolvers/users.js | 18 ++++++++--------- src/server/api/types/_Example.js | 2 ++ webpack.config.js | 2 +- 5 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/server/api/resolvers/comments.js b/src/server/api/resolvers/comments.js index f94d6a8..0a167b5 100644 --- a/src/server/api/resolvers/comments.js +++ b/src/server/api/resolvers/comments.js @@ -5,6 +5,15 @@ const Comment = {} , Mutation = {} , Resolver = { Query, Mutation, Comment } + +// ------ Comment Type ------ // +Comment.children = async comment => + _Comment.findAll({ where: { parentId: comment.id } }) + +Comment.author = async comment => + _User.findById(+comment.authorId) + + // ------ Comment Queries ------ // Query.comment = async (_, { id }) => _Comment.findById(+id) @@ -51,14 +60,6 @@ Query.childComments = async (_, { parentId }) => include: [{ model: _User, as: 'author' }] }) - -// ------ Comment Type Resolvers ------ // -Comment.children = async comment => - _Comment.findAll({ where: { parentId: comment.id } }) - -Comment.author = async comment => - _User.findById(+comment.authorId) - // ------ Comment Mutations ------ // Mutation.createComment = async (_, ...commentFields) => _Comment.create({ ...commentFields }), diff --git a/src/server/api/resolvers/examples.js b/src/server/api/resolvers/examples.js index bc91c7d..b621698 100644 --- a/src/server/api/resolvers/examples.js +++ b/src/server/api/resolvers/examples.js @@ -1,17 +1,18 @@ import { Op } from 'sequelize' import { _Example, _User, _Comment } from '../../db' -const Query = {}, Mutation = {}, Resolver = { Query, Mutation } +const Example = {} + , Query = {} + , Mutation = {} + , Resolver = { Query, Mutation, Example } + +// ------ Example Type ------ // +Example.comments = async example => + _Comment.findAll({ where: { exampleId: example.id } }) + +Example.coder = async example => _User.findById(+example.coderId) // ------ Example Queries ------ // -Query.example = async (_, { id }) => - _Example.findById(+id, { - include: [{ - model: User, - as: 'coder' - }, { - model: Comment - }] - }) +Query.example = async (_, { id }) => _Example.findById(+id) Query.examples = async (_, { limit, offset, include, tag }) => { if (include) @@ -27,6 +28,14 @@ Query.examplesByCoder = async (_, { coderId }) => where: { coderId } }) +// FIX ME! +Query.examplesByTags = async (_, { tags }) => + _Example.findAll({ + where: { + [Op.contains]: [...tags] + } + }) + Query.searchExamples = async (_, { query }) => _Example.findAll({ where: { diff --git a/src/server/api/resolvers/users.js b/src/server/api/resolvers/users.js index c611d27..e486922 100644 --- a/src/server/api/resolvers/users.js +++ b/src/server/api/resolvers/users.js @@ -5,6 +5,13 @@ const User = {} , Mutation = {} , Resolver = { Query, Mutation, User } +// ------ User Type ------ // +User.examples = async user => + _Example.findAll({ where: { coderId: user.id } }) + +User.comments = async user => + _Comment.findAll({ where: { authorId: user.id } }) + // ------ User Queries ------ // Query.user = async (_, { id }) => _User.findById(+id) @@ -17,18 +24,9 @@ Query.userByUsername = async (_, { username }) => where: { username } }) -// ------ User Type Resolvers ------ // -User.examples = async user => - _Example.findAll({ where: { coderId: user.id } }) - -User.comments = async user => - _Comment.findAll({ where: { authorId: id } }) - - -// // ------ Example Mutations ------ // +// // ------ User Mutations ------ // Mutation.createUser = async (_, ...userFields) => _User.create({ ...userFields }) - export default Resolver \ No newline at end of file diff --git a/src/server/api/types/_Example.js b/src/server/api/types/_Example.js index ea2f05b..21df35c 100644 --- a/src/server/api/types/_Example.js +++ b/src/server/api/types/_Example.js @@ -18,6 +18,8 @@ const _Example = gql` examples(limit: Int, offset: Int): [Example] + examplesByTags(tags: [String!]!): [Example] + examplesByCoder(coderId: ID!): [Example] searchExamples(query: String!): [Example] diff --git a/webpack.config.js b/webpack.config.js index 2d8758f..6e24764 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -62,4 +62,4 @@ module.exports = { }) ] : [] -} \ No newline at end of file +}