Skip to content

Commit

Permalink
Create resolvers to model
Browse files Browse the repository at this point in the history
  • Loading branch information
gamarcos committed Jul 15, 2018
1 parent 089c231 commit 4242a51
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 64 deletions.
21 changes: 16 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as express from 'express'
import * as graphqlHTTP from 'express-graphql'
import schema from './graphql/schema';

import db from './models'
import schema from './graphql/schema'

class App {
public express: express.Application
Expand All @@ -11,10 +13,19 @@ class App {
}

private middleware(): void {
this.express.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: process.env.NODE_ENV === 'development'
}))
this.express.use('/graphql',

(req, res, next) => {
req['context'] = {}
req['context'].db = db
next()
},
graphqlHTTP((req) => ({
schema: schema,
graphiql: process.env.NODE_ENV === 'development',
context: req['context']
}))
)
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/graphql/mutation.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { commentMutations } from './resource/comment/comment.schema'
import { postMutations } from './resource/post/post.schema'
import { userMutations } from './resource/user/user.schema'
import { postMutation } from './resource/post/post.schema'
import { commentMutation } from './resource/comment/comment.schema';


const Mutation = `
type: Mutation(
${ commentMutation }
${ postMutation }
type Mutation {
${ commentMutations }
${ postMutations }
${ userMutations }
)
}
`

