Skip to content

Commit

Permalink
Create schema
Browse files Browse the repository at this point in the history
  • Loading branch information
gamarcos committed Jul 14, 2018
1 parent 7f58d12 commit 089c231
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 59 deletions.
15 changes: 9 additions & 6 deletions src/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@
"development": {
"username": "root",
"password": "qwerty",
"database": "database_development",
"database": "graphql_blog_development",
"host": "127.0.0.1",
"dialect": "mysql"
"dialect": "mysql",
"port": 3306
},
"test": {
"username": "root",
"password": "qwerty",
"database": "database_test",
"database": "graphql_blog_development",
"host": "127.0.0.1",
"dialect": "mysql"
"dialect": "mysql",
"port": 3306
},
"production": {
"username": "root",
"password": "qwerty",
"database": "database_production",
"database": "graphql_blog_development",
"host": "127.0.0.1",
"dialect": "mysql"
"dialect": "mysql",
"port": 3306
}
}
17 changes: 17 additions & 0 deletions src/graphql/mutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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 }
${ userMutations }
)
`

export {
Mutation
}
16 changes: 16 additions & 0 deletions src/graphql/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { userQueries } from './resource/user/user.schema'
import { postQueries } from './resource/post/post.schema'
import { commentQueries } from './resource/comment/comment.schema';


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

export {
Query
}
29 changes: 29 additions & 0 deletions src/graphql/resource/comment/comment.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const commentType = `
type Post {
id: ID!
commnet: String!
createdAt: String!
updatedAt: String!
user: User!
post: Post!
}
input CommentInput {
comment: String!
post: Int!,
user: Int!
}
`
const commentQueries = `
commentsByPost(post: ID!, first: Int, offset: Int): [ Comment! ]!
`
const commentMutation = `
createComment(input: CommentInput!): Comment
updatePost(id: ID!, input: CommentInput!): Comment
deletePost(id: ID!)
`
export {
commentType,
commentQueries,
commentMutation
}
34 changes: 34 additions & 0 deletions src/graphql/resource/post/post.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const postTypes = `
type Post {
id: ID!
title: String!
content: String!
photo: String
createdAt: String!
updatedAt: String!
author: User!
comments: [ Comment! ]
}
input PostInput {
title: String!
content: String!
photo: String
author: User!
}
`

const postQueries = `
posts(first: Int, offset: Int): [ Post! ]
post(id: ID!): Post
`
const postMutation = `
createPost(input: PostInput!): Post
updatePost(id: ID!, input: PostInput!): Post
deletePost(id: ID!)
`
export {
postTypes,
postQueries,
postMutation
}
45 changes: 45 additions & 0 deletions src/graphql/resource/user/user.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const userTypes = `
# User definition type
type User {
id: ID!
name: String!
email: String!
photo: String
createdAt: String!
updatedAt: String!
}
input UserCreateInput {
name: String!
email: String!
password: String!
}
input UserUpdateInput {
name: String!
email: String!
photo: String!
}
input UserUpdatePasswordInput {
password: String!
}
`

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

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

export {
userTypes,
userQueries,
userMutations
}
63 changes: 19 additions & 44 deletions src/graphql/schema.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,26 @@
import { makeExecutableSchema } from 'graphql-tools'

const users: any[] = [
{
id: 1,
name: 'Gabriel Marcos',
email: '[email protected]'
},
{
id: 2,
name: 'Marcelo Montanher',
email: '[email protected]'
},
{
id: 3,
name: 'Camila Caligari',
email: '[email protected]'
}
]

const typeDefs = `
type User {
id: ID!
name: String!
email: String!
}
import { Query } from "./query"
import { Mutation } from "./mutation"

type Query {
allUser: [User!]!
}
import { postTypes } from './resource/post/post.schema';
import { userTypes } from './resource/user/user.schema';
import { commentType } from './resource/comment/comment.schema';

type Mutation {
createUser(name: String!, email: String!): User
const SchemaDefinition = `
type: Schema {
query: Query,
mutation: Mutation
}
`

const resolvers = {
Query: {
allUser: () => users
},
Mutation: {
createUser: (parent, args) => {
const newUser = Object.assign({id: users.length + 1}, args)
users.push(newUser)
return newUser
}
}
}

export default makeExecutableSchema({typeDefs, resolvers})
export default makeExecutableSchema({
typeDefs: [
SchemaDefinition,
Query,
Mutation,
commentType,
postTypes,
userTypes
]
})
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import { normalizePort, onError, onListening } from './utils/utils'
const server = http.createServer(app)
const port = normalizePort(process.env.port || 3000)


db.sequelize.sync()
.then(() => {
server.listen(port)
server.on('error', onError(server))
server.on('listening', onListening(server))
})
server.on('listening', onListening(server))
})
2 changes: 1 addition & 1 deletion src/models/PostModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default (Sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes):
type: DataTypes.BLOB({
length: 'long'
}),
allowNull: false,
allowNull: true
}
},{
tableName: 'posts'
Expand Down
4 changes: 1 addition & 3 deletions src/models/UserModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as Sequelize from "sequelize"
import { genSaltSync, hashSync, compareSync } from 'bcryptjs'

import { BaseModelInterface } from "../interfaces/BaseModelInterface"
import { userInfo } from "os";
import { ModelsInterface } from "../interfaces/ModelsInterface";

export interface UserAttributes {
Expand Down Expand Up @@ -49,8 +48,7 @@ export default (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes):
type: DataTypes.BLOB({
length: 'long'
}),
allowNull: false,
defaultValue: null
allowNull: true
}
}, {
tableName: 'users',
Expand Down
8 changes: 5 additions & 3 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import * as path from 'path'
import * as Sequelize from 'sequelize'
import { DbConnection } from '../interfaces/DbConnectionInterface'

// FIX PROCESS ENV

const basename: string = path.basename(module.filename)
const env: string = process.env.NODE_ENV || 'development'
let config = require(path.resolve(`${__dirname}./../config/config.json`))[env]
Expand All @@ -13,6 +11,10 @@ let db = null
if (!db) {
db = {}

const operatorsAlias = false

config = Object.assign({operatorsAlias}, config)

const sequelize: Sequelize.Sequelize = new Sequelize(
config.database,
config.username,
Expand All @@ -21,7 +23,7 @@ if (!db) {
)

fs.readdirSync(__dirname)
.filter((file: string)=> {
.filter((file: string) => {
return (file.indexOf('.') !== 0 && (file !== basename) && file.slice(-3) === '.js')
})
.forEach((file: string) => {
Expand Down

0 comments on commit 089c231

Please sign in to comment.