Robert Schäfer 0811ca8248 Massive refactoring of factories
cc @mattwr18 @tirokk @ulfgebhardt @appinteractive

The factories don't use string interpolation now but they use variables.
This resolves some errors of escaping strings when you send html along
with `post.content`. It is much cleaner, too.
2019-04-18 16:41:34 +02:00

133 lines
3.6 KiB
JavaScript

import { GraphQLClient, request } from 'graphql-request'
import { getDriver } from '../../bootstrap/neo4j'
import createBadge from './badges.js'
import createUser from './users.js'
import createOrganization from './organizations.js'
import createPost from './posts.js'
import createComment from './comments.js'
import createCategory from './categories.js'
import createTag from './tags.js'
import createReport from './reports.js'
import createNotification from './notifications.js'
export const seedServerHost = 'http://127.0.0.1:4001'
const authenticatedHeaders = async ({ email, password }, host) => {
const mutation = `
mutation {
login(email:"${email}", password:"${password}")
}`
const response = await request(host, mutation)
return {
authorization: `Bearer ${response.login}`
}
}
const factories = {
Badge: createBadge,
User: createUser,
Organization: createOrganization,
Post: createPost,
Comment: createComment,
Category: createCategory,
Tag: createTag,
Report: createReport,
Notification: createNotification
}
export const cleanDatabase = async (options = {}) => {
const { driver = getDriver() } = options
const session = driver.session()
const cypher = 'MATCH (n) DETACH DELETE n'
try {
return await session.run(cypher)
} catch (error) {
throw error
} finally {
session.close()
}
}
export default function Factory (options = {}) {
const {
neo4jDriver = getDriver(),
seedServerHost = 'http://127.0.0.1:4001'
} = options
const graphQLClient = new GraphQLClient(seedServerHost)
const result = {
neo4jDriver,
seedServerHost,
graphQLClient,
factories,
lastResponse: null,
async authenticateAs ({ email, password }) {
const headers = await authenticatedHeaders(
{ email, password },
seedServerHost
)
this.lastResponse = headers
this.graphQLClient = new GraphQLClient(seedServerHost, { headers })
return this
},
async create (node, properties) {
const { mutation, variables } = this.factories[node](properties)
this.lastResponse = await this.graphQLClient.request(mutation, variables)
return this
},
async relate (node, relationship, properties) {
const { from, to } = properties
const mutation = `
mutation {
Add${node}${relationship}(
from: { id: "${from}" },
to: { id: "${to}" }
) { from { id } }
}
`
this.lastResponse = await this.graphQLClient.request(mutation)
return this
},
async mutate (mutation, variables) {
this.lastResponse = await this.graphQLClient.request(mutation, variables)
return this
},
async shout (properties) {
const { id, type } = properties
const mutation = `
mutation {
shout(
id: "${id}",
type: ${type}
)
}
`
this.lastResponse = await this.graphQLClient.request(mutation)
return this
},
async follow (properties) {
const { id, type } = properties
const mutation = `
mutation {
follow(
id: "${id}",
type: ${type}
)
}
`
this.lastResponse = await this.graphQLClient.request(mutation)
return this
},
async cleanDatabase () {
this.lastResponse = await cleanDatabase({ driver: this.neo4jDriver })
return this
}
}
result.authenticateAs.bind(result)
result.create.bind(result)
result.relate.bind(result)
result.mutate.bind(result)
result.cleanDatabase.bind(result)
return result
}