export {
Expand Down
9 changes: 4 additions & 5 deletions src/graphql/query.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { userQueries } from './resource/user/user.schema'
import { commentQueries } from './resource/comment/comment.schema'
import { postQueries } from './resource/post/post.schema'
import { commentQueries } from './resource/comment/comment.schema';

import { userQueries } from './resource/user/user.schema'

const Query = `
type: Query(
type Query {
${ commentQueries }
${ postQueries }
${ userQueries }
)
}
`

export {
Expand Down
Empty file.
28 changes: 16 additions & 12 deletions src/graphql/resource/comment/comment.schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const commentType = `
type Post {
const commentTypes = `
type Comment {
id: ID!
commnet: String!
comment: String!
createdAt: String!
updatedAt: String!
user: User!
Expand All @@ -10,20 +11,23 @@ const commentType = `
input CommentInput {
comment: String!
post: Int!,
user: Int!
post: Int!
}
`

const commentQueries = `
commentsByPost(post: ID!, first: Int, offset: Int): [ Comment! ]!
commentsByPost(postId: ID!, first: Int, offset: Int): [ Comment! ]!
`
const commentMutation = `

const commentMutations = `
createComment(input: CommentInput!): Comment
updatePost(id: ID!, input: CommentInput!): Comment
deletePost(id: ID!)
updateComment(id: ID!, input: CommentInput!): Comment
deleteComment(id: ID!): Boolean
`

export {
commentType,
commentTypes,
commentQueries,
commentMutation
}
commentMutations
}
42 changes: 42 additions & 0 deletions src/graphql/resource/post/post.resovers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { GraphQLResolveInfo } from "graphql"

import { DbConnection } from "../../../interfaces/DbConnectionInterface"
import { PostInterface } from "../../../models/PostModel";

export const postResolvers = {

Post: {

author:(post, args, {db}: {db: DbConnection}, info: GraphQLResolveInfo) => {
return db.User.findById(post.get('author'))
},

comments:(post, {first = 10, offset = 0}, {db}: {db: DbConnection}, info: GraphQLResolveInfo) => {
return db.Comment
.findAll({
where: { post: post.get('id') },
limit: first,
offset: offset
})
}
},

Query: {
posts: (parent, {first = 10, offset = 0}, {db}: {db: DbConnection}, info: GraphQLResolveInfo) => {
return db.Post
.findAll({
limit: first,
offset: offset
})
},

post: (parent, {id}, {db}: {db: DbConnection}, info: GraphQLResolveInfo) => {
return db.Post
.findById(id)
.then((post: PostInterface) => {
if (!post) throw new Error(`Post with id ${id} not found!`)
return post
})
}
}
}
21 changes: 12 additions & 9 deletions src/graphql/resource/post/post.schema.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
const postTypes = `
type Post {
id: ID!
title: String!
content: String!
photo: String
photo: String!
createdAt: String!
updatedAt: String!
updated: String!
author: User!
comments: [ Comment! ]
comments: [ Comment! ]!
}
input PostInput {
title: String!
content: String!
photo: String
author: User!
photo: String!
}
`

const postQueries = `
posts(first: Int, offset: Int): [ Post! ]
posts(first: Int, offset: Int): [ Post! ]!
post(id: ID!): Post
`
const postMutation = `

const postMutations = `
createPost(input: PostInput!): Post
updatePost(id: ID!, input: PostInput!): Post
deletePost(id: ID!)
deletePost(id: ID!): Boolean
`

export {
postTypes,
postQueries,
postMutation
postMutations
}
88 changes: 88 additions & 0 deletions src/graphql/resource/user/user.resolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { GraphQLResolveInfo } from "../../../../node_modules/@types/graphql"
import { Transaction } from "../../../../node_modules/@types/sequelize"

import { DbConnection } from "../../../interfaces/DbConnectionInterface"
import { UserInstance } from "../../../models/UserModel"

export const userResolvers = {

User: {
posts: (user: UserInstance, {first = 10, offset = 0}, {db}: {db: DbConnection}, info: GraphQLResolveInfo) => {
return db.Post
.findAll({
where: {author: user.get('id')},
limit: first,
offset: offset
})
}
},

Query: {
users: (parent, {first = 10, offset = 0}, {db}: {db: DbConnection}, info: GraphQLResolveInfo) => {
return db.User
.findAll({
limit: first,
offset: offset
})
},

user: (parent, {id}, {db}: {db: DbConnection}, info: GraphQLResolveInfo) => {
return db.User
.find(id)
.then((user: UserInstance) => {
if (!user) {
throw new Error(`User with ${id} not found!`)
return user
}
})
}
},

Mutation: {

createUser: (parent, {input}, {db}: {db: DbConnection}, info: GraphQLResolveInfo) => {
return db.sequelize.transaction((t: Transaction) => {
return db.User.create(input, {transaction: t})
})
},

updateUser: (parent, {id, input}, {db}: {db: DbConnection}, info: GraphQLResolveInfo) => {
id = parseInt(id)
return db.sequelize.transaction((t: Transaction) => {
return db.User
.findById(id)
.then((user: UserInstance) => {
if (!user) throw new Error(`User with ${id} not found!`)
return user.update(input, {transaction: t})
})
})
},

updateUserPassword: (parent, {id, input}, {db}: {db: DbConnection}, info: GraphQLResolveInfo) => {
id = parseInt(id)
return db.sequelize.transaction((t: Transaction) => {
return db.User
.findById(id)
.then((user: UserInstance) => {
if (!user) throw new Error(`User with ${id} not found!`)
return user.update(input, {transaction: t})
.then((user: UserInstance) => !!user)
})
})
},

deleteuser: (parent, {id}, {db}: {db: DbConnection}, info: GraphQLResolveInfo) => {
id = parseInt(id)
return db.sequelize.transaction((t: Transaction) => {
return db.User
.findById(id)
.then((user: UserInstance) => {
if (!user) throw new Error(`User with ${id} not found!`)
return user.destroy({transaction: t})
.then(user => !!user)
})
})
}
}

}
15 changes: 9 additions & 6 deletions src/graphql/resource/user/user.schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const userTypes = `
# User definition type
type User {
id: ID!
Expand All @@ -7,6 +8,7 @@ const userTypes = `
photo: String
createdAt: String!
updatedAt: String!
posts(first: Int, offset: Int): [ Post! ]!
}
input UserCreateInput {
Expand All @@ -27,15 +29,16 @@ const userTypes = `
`

const userQueries = `
users(first: Int, offset: Int): [ User! ]
user(id: ID!): User
users(first: Int, offset: Int): [ User! ]!
user(id: ID!): User
currentUser: User
`

const userMutations = `
createUser(input: UserCreateInput()): User)
updateUser(id: ID!, input: UserUpdateInput): User
UserUpdatePassword(id: ID!, input: UserUpdatePasswordInput): Boolean
deleteUser(id: ID!): Boolean
createUser(input: UserCreateInput!): User
updateUser(input: UserUpdateInput!): User
updateUserPassword(input: UserUpdatePasswordInput!): Boolean
deleteUser: Boolean
`

export {
Expand Down
16 changes: 8 additions & 8 deletions src/graphql/schema.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { makeExecutableSchema } from 'graphql-tools'

import { Query } from "./query"
import { Mutation } from "./mutation"
import { Query } from './query'
import { Mutation } from './mutation'

import { postTypes } from './resource/post/post.schema';
import { userTypes } from './resource/user/user.schema';
import { commentType } from './resource/comment/comment.schema';
import { commentTypes } from './resource/comment/comment.schema'
import { postTypes } from './resource/post/post.schema'
import { userTypes } from './resource/user/user.schema'

const SchemaDefinition = `
type: Schema {
query: Query,
type Schema {
query: Query
mutation: Mutation
}
`
Expand All @@ -19,7 +19,7 @@ export default makeExecutableSchema({
SchemaDefinition,
Query,
Mutation,
commentType,
commentTypes,
postTypes,
userTypes
]
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/BaseModelInterface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ModelsInterface } from "./ModelsInterface";
import { ModelsInterface } from "./ModelsInterface"

export interface BaseModelInterface {
prototype?
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/DbConnectionInterface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Sequelize from 'sequelize'
import { ModelsInterface } from './ModelsInterface';
import { ModelsInterface } from './ModelsInterface'

export interface DbConnection extends ModelsInterface {
sequelize: Sequelize.Sequelize
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/ModelsInterface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UserModel } from "../models/UserModel";
import { PostModel } from "../models/PostModel";
import { CommentModel } from "../models/CommentModel";
import { UserModel } from "../models/UserModel"
import { PostModel } from "../models/PostModel"
import { CommentModel } from "../models/CommentModel"

export interface ModelsInterface {
User: UserModel
Expand Down
Loading

0 comments on commit 4242a51

Please sign in to comment.