mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Clarify conversion to Cypher literal map for use as 'MATCH' parameter by making a resolver helper function
This commit is contained in:
parent
b7d4d506e3
commit
7bb3a05b07
@ -4,7 +4,10 @@ import CONFIG from '../../config'
|
||||
import { CATEGORIES_MIN, CATEGORIES_MAX } from '../../constants/categories'
|
||||
import { DESCRIPTION_WITHOUT_HTML_LENGTH_MIN } from '../../constants/groups'
|
||||
import { removeHtmlTags } from '../../middleware/helpers/cleanHtml.js'
|
||||
import Resolver from './helpers/Resolver'
|
||||
import Resolver, {
|
||||
removeUndefinedNullValuesFromObject,
|
||||
convertObjectToCypherMapLiteral,
|
||||
} from './helpers/Resolver'
|
||||
import { mergeImage } from './images/images'
|
||||
|
||||
export default {
|
||||
@ -12,20 +15,10 @@ export default {
|
||||
Group: async (_object, params, context, _resolveInfo) => {
|
||||
const { isMember, id, slug } = params
|
||||
const matchParams = { id, slug }
|
||||
Object.keys(matchParams).forEach((key) => {
|
||||
if ([undefined, null].includes(matchParams[key])) {
|
||||
delete matchParams[key]
|
||||
}
|
||||
})
|
||||
removeUndefinedNullValuesFromObject(matchParams)
|
||||
const session = context.driver.session()
|
||||
const readTxResultPromise = session.readTransaction(async (txc) => {
|
||||
const matchParamsEntries = Object.entries(matchParams)
|
||||
let groupMatchParamsCypher = ''
|
||||
matchParamsEntries.forEach((ele, index) => {
|
||||
groupMatchParamsCypher += index === 0 ? ' {' : ''
|
||||
groupMatchParamsCypher += `${ele[0]}: "${ele[1]}"`
|
||||
groupMatchParamsCypher += index < matchParamsEntries.length - 1 ? ', ' : '}'
|
||||
})
|
||||
const groupMatchParamsCypher = convertObjectToCypherMapLiteral(matchParams)
|
||||
let groupCypher
|
||||
if (isMember === true) {
|
||||
groupCypher = `
|
||||
|
||||
@ -121,3 +121,24 @@ export default function Resolver(type, options = {}) {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export const removeUndefinedNullValuesFromObject = (obj) => {
|
||||
Object.keys(obj).forEach((key) => {
|
||||
if ([undefined, null].includes(obj[key])) {
|
||||
delete obj[key]
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const convertObjectToCypherMapLiteral = (params) => {
|
||||
// I have found no other way yet. maybe "apoc.convert.fromJsonMap(key)" can help, but couldn't get it how, see: https://stackoverflow.com/questions/43217823/neo4j-cypher-inline-conversion-of-string-to-a-map
|
||||
// result looks like: '{id: "g0", slug: "yoga"}'
|
||||
const paramsEntries = Object.entries(params)
|
||||
let mapLiteral = ''
|
||||
paramsEntries.forEach((ele, index) => {
|
||||
mapLiteral += index === 0 ? ' {' : ''
|
||||
mapLiteral += `${ele[0]}: "${ele[1]}"`
|
||||
mapLiteral += index < paramsEntries.length - 1 ? ', ' : '}'
|
||||
})
|
||||
return mapLiteral
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user