From d7193ab3b6dddd624a10181bcb19dded1d9d4aab Mon Sep 17 00:00:00 2001 From: roschaefer Date: Tue, 17 Dec 2019 11:21:25 +0100 Subject: [PATCH 1/2] Update neo4j-driver Fix API changes. Also close the session in resolver only and refactor userMiddleware to become a part of the resolver. --- backend/package.json | 2 +- backend/src/bootstrap/neo4j.js | 2 +- backend/src/middleware/index.js | 3 - backend/src/middleware/user/userMiddleware.js | 16 ----- .../resolvers/helpers/databaseLogger.js | 4 +- backend/src/schema/resolvers/registration.js | 8 ++- backend/src/schema/resolvers/users.js | 2 + .../resolvers/users/location.js} | 61 ++++++++----------- .../resolvers/users/location.spec.js} | 8 +-- backend/yarn.lock | 19 +++++- package.json | 2 +- yarn.lock | 22 +++++++ 12 files changed, 82 insertions(+), 67 deletions(-) delete mode 100644 backend/src/middleware/user/userMiddleware.js rename backend/src/{middleware/nodes/locations.js => schema/resolvers/users/location.js} (76%) rename backend/src/{middleware/user/userMiddleware.spec.js => schema/resolvers/users/location.spec.js} (96%) diff --git a/backend/package.json b/backend/package.json index f74697e3f..95d8cf6f2 100644 --- a/backend/package.json +++ b/backend/package.json @@ -80,7 +80,7 @@ "metascraper-youtube": "^5.8.9", "minimatch": "^3.0.4", "mustache": "^3.1.0", - "neo4j-driver": "~1.7.6", + "neo4j-driver": "^4.0.1", "neo4j-graphql-js": "^2.10.2", "neode": "^0.3.6", "node-fetch": "~2.6.0", diff --git a/backend/src/bootstrap/neo4j.js b/backend/src/bootstrap/neo4j.js index 404e8a2c0..6d46a0279 100644 --- a/backend/src/bootstrap/neo4j.js +++ b/backend/src/bootstrap/neo4j.js @@ -1,4 +1,4 @@ -import { v1 as neo4j } from 'neo4j-driver' +import neo4j from 'neo4j-driver' import CONFIG from './../config' import Neode from 'neode' import models from '../models' diff --git a/backend/src/middleware/index.js b/backend/src/middleware/index.js index 9c68d8c00..ac8ebd1f6 100644 --- a/backend/src/middleware/index.js +++ b/backend/src/middleware/index.js @@ -7,7 +7,6 @@ import sluggify from './sluggifyMiddleware' import excerpt from './excerptMiddleware' import xss from './xssMiddleware' import permissions from './permissionsMiddleware' -import user from './user/userMiddleware' import includedFields from './includedFieldsMiddleware' import orderBy from './orderByMiddleware' import validation from './validation/validationMiddleware' @@ -28,7 +27,6 @@ export default schema => { hashtags, xss, softDelete, - user, includedFields, orderBy, email, @@ -46,7 +44,6 @@ export default schema => { 'hashtags', 'xss', 'softDelete', - 'user', 'includedFields', 'orderBy', ] diff --git a/backend/src/middleware/user/userMiddleware.js b/backend/src/middleware/user/userMiddleware.js deleted file mode 100644 index 2ca61e69f..000000000 --- a/backend/src/middleware/user/userMiddleware.js +++ /dev/null @@ -1,16 +0,0 @@ -import createOrUpdateLocations from '../nodes/locations' - -export default { - Mutation: { - SignupVerification: async (resolve, root, args, context, info) => { - const result = await resolve(root, args, context, info) - await createOrUpdateLocations(result.id, args.locationName, context.driver) - return result - }, - UpdateUser: async (resolve, root, args, context, info) => { - const result = await resolve(root, args, context, info) - await createOrUpdateLocations(args.id, args.locationName, context.driver) - return result - }, - }, -} diff --git a/backend/src/schema/resolvers/helpers/databaseLogger.js b/backend/src/schema/resolvers/helpers/databaseLogger.js index 1e97b4d72..fac1a5c4a 100644 --- a/backend/src/schema/resolvers/helpers/databaseLogger.js +++ b/backend/src/schema/resolvers/helpers/databaseLogger.js @@ -3,8 +3,8 @@ const debugCypher = Debug('human-connection:neo4j:cypher') const debugStats = Debug('human-connection:neo4j:stats') export default function log(response) { - const { statement, counters, resultConsumedAfter, resultAvailableAfter } = response.summary - const { text, parameters } = statement + const { counters, resultConsumedAfter, resultAvailableAfter, query } = response.summary + const { text, parameters } = query debugCypher('%s', text) debugCypher('%o', parameters) debugStats('%o', counters) diff --git a/backend/src/schema/resolvers/registration.js b/backend/src/schema/resolvers/registration.js index 1a6bda1c8..e03f294cd 100644 --- a/backend/src/schema/resolvers/registration.js +++ b/backend/src/schema/resolvers/registration.js @@ -5,6 +5,7 @@ import encryptPassword from '../../helpers/encryptPassword' import generateNonce from './helpers/generateNonce' import existingEmailAddress from './helpers/existingEmailAddress' import normalizeEmail from './helpers/normalizeEmail' +import createOrUpdateLocations from './users/location' const neode = getNeode() @@ -22,7 +23,9 @@ export default { throw new UserInputError(e.message) } }, - SignupVerification: async (_parent, args) => { + SignupVerification: async (_parent, args, context) => { + const { driver } = context + const session = driver.session() const { termsAndConditionsAgreedVersion } = args const regEx = new RegExp(/^[0-9]+\.[0-9]+\.[0-9]+$/g) if (!regEx.test(termsAndConditionsAgreedVersion)) { @@ -51,11 +54,14 @@ export default { emailAddress.relateTo(user, 'belongsTo'), emailAddress.update({ verifiedAt: new Date().toISOString() }), ]) + await createOrUpdateLocations(args.id, args.locationName, session) return user.toJson() } catch (e) { if (e.code === 'Neo.ClientError.Schema.ConstraintValidationFailed') throw new UserInputError('User with this slug already exists!') throw new UserInputError(e.message) + } finally { + session.close() } }, }, diff --git a/backend/src/schema/resolvers/users.js b/backend/src/schema/resolvers/users.js index be9a69e80..0b3f13631 100644 --- a/backend/src/schema/resolvers/users.js +++ b/backend/src/schema/resolvers/users.js @@ -4,6 +4,7 @@ import { getNeode } from '../../bootstrap/neo4j' import { UserInputError, ForbiddenError } from 'apollo-server' import Resolver from './helpers/Resolver' import log from './helpers/databaseLogger' +import createOrUpdateLocations from './users/location' const neode = getNeode() @@ -127,6 +128,7 @@ export default { }) try { const [user] = await writeTxResultPromise + await createOrUpdateLocations(params.id, params.locationName, session) return user } catch (error) { throw new UserInputError(error.message) diff --git a/backend/src/middleware/nodes/locations.js b/backend/src/schema/resolvers/users/location.js similarity index 76% rename from backend/src/middleware/nodes/locations.js rename to backend/src/schema/resolvers/users/location.js index 47262d7ba..3f3638bf5 100644 --- a/backend/src/middleware/nodes/locations.js +++ b/backend/src/schema/resolvers/users/location.js @@ -2,8 +2,8 @@ import request from 'request' import { UserInputError } from 'apollo-server' import isEmpty from 'lodash/isEmpty' import Debug from 'debug' -import asyncForEach from '../../helpers/asyncForEach' -import CONFIG from './../../config' +import asyncForEach from '../../../helpers/asyncForEach' +import CONFIG from '../../../config' const debug = Debug('human-connection:location') @@ -57,16 +57,12 @@ const createLocation = async (session, mapboxData) => { } mutation += ' RETURN l.id' - try { - await session.writeTransaction(transaction => { - return transaction.run(mutation, data) - }) - } finally { - session.close() - } + await session.writeTransaction(transaction => { + return transaction.run(mutation, data) + }) } -const createOrUpdateLocations = async (userId, locationName, driver) => { +const createOrUpdateLocations = async (userId, locationName, session) => { if (isEmpty(locationName)) { return } @@ -99,7 +95,6 @@ const createOrUpdateLocations = async (userId, locationName, driver) => { throw new UserInputError('locationName is invalid') } - const session = driver.session() if (data.place_type.length > 1) { data.id = 'region.' + data.id.split('.')[1] } @@ -110,44 +105,36 @@ const createOrUpdateLocations = async (userId, locationName, driver) => { if (data.context) { await asyncForEach(data.context, async ctx => { await createLocation(session, ctx) - try { - await session.writeTransaction(transaction => { - return transaction.run( - ` + await session.writeTransaction(transaction => { + return transaction.run( + ` MATCH (parent:Location {id: $parentId}), (child:Location {id: $childId}) MERGE (child)<-[:IS_IN]-(parent) RETURN child.id, parent.id `, - { - parentId: parent.id, - childId: ctx.id, - }, - ) - }) - parent = ctx - } finally { - session.close() - } + { + parentId: parent.id, + childId: ctx.id, + }, + ) + }) + parent = ctx }) } // delete all current locations from user and add new location - try { - await session.writeTransaction(transaction => { - return transaction.run( - ` + await session.writeTransaction(transaction => { + return transaction.run( + ` MATCH (user:User {id: $userId})-[relationship:IS_IN]->(location:Location) DETACH DELETE relationship WITH user - MATCH (location:Location {id: $locationId}) - MERGE (user)-[:IS_IN]->(location) + MATCH (location:Location {id: $locationId}) + MERGE (user)-[:IS_IN]->(location) RETURN location.id, user.id `, - { userId: userId, locationId: data.id }, - ) - }) - } finally { - session.close() - } + { userId: userId, locationId: data.id }, + ) + }) } export default createOrUpdateLocations diff --git a/backend/src/middleware/user/userMiddleware.spec.js b/backend/src/schema/resolvers/users/location.spec.js similarity index 96% rename from backend/src/middleware/user/userMiddleware.spec.js rename to backend/src/schema/resolvers/users/location.spec.js index 4ca8fd89f..59d093afb 100644 --- a/backend/src/middleware/user/userMiddleware.spec.js +++ b/backend/src/schema/resolvers/users/location.spec.js @@ -1,8 +1,8 @@ -import { gql } from '../../helpers/jest' -import Factory from '../../seed/factories' -import { getNeode, getDriver } from '../../bootstrap/neo4j' +import { gql } from '../../../helpers/jest' +import Factory from '../../../seed/factories' +import { getNeode, getDriver } from '../../../bootstrap/neo4j' import { createTestClient } from 'apollo-server-testing' -import createServer from '../../server' +import createServer from '../../../server' const factory = Factory() const neode = getNeode() diff --git a/backend/yarn.lock b/backend/yarn.lock index f7cd30fa8..a6ccb543c 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -6152,7 +6152,7 @@ neo-async@^2.6.0: resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.1.tgz#ac27ada66167fa8849a6addd837f6b189ad2081c" integrity sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw== -neo4j-driver@^1.7.3, neo4j-driver@^1.7.6, neo4j-driver@~1.7.6: +neo4j-driver@^1.7.3, neo4j-driver@^1.7.6: version "1.7.6" resolved "https://registry.yarnpkg.com/neo4j-driver/-/neo4j-driver-1.7.6.tgz#eccb135a71eba9048c68717444593a6424cffc49" integrity sha512-6c3ALO3vYDfUqNoCy8OFzq+fQ7q/ab3LCuJrmm8P04M7RmyRCCnUtJ8IzSTGbiZvyhcehGK+azNDAEJhxPV/hA== @@ -6161,6 +6161,16 @@ neo4j-driver@^1.7.3, neo4j-driver@^1.7.6, neo4j-driver@~1.7.6: text-encoding-utf-8 "^1.0.2" uri-js "^4.2.2" +neo4j-driver@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/neo4j-driver/-/neo4j-driver-4.0.1.tgz#b25ffde0f16602e94c46d097e16a8bacbd773d5a" + integrity sha512-SqBhXyyyayVs5gV/6BrgdKbcmU5AsYQXkFAiYO74XAE8XPLJ1HVR/Hu4wjonAX7+70DsalkWEiFN1c6UaCVzlQ== + dependencies: + "@babel/runtime" "^7.5.5" + rxjs "^6.5.2" + text-encoding-utf-8 "^1.0.2" + uri-js "^4.2.2" + neo4j-graphql-js@^2.10.2: version "2.10.2" resolved "https://registry.yarnpkg.com/neo4j-graphql-js/-/neo4j-graphql-js-2.10.2.tgz#e67d1aab6441b28f276adf0f6d655720983b9b84" @@ -7391,6 +7401,13 @@ rxjs@^6.4.0: dependencies: tslib "^1.9.0" +rxjs@^6.5.2: + version "6.5.3" + resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.5.3.tgz#510e26317f4db91a7eb1de77d9dd9ba0a4899a3a" + integrity sha512-wuYsAYYFdWTAnAaPoKGNhfpWwKZbJW+HgAJ+mImp+Epl7BG8oNWBCTyRM8gba9k4lk8BgWdoYm21Mo/RYhhbgA== + dependencies: + tslib "^1.9.0" + safe-buffer@5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1: version "5.1.2" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" diff --git a/package.json b/package.json index 33c094201..f7610b11e 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "expect": "^24.9.0", "faker": "Marak/faker.js#master", "graphql-request": "^1.8.2", - "neo4j-driver": "^1.7.6", + "neo4j-driver": "^4.0.1", "neode": "^0.3.6", "npm-run-all": "^4.1.5", "slug": "^1.1.0" diff --git a/yarn.lock b/yarn.lock index 2bcb7250a..99c0b467b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3989,6 +3989,16 @@ neo4j-driver@^1.7.6: text-encoding-utf-8 "^1.0.2" uri-js "^4.2.2" +neo4j-driver@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/neo4j-driver/-/neo4j-driver-4.0.1.tgz#b25ffde0f16602e94c46d097e16a8bacbd773d5a" + integrity sha512-SqBhXyyyayVs5gV/6BrgdKbcmU5AsYQXkFAiYO74XAE8XPLJ1HVR/Hu4wjonAX7+70DsalkWEiFN1c6UaCVzlQ== + dependencies: + "@babel/runtime" "^7.5.5" + rxjs "^6.5.2" + text-encoding-utf-8 "^1.0.2" + uri-js "^4.2.2" + neode@^0.3.6: version "0.3.6" resolved "https://registry.yarnpkg.com/neode/-/neode-0.3.6.tgz#7daf791eff6d170e52c338ea2e5cca6fdc6bfbe3" @@ -4777,6 +4787,13 @@ rxjs@^5.0.0-beta.11: dependencies: symbol-observable "1.0.1" +rxjs@^6.5.2: + version "6.5.3" + resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.5.3.tgz#510e26317f4db91a7eb1de77d9dd9ba0a4899a3a" + integrity sha512-wuYsAYYFdWTAnAaPoKGNhfpWwKZbJW+HgAJ+mImp+Epl7BG8oNWBCTyRM8gba9k4lk8BgWdoYm21Mo/RYhhbgA== + dependencies: + tslib "^1.9.0" + safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1: version "5.1.2" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" @@ -5363,6 +5380,11 @@ tough-cookie@~2.4.3: psl "^1.1.24" punycode "^1.4.1" +tslib@^1.9.0: + version "1.10.0" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a" + integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ== + tty-browserify@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/tty-browserify/-/tty-browserify-0.0.1.tgz#3f05251ee17904dfd0677546670db9651682b811" From 33e8be755b66d81b8dbebf16a6c786dcecab0a2b Mon Sep 17 00:00:00 2001 From: roschaefer Date: Tue, 17 Dec 2019 11:21:25 +0100 Subject: [PATCH 2/2] Update neo4j-driver Fix API changes. Also close the session in resolver only and refactor userMiddleware to become a part of the resolver. --- backend/package.json | 2 +- backend/src/bootstrap/neo4j.js | 2 +- backend/src/middleware/index.js | 3 - backend/src/middleware/user/userMiddleware.js | 16 ----- .../resolvers/helpers/databaseLogger.js | 4 +- backend/src/schema/resolvers/registration.js | 8 ++- backend/src/schema/resolvers/users.js | 2 + .../resolvers/users/location.js} | 61 ++++++++----------- .../resolvers/users/location.spec.js} | 8 +-- backend/yarn.lock | 4 +- package.json | 2 +- yarn.lock | 22 +++++++ 12 files changed, 66 insertions(+), 68 deletions(-) delete mode 100644 backend/src/middleware/user/userMiddleware.js rename backend/src/{middleware/nodes/locations.js => schema/resolvers/users/location.js} (76%) rename backend/src/{middleware/user/userMiddleware.spec.js => schema/resolvers/users/location.spec.js} (96%) diff --git a/backend/package.json b/backend/package.json index 516da0e7e..1fc82b70f 100644 --- a/backend/package.json +++ b/backend/package.json @@ -80,7 +80,7 @@ "metascraper-youtube": "^5.8.12", "minimatch": "^3.0.4", "mustache": "^3.2.0", - "neo4j-driver": "~1.7.6", + "neo4j-driver": "^4.0.1", "neo4j-graphql-js": "^2.11.0", "neode": "^0.3.6", "node-fetch": "~2.6.0", diff --git a/backend/src/bootstrap/neo4j.js b/backend/src/bootstrap/neo4j.js index 404e8a2c0..6d46a0279 100644 --- a/backend/src/bootstrap/neo4j.js +++ b/backend/src/bootstrap/neo4j.js @@ -1,4 +1,4 @@ -import { v1 as neo4j } from 'neo4j-driver' +import neo4j from 'neo4j-driver' import CONFIG from './../config' import Neode from 'neode' import models from '../models' diff --git a/backend/src/middleware/index.js b/backend/src/middleware/index.js index 9c68d8c00..ac8ebd1f6 100644 --- a/backend/src/middleware/index.js +++ b/backend/src/middleware/index.js @@ -7,7 +7,6 @@ import sluggify from './sluggifyMiddleware' import excerpt from './excerptMiddleware' import xss from './xssMiddleware' import permissions from './permissionsMiddleware' -import user from './user/userMiddleware' import includedFields from './includedFieldsMiddleware' import orderBy from './orderByMiddleware' import validation from './validation/validationMiddleware' @@ -28,7 +27,6 @@ export default schema => { hashtags, xss, softDelete, - user, includedFields, orderBy, email, @@ -46,7 +44,6 @@ export default schema => { 'hashtags', 'xss', 'softDelete', - 'user', 'includedFields', 'orderBy', ] diff --git a/backend/src/middleware/user/userMiddleware.js b/backend/src/middleware/user/userMiddleware.js deleted file mode 100644 index 2ca61e69f..000000000 --- a/backend/src/middleware/user/userMiddleware.js +++ /dev/null @@ -1,16 +0,0 @@ -import createOrUpdateLocations from '../nodes/locations' - -export default { - Mutation: { - SignupVerification: async (resolve, root, args, context, info) => { - const result = await resolve(root, args, context, info) - await createOrUpdateLocations(result.id, args.locationName, context.driver) - return result - }, - UpdateUser: async (resolve, root, args, context, info) => { - const result = await resolve(root, args, context, info) - await createOrUpdateLocations(args.id, args.locationName, context.driver) - return result - }, - }, -} diff --git a/backend/src/schema/resolvers/helpers/databaseLogger.js b/backend/src/schema/resolvers/helpers/databaseLogger.js index 1e97b4d72..fac1a5c4a 100644 --- a/backend/src/schema/resolvers/helpers/databaseLogger.js +++ b/backend/src/schema/resolvers/helpers/databaseLogger.js @@ -3,8 +3,8 @@ const debugCypher = Debug('human-connection:neo4j:cypher') const debugStats = Debug('human-connection:neo4j:stats') export default function log(response) { - const { statement, counters, resultConsumedAfter, resultAvailableAfter } = response.summary - const { text, parameters } = statement + const { counters, resultConsumedAfter, resultAvailableAfter, query } = response.summary + const { text, parameters } = query debugCypher('%s', text) debugCypher('%o', parameters) debugStats('%o', counters) diff --git a/backend/src/schema/resolvers/registration.js b/backend/src/schema/resolvers/registration.js index 1a6bda1c8..e03f294cd 100644 --- a/backend/src/schema/resolvers/registration.js +++ b/backend/src/schema/resolvers/registration.js @@ -5,6 +5,7 @@ import encryptPassword from '../../helpers/encryptPassword' import generateNonce from './helpers/generateNonce' import existingEmailAddress from './helpers/existingEmailAddress' import normalizeEmail from './helpers/normalizeEmail' +import createOrUpdateLocations from './users/location' const neode = getNeode() @@ -22,7 +23,9 @@ export default { throw new UserInputError(e.message) } }, - SignupVerification: async (_parent, args) => { + SignupVerification: async (_parent, args, context) => { + const { driver } = context + const session = driver.session() const { termsAndConditionsAgreedVersion } = args const regEx = new RegExp(/^[0-9]+\.[0-9]+\.[0-9]+$/g) if (!regEx.test(termsAndConditionsAgreedVersion)) { @@ -51,11 +54,14 @@ export default { emailAddress.relateTo(user, 'belongsTo'), emailAddress.update({ verifiedAt: new Date().toISOString() }), ]) + await createOrUpdateLocations(args.id, args.locationName, session) return user.toJson() } catch (e) { if (e.code === 'Neo.ClientError.Schema.ConstraintValidationFailed') throw new UserInputError('User with this slug already exists!') throw new UserInputError(e.message) + } finally { + session.close() } }, }, diff --git a/backend/src/schema/resolvers/users.js b/backend/src/schema/resolvers/users.js index be9a69e80..0b3f13631 100644 --- a/backend/src/schema/resolvers/users.js +++ b/backend/src/schema/resolvers/users.js @@ -4,6 +4,7 @@ import { getNeode } from '../../bootstrap/neo4j' import { UserInputError, ForbiddenError } from 'apollo-server' import Resolver from './helpers/Resolver' import log from './helpers/databaseLogger' +import createOrUpdateLocations from './users/location' const neode = getNeode() @@ -127,6 +128,7 @@ export default { }) try { const [user] = await writeTxResultPromise + await createOrUpdateLocations(params.id, params.locationName, session) return user } catch (error) { throw new UserInputError(error.message) diff --git a/backend/src/middleware/nodes/locations.js b/backend/src/schema/resolvers/users/location.js similarity index 76% rename from backend/src/middleware/nodes/locations.js rename to backend/src/schema/resolvers/users/location.js index 47262d7ba..3f3638bf5 100644 --- a/backend/src/middleware/nodes/locations.js +++ b/backend/src/schema/resolvers/users/location.js @@ -2,8 +2,8 @@ import request from 'request' import { UserInputError } from 'apollo-server' import isEmpty from 'lodash/isEmpty' import Debug from 'debug' -import asyncForEach from '../../helpers/asyncForEach' -import CONFIG from './../../config' +import asyncForEach from '../../../helpers/asyncForEach' +import CONFIG from '../../../config' const debug = Debug('human-connection:location') @@ -57,16 +57,12 @@ const createLocation = async (session, mapboxData) => { } mutation += ' RETURN l.id' - try { - await session.writeTransaction(transaction => { - return transaction.run(mutation, data) - }) - } finally { - session.close() - } + await session.writeTransaction(transaction => { + return transaction.run(mutation, data) + }) } -const createOrUpdateLocations = async (userId, locationName, driver) => { +const createOrUpdateLocations = async (userId, locationName, session) => { if (isEmpty(locationName)) { return } @@ -99,7 +95,6 @@ const createOrUpdateLocations = async (userId, locationName, driver) => { throw new UserInputError('locationName is invalid') } - const session = driver.session() if (data.place_type.length > 1) { data.id = 'region.' + data.id.split('.')[1] } @@ -110,44 +105,36 @@ const createOrUpdateLocations = async (userId, locationName, driver) => { if (data.context) { await asyncForEach(data.context, async ctx => { await createLocation(session, ctx) - try { - await session.writeTransaction(transaction => { - return transaction.run( - ` + await session.writeTransaction(transaction => { + return transaction.run( + ` MATCH (parent:Location {id: $parentId}), (child:Location {id: $childId}) MERGE (child)<-[:IS_IN]-(parent) RETURN child.id, parent.id `, - { - parentId: parent.id, - childId: ctx.id, - }, - ) - }) - parent = ctx - } finally { - session.close() - } + { + parentId: parent.id, + childId: ctx.id, + }, + ) + }) + parent = ctx }) } // delete all current locations from user and add new location - try { - await session.writeTransaction(transaction => { - return transaction.run( - ` + await session.writeTransaction(transaction => { + return transaction.run( + ` MATCH (user:User {id: $userId})-[relationship:IS_IN]->(location:Location) DETACH DELETE relationship WITH user - MATCH (location:Location {id: $locationId}) - MERGE (user)-[:IS_IN]->(location) + MATCH (location:Location {id: $locationId}) + MERGE (user)-[:IS_IN]->(location) RETURN location.id, user.id `, - { userId: userId, locationId: data.id }, - ) - }) - } finally { - session.close() - } + { userId: userId, locationId: data.id }, + ) + }) } export default createOrUpdateLocations diff --git a/backend/src/middleware/user/userMiddleware.spec.js b/backend/src/schema/resolvers/users/location.spec.js similarity index 96% rename from backend/src/middleware/user/userMiddleware.spec.js rename to backend/src/schema/resolvers/users/location.spec.js index 4ca8fd89f..59d093afb 100644 --- a/backend/src/middleware/user/userMiddleware.spec.js +++ b/backend/src/schema/resolvers/users/location.spec.js @@ -1,8 +1,8 @@ -import { gql } from '../../helpers/jest' -import Factory from '../../seed/factories' -import { getNeode, getDriver } from '../../bootstrap/neo4j' +import { gql } from '../../../helpers/jest' +import Factory from '../../../seed/factories' +import { getNeode, getDriver } from '../../../bootstrap/neo4j' import { createTestClient } from 'apollo-server-testing' -import createServer from '../../server' +import createServer from '../../../server' const factory = Factory() const neode = getNeode() diff --git a/backend/yarn.lock b/backend/yarn.lock index ddcb61197..5e92e048c 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -1035,7 +1035,7 @@ url-regex "~4.1.1" video-extensions "~1.1.0" -"@metascraper/helpers@^5.8.10", "@metascraper/helpers@^5.8.12": +"@metascraper/helpers@^5.8.12": version "5.8.12" resolved "https://registry.yarnpkg.com/@metascraper/helpers/-/helpers-5.8.12.tgz#c4c1375a90ee9b674f8fb4d5a65cce6f5c6ce30d" integrity sha512-hmaIRXWcLGFWAXFKBHECHhf3VhHrbz/iV6spPtTeYyxCVO1TX62qYigqbizZwHk4dGeU1cTtbT2YN8/RCr1RiQ== @@ -6148,7 +6148,7 @@ neo-async@^2.6.0: resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.1.tgz#ac27ada66167fa8849a6addd837f6b189ad2081c" integrity sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw== -neo4j-driver@^1.7.6, neo4j-driver@~1.7.6: +neo4j-driver@^1.7.6: version "1.7.6" resolved "https://registry.yarnpkg.com/neo4j-driver/-/neo4j-driver-1.7.6.tgz#eccb135a71eba9048c68717444593a6424cffc49" integrity sha512-6c3ALO3vYDfUqNoCy8OFzq+fQ7q/ab3LCuJrmm8P04M7RmyRCCnUtJ8IzSTGbiZvyhcehGK+azNDAEJhxPV/hA== diff --git a/package.json b/package.json index 8fb463994..dcfbec425 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "expect": "^24.9.0", "faker": "Marak/faker.js#master", "graphql-request": "^1.8.2", - "neo4j-driver": "^1.7.6", + "neo4j-driver": "^4.0.1", "neode": "^0.3.6", "npm-run-all": "^4.1.5", "slug": "^1.1.0" diff --git a/yarn.lock b/yarn.lock index 0ddaae0a2..33fd7aef7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3989,6 +3989,16 @@ neo4j-driver@^1.7.6: text-encoding-utf-8 "^1.0.2" uri-js "^4.2.2" +neo4j-driver@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/neo4j-driver/-/neo4j-driver-4.0.1.tgz#b25ffde0f16602e94c46d097e16a8bacbd773d5a" + integrity sha512-SqBhXyyyayVs5gV/6BrgdKbcmU5AsYQXkFAiYO74XAE8XPLJ1HVR/Hu4wjonAX7+70DsalkWEiFN1c6UaCVzlQ== + dependencies: + "@babel/runtime" "^7.5.5" + rxjs "^6.5.2" + text-encoding-utf-8 "^1.0.2" + uri-js "^4.2.2" + neode@^0.3.6: version "0.3.6" resolved "https://registry.yarnpkg.com/neode/-/neode-0.3.6.tgz#7daf791eff6d170e52c338ea2e5cca6fdc6bfbe3" @@ -4777,6 +4787,13 @@ rxjs@^5.0.0-beta.11: dependencies: symbol-observable "1.0.1" +rxjs@^6.5.2: + version "6.5.3" + resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.5.3.tgz#510e26317f4db91a7eb1de77d9dd9ba0a4899a3a" + integrity sha512-wuYsAYYFdWTAnAaPoKGNhfpWwKZbJW+HgAJ+mImp+Epl7BG8oNWBCTyRM8gba9k4lk8BgWdoYm21Mo/RYhhbgA== + dependencies: + tslib "^1.9.0" + safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1: version "5.1.2" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" @@ -5368,6 +5385,11 @@ tough-cookie@~2.4.3: psl "^1.1.24" punycode "^1.4.1" +tslib@^1.9.0: + version "1.10.0" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a" + integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ== + tty-browserify@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/tty-browserify/-/tty-browserify-0.0.1.tgz#3f05251ee17904dfd0677546670db9651682b811"