From 36f6fee8be86737266ae0ab795b7907ed90d65d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Fri, 9 Sep 2022 13:44:13 +0200 Subject: [PATCH] Query groups by 'Group' resolver via 'slug' and test it --- backend/src/db/graphql/groups.js | 12 ---- backend/src/schema/resolvers/groups.js | 22 +++++-- backend/src/schema/resolvers/groups.spec.js | 66 +++++++++++++++++++++ backend/src/schema/types/type/Group.gql | 9 --- webapp/graphql/groups.js | 12 ---- 5 files changed, 83 insertions(+), 38 deletions(-) diff --git a/backend/src/db/graphql/groups.js b/backend/src/db/graphql/groups.js index ff63f1a25..3585a7b2d 100644 --- a/backend/src/db/graphql/groups.js +++ b/backend/src/db/graphql/groups.js @@ -123,13 +123,7 @@ export const groupQuery = gql` query ( $isMember: Boolean $id: ID - $name: String $slug: String - $createdAt: String - $updatedAt: String - $about: String - $description: String - $locationName: String $first: Int $offset: Int $orderBy: [_GroupOrdering] @@ -138,13 +132,7 @@ export const groupQuery = gql` Group( isMember: $isMember id: $id - name: $name slug: $slug - createdAt: $createdAt - updatedAt: $updatedAt - about: $about - description: $description - locationName: $locationName first: $first offset: $offset orderBy: $orderBy diff --git a/backend/src/schema/resolvers/groups.js b/backend/src/schema/resolvers/groups.js index 239a299dd..73ce56c0c 100644 --- a/backend/src/schema/resolvers/groups.js +++ b/backend/src/schema/resolvers/groups.js @@ -10,14 +10,26 @@ import { mergeImage } from './images/images' export default { Query: { Group: async (_object, params, context, _resolveInfo) => { - const { id: groupId, isMember } = params + const { isMember, id, slug } = params + const matchParams = { id, slug } + Object.keys(matchParams).forEach((key) => { + if ([undefined, null].includes(matchParams[key])) { + delete matchParams[key] + } + }) const session = context.driver.session() const readTxResultPromise = session.readTransaction(async (txc) => { - const groupIdCypher = groupId ? ` {id: "${groupId}"}` : '' + const matchParamsEntries = Object.entries(matchParams) + let groupMatchParamsCypher = '' + matchParamsEntries.forEach((ele, index) => { + groupMatchParamsCypher += index === 0 ? ' {' : '' + groupMatchParamsCypher += `${ele[0]}: "${ele[1]}"` + groupMatchParamsCypher += index < matchParamsEntries.length - 1 ? ', ' : '}' + }) let groupCypher if (isMember === true) { groupCypher = ` - MATCH (:User {id: $userId})-[membership:MEMBER_OF]->(group:Group${groupIdCypher}) + MATCH (:User {id: $userId})-[membership:MEMBER_OF]->(group:Group${groupMatchParamsCypher}) WITH group, membership WHERE (group.groupType IN ['public', 'closed']) OR (group.groupType = 'hidden' AND membership.role IN ['usual', 'admin', 'owner']) RETURN group {.*, myRole: membership.role} @@ -25,7 +37,7 @@ export default { } else { if (isMember === false) { groupCypher = ` - MATCH (group:Group${groupIdCypher}) + MATCH (group:Group${groupMatchParamsCypher}) WHERE (NOT (:User {id: $userId})-[:MEMBER_OF]->(group)) WITH group WHERE group.groupType IN ['public', 'closed'] @@ -33,7 +45,7 @@ export default { ` } else { groupCypher = ` - MATCH (group:Group${groupIdCypher}) + MATCH (group:Group${groupMatchParamsCypher}) OPTIONAL MATCH (:User {id: $userId})-[membership:MEMBER_OF]->(group) WITH group, membership WHERE (group.groupType IN ['public', 'closed']) OR (group.groupType = 'hidden' AND membership.role IN ['usual', 'admin', 'owner']) diff --git a/backend/src/schema/resolvers/groups.spec.js b/backend/src/schema/resolvers/groups.spec.js index e9b38cc2b..03b49e64a 100644 --- a/backend/src/schema/resolvers/groups.spec.js +++ b/backend/src/schema/resolvers/groups.spec.js @@ -503,6 +503,72 @@ describe('in mode', () => { }) }) + describe('with given slug', () => { + describe("slug = 'the-best-group'", () => { + it('finds only the listed group with this slug', async () => { + const result = await query({ + query: groupQuery, + variables: { slug: 'the-best-group' }, + }) + expect(result).toMatchObject({ + data: { + Group: [ + expect.objectContaining({ + id: 'my-group', + slug: 'the-best-group', + myRole: 'owner', + }), + ], + }, + errors: undefined, + }) + expect(result.data.Group.length).toBe(1) + }) + }) + + describe("slug = 'third-investigative-journalism-group'", () => { + it("finds only the hidden group where I'm 'usual' member", async () => { + const result = await query({ + query: groupQuery, + variables: { slug: 'third-investigative-journalism-group' }, + }) + expect(result).toMatchObject({ + data: { + Group: expect.arrayContaining([ + expect.objectContaining({ + id: 'third-hidden-group', + slug: 'third-investigative-journalism-group', + myRole: 'usual', + }), + ]), + }, + errors: undefined, + }) + expect(result.data.Group.length).toBe(1) + }) + }) + + describe("slug = 'second-investigative-journalism-group'", () => { + it("finds no hidden group where I'm 'pending' member", async () => { + const result = await query({ + query: groupQuery, + variables: { slug: 'second-investigative-journalism-group' }, + }) + expect(result.data.Group.length).toBe(0) + }) + }) + + describe("slug = 'investigative-journalism-group'", () => { + it("finds no hidden group where I'm not(!) a member at all", async () => { + const result = await query({ + query: groupQuery, + variables: { slug: 'investigative-journalism-group' }, + }) + expect(result.data.Group.length).toBe(0) + }) + }) + }) + describe('isMember = true', () => { it('finds only listed groups where user is member', async () => { const result = await query({ query: groupQuery, variables: { isMember: true } }) diff --git a/backend/src/schema/types/type/Group.gql b/backend/src/schema/types/type/Group.gql index c1b097857..5cd8821b0 100644 --- a/backend/src/schema/types/type/Group.gql +++ b/backend/src/schema/types/type/Group.gql @@ -61,16 +61,7 @@ type Query { Group( isMember: Boolean # if 'undefined' or 'null' then get all groups id: ID - name: String slug: String - createdAt: String - updatedAt: String - about: String - description: String - # groupType: GroupType # test this - # actionRadius: GroupActionRadius # test this - # avatar: ImageInput # test this - locationName: String first: Int offset: Int orderBy: [_GroupOrdering] diff --git a/webapp/graphql/groups.js b/webapp/graphql/groups.js index ff63f1a25..3585a7b2d 100644 --- a/webapp/graphql/groups.js +++ b/webapp/graphql/groups.js @@ -123,13 +123,7 @@ export const groupQuery = gql` query ( $isMember: Boolean $id: ID - $name: String $slug: String - $createdAt: String - $updatedAt: String - $about: String - $description: String - $locationName: String $first: Int $offset: Int $orderBy: [_GroupOrdering] @@ -138,13 +132,7 @@ export const groupQuery = gql` Group( isMember: $isMember id: $id - name: $name slug: $slug - createdAt: $createdAt - updatedAt: $updatedAt - about: $about - description: $description - locationName: $locationName first: $first offset: $offset orderBy: $orderBy