Query groups by 'Group' resolver via 'slug' and test it

This commit is contained in:
Wolfgang Huß 2022-09-09 13:44:13 +02:00
parent b3a0d88a46
commit 36f6fee8be
5 changed files with 83 additions and 38 deletions

View File

@ -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

View File

@ -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'])

View File

@ -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 } })

View File

@ -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]

View File

@ -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