From 089c231c455250e4f7b0a44fdeb01c3a602fb024 Mon Sep 17 00:00:00 2001 From: Gabriel Marcos Date: Sat, 14 Jul 2018 03:03:18 -0300 Subject: [PATCH] Create schema --- src/config/config.json | 15 +++-- src/graphql/mutation.ts | 17 +++++ src/graphql/query.ts | 16 +++++ .../resource/comment/comment.schema.ts | 29 +++++++++ src/graphql/resource/post/post.schema.ts | 34 ++++++++++ src/graphql/resource/user/user.schema.ts | 45 +++++++++++++ src/graphql/schema.ts | 63 ++++++------------- src/index.ts | 5 +- src/models/PostModel.ts | 2 +- src/models/UserModel.ts | 4 +- src/models/index.ts | 8 ++- 11 files changed, 179 insertions(+), 59 deletions(-) create mode 100644 src/graphql/mutation.ts create mode 100644 src/graphql/query.ts create mode 100644 src/graphql/resource/comment/comment.schema.ts create mode 100644 src/graphql/resource/post/post.schema.ts create mode 100644 src/graphql/resource/user/user.schema.ts diff --git a/src/config/config.json b/src/config/config.json index 4a4fed3..572f743 100644 --- a/src/config/config.json +++ b/src/config/config.json @@ -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 } } diff --git a/src/graphql/mutation.ts b/src/graphql/mutation.ts new file mode 100644 index 0000000..09761e9 --- /dev/null +++ b/src/graphql/mutation.ts @@ -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 +} \ No newline at end of file diff --git a/src/graphql/query.ts b/src/graphql/query.ts new file mode 100644 index 0000000..3c30de8 --- /dev/null +++ b/src/graphql/query.ts @@ -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 +} \ No newline at end of file diff --git a/src/graphql/resource/comment/comment.schema.ts b/src/graphql/resource/comment/comment.schema.ts new file mode 100644 index 0000000..5f6801d --- /dev/null +++ b/src/graphql/resource/comment/comment.schema.ts @@ -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 +} \ No newline at end of file diff --git a/src/graphql/resource/post/post.schema.ts b/src/graphql/resource/post/post.schema.ts new file mode 100644 index 0000000..afe5a33 --- /dev/null +++ b/src/graphql/resource/post/post.schema.ts @@ -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 +} \ No newline at end of file diff --git a/src/graphql/resource/user/user.schema.ts b/src/graphql/resource/user/user.schema.ts new file mode 100644 index 0000000..3499dc0 --- /dev/null +++ b/src/graphql/resource/user/user.schema.ts @@ -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 +} \ No newline at end of file diff --git a/src/graphql/schema.ts b/src/graphql/schema.ts index ab1e4ff..15f180d 100644 --- a/src/graphql/schema.ts +++ b/src/graphql/schema.ts @@ -1,51 +1,26 @@ import { makeExecutableSchema } from 'graphql-tools' -const users: any[] = [ - { - id: 1, - name: 'Gabriel Marcos', - email: 'gabriel.marcos@gmail.com' - }, - { - id: 2, - name: 'Marcelo Montanher', - email: 'marcelo.montanher@gmail.com' - }, - { - id: 3, - name: 'Camila Caligari', - email: 'camila.caligari@gmail.com' - } -] - -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}) \ No newline at end of file +export default makeExecutableSchema({ + typeDefs: [ + SchemaDefinition, + Query, + Mutation, + commentType, + postTypes, + userTypes + ] +}) \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index e120b1b..5655fdb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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)) + }) \ No newline at end of file diff --git a/src/models/PostModel.ts b/src/models/PostModel.ts index 5f60fa1..5420a26 100644 --- a/src/models/PostModel.ts +++ b/src/models/PostModel.ts @@ -36,7 +36,7 @@ export default (Sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes): type: DataTypes.BLOB({ length: 'long' }), - allowNull: false, + allowNull: true } },{ tableName: 'posts' diff --git a/src/models/UserModel.ts b/src/models/UserModel.ts index 13a28df..508d14b 100644 --- a/src/models/UserModel.ts +++ b/src/models/UserModel.ts @@ -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 { @@ -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', diff --git a/src/models/index.ts b/src/models/index.ts index 58692dc..1e7a940 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -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] @@ -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, @@ -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) => {