mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Change API: create/relate
Separating the relations from mere properties removes boilerplate code.
This commit is contained in:
parent
e51817c849
commit
eb22555797
@ -25,16 +25,30 @@ const builders = {
|
||||
'tag': require('./tags.js').default
|
||||
}
|
||||
|
||||
const relationBuilders = {
|
||||
'user': require('./users.js').relate
|
||||
}
|
||||
|
||||
const buildMutation = (model, parameters) => {
|
||||
return builders[model](parameters)
|
||||
}
|
||||
|
||||
const buildRelationMutation = (model, type, parameters) => {
|
||||
return relationBuilders[model](type, parameters)
|
||||
}
|
||||
|
||||
const create = (model, parameters, options) => {
|
||||
const graphQLClient = new GraphQLClient(seedServerHost, options)
|
||||
const mutation = buildMutation(model, parameters)
|
||||
return graphQLClient.request(mutation)
|
||||
}
|
||||
|
||||
const relate = (model, type, parameters, options) => {
|
||||
const graphQLClient = new GraphQLClient(seedServerHost, options)
|
||||
const mutation = buildRelationMutation(model, type, parameters)
|
||||
return graphQLClient.request(mutation)
|
||||
}
|
||||
|
||||
const cleanDatabase = async () => {
|
||||
const session = driver.session()
|
||||
const cypher = 'MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r'
|
||||
@ -50,7 +64,9 @@ const cleanDatabase = async () => {
|
||||
export {
|
||||
driver,
|
||||
apolloClient,
|
||||
create,
|
||||
buildMutation,
|
||||
buildRelationMutation,
|
||||
create,
|
||||
relate,
|
||||
cleanDatabase
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import faker from 'faker'
|
||||
|
||||
export default function (params) {
|
||||
export default function create (params) {
|
||||
const {
|
||||
id = `u${faker.random.number()}`,
|
||||
name = faker.name.findName(),
|
||||
@ -65,3 +65,18 @@ export default function (params) {
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
export function relate(type, params) {
|
||||
switch(type){
|
||||
case 'friends':
|
||||
const { from, to } = params
|
||||
return `
|
||||
mutation {
|
||||
${from}_friends_${to}: AddUserFriends(
|
||||
from: { id: "${from}" },
|
||||
to: { id: "${to}" }
|
||||
) { from { id } }
|
||||
}
|
||||
`
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { create, apolloClient, seedServerHost as host } from './factories'
|
||||
import { create, relate, apolloClient, seedServerHost as host } from './factories'
|
||||
import { authenticatedHeaders } from '../jest/helpers.js'
|
||||
import gql from 'graphql-tag'
|
||||
import asyncForEach from '../helpers/asyncForEach'
|
||||
@ -26,6 +26,12 @@ import seed from './data'
|
||||
create('user', { id: 'u7', name: 'Dagobert', role: 'user', badgeIds: ['b1', 'b2'], blacklistedUserIds: ['u4', 'u5', 'u6'], email: 'dagobert@example.org' })
|
||||
])
|
||||
|
||||
await Promise.all([
|
||||
relate('user', 'friends', { from: 'u1', to: 'u2' }),
|
||||
relate('user', 'friends', { from: 'u1', to: 'u3' }),
|
||||
relate('user', 'friends', { from: 'u2', to: 'u3' })
|
||||
])
|
||||
|
||||
const headers = await Promise.all([
|
||||
authenticatedHeaders({ email: 'admin@example.org', password: '1234' }, host),
|
||||
authenticatedHeaders({ email: 'moderator@example.org', password: '1234' }, host),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user