Browse Source

+gql schema

Ivan Asmer 4 years ago
parent
commit
cc15274ba3
2 changed files with 144 additions and 43 deletions
  1. 59 0
      anon.js
  2. 85 43
      index.js

+ 59 - 0
anon.js

@@ -0,0 +1,59 @@
+const { buildSchema } = require('graphql');
+const jwt         = require('jsonwebtoken')
+
+module.exports = ({Savable, secret}) => {
+    class User extends Savable {
+        async getACL(){
+            return [this._id.toString(), "user"]
+        }
+    }
+    Savable.addClass(User)
+
+    const anonResolvers = {
+        createUser:async function ({login, password}){
+            let user =  await Savable.m.User.findOne({login, password})
+            if (user)
+                return null;
+            user = await (new User({login, password})).save()
+
+            user.___owner = user._id.toString()
+            user.___permissions = {
+                read: ["owner", "user"]
+            }
+
+            return await user.save()
+        },
+
+        async login({login, password}){
+            const user =  await Savable.m.User.findOne({login, password})
+            if (!user)
+                return null;
+
+            const token = jwt.sign({ sub: {id: user._id, login, acl: await user.getACL()}}, secret); //подписывам токен нашим ключем
+            return token
+        },
+
+        changePassword:async function ({login, password, newPassword}){
+            const user =  await Savable.m.User.findOne({login, password})
+            if (!user) return null;
+            user.password = newPassword;
+            return await user.save()
+        },
+    }
+
+    const anonSchema = buildSchema(`
+        type Query {
+            login(login: String!, password: String!): String
+        }
+        type Mutation {
+            createUser(login: String!, password: String!): User
+            changePassword(login: String!, password: String!, newPassword: String!): User
+        }
+
+        type User {
+             _id: String
+             login: String
+        }
+    `)
+    return {anonResolvers, anonSchema}
+}

+ 85 - 43
index.js

@@ -1,7 +1,7 @@
-const jwtSecret   = 'CbymrfGfnB'
+const jwtSecret   = 'AjnjLhjxM'
 
-const express = require('express');
-const express_graphql = require('express-graphql');
+const express           = require('express');
+const express_graphql   = require('express-graphql');
 
 const { buildSchema, printSchema } = require('graphql');
 const expand = require('mm-graphql/expand')
@@ -19,78 +19,120 @@ const expand = require('mm-graphql/expand')
              createdAt: String
              login: String
              nick : String
-             orders: [Order]
+             avatar: Image
+             likes: [Like]
+             likesCount: Int
         }
 
         input UserInput {
              _id: String
              login: String
              nick : String
+             avatar: ImageInput
         }
 
-        type Category {
+        type Like {
             _id: ID,
-            createdAt: String
-            name: String!,
-            goods: [Good]
+            post: Post,
+            comment: Comment,
+            direct: Direct,
+            user: User,
         }
-        input CategoryInput {
+
+        input LikeInput {
             _id: ID,
-            name: String,
-            goods: [GoodInput]
+            post: Post,
+            comment: CommentInput,
+            direct: DirectInput,
+            user: UserInput,
         }
 
-        type Good {
+        type Post {
             _id: ID,
             createdAt: String
-            name: String!,
-            description: String
-            price: Float
-            imgUrls: [String]
-            orderGoods: [OrderGood]
-            categories: [Category]
+            text: String,
+            images: [Image],
+            comments: [Comment],
+            directs: [Direct],
+            collections: [Collection]
+            likes: [Like]
+            likesCount: Int
         }
 
-        input GoodInput {
+        input PostInput {
             _id: ID,
-            name: String,
-            description: String
-            imgUrls: [String]
-            price: Float
-            categories: [CategoryInput]
+            text: String,
+            images: [ImageInput],
+            comments: [CommentInput],
+            directs: [DirectInput],
+            collections: [CollectionInput]
         }
 
+        type Image {
+            _id: ID,
+            text: String,
+            url: String,
+            originalFileName: String,
+            userAvatar: User,
+            posts: [Post],
+            directs: [Direct]
+        }
 
-        type OrderGood {
+        input ImageInput {
             _id: ID,
-            createdAt: String
-            price: Float,
-            count: Float,
-            good: Good,
-            order: Order
-            total: Float
+            userAvatar: User,
+            posts: [Post],
+            directs: [Direct]
         }
 
-        input OrderGoodInput {
+        type Comment {
             _id: ID,
-            count: Int,
-            good: GoodInput,
-            order: OrderInput
+            createdAt: String,
+            text: String,
+            post: Post,
+            answers: [Comment],
+            answerTo: Comment
+            likes: [Like]
+            likesCount: Int
         }
 
+        input CommentInput {
+            _id: ID,
+            text: String,
+            post: PostInput,
+            answers: [CommentInput],
+            answerTo: CommentInput
+        }
 
-        type Order {
-            _id: ID
-            createdAt: String
-            orderGoods: [OrderGood]
-            total: Float
+        type Direct {
+            _id: ID,
+            createdAt: String,
+            text: String,
+            post: Post,
+            image: Image,
+            likes: [Like]
+            likesCount: Int
         }
 
-        input OrderInput {
-            _id: ID
-            orderGoods: [OrderGoodInput]
+        input DirectInput {
+            _id: ID,
+            text: String,
+            post: PostInput,
+            image: ImageInput,
+            likes: [LikeInput]
         }
 
+        type Collection {
+            _id: ID,
+            text: String,
+            posts: [Post]
+        }
+
+        input CollectionInput {
+            _id: ID,
+            text: String,
+            posts: [Post]
+        }
     `);
 
     schema = expand(schema)