From 5c88037d5ca82d6d7a7bb83948177de4ac58c3b4 Mon Sep 17 00:00:00 2001 From: Grzegorz Leoniec Date: Thu, 3 Jan 2019 15:27:26 +0100 Subject: [PATCH] WIP - first try to create a report mutation which attaches nodes internally --- src/graphql-schema.js | 7 ++++ src/middleware/permissionsMiddleware.js | 9 +++-- src/schema.graphql | 22 ++++++++++++ src/seed/data/index.js | 4 ++- src/seed/data/reports.js | 45 +++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 src/seed/data/reports.js diff --git a/src/graphql-schema.js b/src/graphql-schema.js index b52c9e232..7a3e65e3c 100644 --- a/src/graphql-schema.js +++ b/src/graphql-schema.js @@ -5,6 +5,7 @@ import bcrypt from 'bcryptjs' import zipObject from 'lodash/zipObject' import generateJwt from './jwt/generateToken' import { fixUrl } from './middleware/fixImageUrlsMiddleware' +import { neo4jgraphql } from 'neo4j-graphql-js' export const typeDefs = fs.readFileSync(process.env.GRAPHQL_SCHEMA || path.join(__dirname, 'schema.graphql')) @@ -121,6 +122,12 @@ export const resolvers = { session.close() throw new Error('No Such User exists.') + }, + report: async (parent, { resource, description }, { driver, req, user }, resolveInfo) => { + return neo4jgraphql(parent, { resource, description }, { driver, req, user }, resolveInfo) + // console.log('params', { resource, description }) + // console.log(`the user with the id ${user.id} tries to create a report on content of type ${resource.type} (${resource.id})`) + // throw new Error(`resource.id: ${resource.id}, resource.type: ${resource.type}, description: ${description}, user: ${user.id}`) } } } diff --git a/src/middleware/permissionsMiddleware.js b/src/middleware/permissionsMiddleware.js index 5b0cb87d2..a0051e856 100644 --- a/src/middleware/permissionsMiddleware.js +++ b/src/middleware/permissionsMiddleware.js @@ -1,11 +1,13 @@ import { rule, shield, allow } from 'graphql-shield' /* - * TODO: implement - * See: https://github.com/Human-Connection/Nitro-Backend/pull/40#pullrequestreview-180898363 +* TODO: implement +* See: https://github.com/Human-Connection/Nitro-Backend/pull/40#pullrequestreview-180898363 +*/ const isAuthenticated = rule()(async (parent, args, ctx, info) => { return ctx.user !== null }) +/* const isAdmin = rule()(async (parent, args, ctx, info) => { return ctx.user.role === 'ADMIN' }) @@ -26,8 +28,9 @@ const permissions = shield({ // customers: and(isAuthenticated, isAdmin) }, Mutation: { + report: isAuthenticated // addFruitToBasket: isAuthenticated - // CreateUser: allow + // CreateUser: allow, }, User: { email: isOwner, diff --git a/src/schema.graphql b/src/schema.graphql index bc6a3829c..978981c6f 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -5,6 +5,7 @@ type Query { type Mutation { login(email: String!, password: String!): LoggedInUser signup(email: String!, password: String!): Boolean! + report(resource: Resource!, description: String): Report } type LoggedInUser { id: ID! @@ -32,6 +33,17 @@ scalar Date scalar Time scalar DateTime +input Resource { + id: ID!, + type: _ResourceType! +} + +enum _ResourceType { + contribution + comment + user +} + enum VisibilityEnum { public friends @@ -138,6 +150,16 @@ type Comment { disabled: Boolean } +type Report { + id: ID! + author: User @relation(name: "REPORTED", direction: "IN") + description: String + createdAt: String + comment: Comment @relation(name: "REPORTED", direction: "OUT") + contribution: Post @relation(name: "REPORTED", direction: "OUT") + user: User @relation(name: "REPORTED", direction: "OUT") +} + type Category { id: ID! name: String! diff --git a/src/seed/data/index.js b/src/seed/data/index.js index e66342fe3..e67ceb59e 100644 --- a/src/seed/data/index.js +++ b/src/seed/data/index.js @@ -15,7 +15,9 @@ const seed = { Organization: require('./organizations.js').default, Post: require('./posts.js').default, Comment: require('./comments.js').default, - UserShouts: require('./users-shouts.js').default + UserShouts: require('./users-shouts.js').default, + + Reports: require('./reports.js').default } let data = {} diff --git a/src/seed/data/reports.js b/src/seed/data/reports.js new file mode 100644 index 000000000..939fe087b --- /dev/null +++ b/src/seed/data/reports.js @@ -0,0 +1,45 @@ +export default function (data) { + return ` + mutation { + r1: CreateReport(id: "r1", description: "Bad Stuff") { + id + } + r2: CreateReport(id: "r2", description: "Please remove this sh**") { + id + } + r3: CreateReport(id: "r3", description: "The user have misbehaved!") { + id + } + ra1: AddReportAuthor(from: { id: "u1" }, to: { id: "r1" }) { + from { + id + } + } + ra2: AddReportAuthor(from: { id: "u2" }, to: { id: "r2" }) { + from { + id + } + } + ra3: AddReportAuthor(from: { id: "u3" }, to: { id: "r3" }) { + from { + id + } + } + rc1: AddReportContribution(from: { id: "r1" }, to: { id: "p2" }) { + from { + id + } + } + rc2: AddReportComment(from: { id: "r2" }, to: { id: "c2" }) { + from { + id + } + } + rc3: AddReportUser(from: { id: "r3" }, to: { id: "u4" }) { + from { + id + } + } + } + ` +}