mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Add custom resolver for createSocialMedia
This commit is contained in:
parent
d9bf8049c5
commit
a51190d4e9
@ -57,7 +57,7 @@ const permissions = shield({
|
|||||||
UpdateBadge: isAdmin,
|
UpdateBadge: isAdmin,
|
||||||
DeleteBadge: isAdmin,
|
DeleteBadge: isAdmin,
|
||||||
AddUserBadges: isAdmin,
|
AddUserBadges: isAdmin,
|
||||||
addSocialMedia: isAuthenticated,
|
CreateSocialMedia: isAuthenticated,
|
||||||
// AddBadgeRewarded: isAdmin,
|
// AddBadgeRewarded: isAdmin,
|
||||||
// RemoveBadgeRewarded: isAdmin,
|
// RemoveBadgeRewarded: isAdmin,
|
||||||
reward: isAdmin,
|
reward: isAdmin,
|
||||||
|
|||||||
22
backend/src/resolvers/socialMedia.js
Normal file
22
backend/src/resolvers/socialMedia.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { neo4jgraphql } from 'neo4j-graphql-js'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
Mutation: {
|
||||||
|
createSocialMedia: async (object, params, context, resolveInfo) => {
|
||||||
|
const result = await neo4jgraphql(object, params, context, resolveInfo, true)
|
||||||
|
|
||||||
|
const session = context.driver.session()
|
||||||
|
await session.run(
|
||||||
|
'MATCH (owner:User {id: $userId}), (socialMedia:SocialMedia {id: $socialMediaId}) ' +
|
||||||
|
'MERGE (socialMedia)<-[:OWNED]-(owner) ' +
|
||||||
|
'RETURN owner', {
|
||||||
|
userId: context.user.id,
|
||||||
|
socialMediaId: result.id
|
||||||
|
}
|
||||||
|
)
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -100,27 +100,6 @@ export default {
|
|||||||
|
|
||||||
return encode(currentUser)
|
return encode(currentUser)
|
||||||
}
|
}
|
||||||
},
|
|
||||||
addSocialMedia: async (_, { url }, { driver, user }) => {
|
|
||||||
const session = driver.session()
|
|
||||||
|
|
||||||
const { id } = user
|
|
||||||
const result = await session.run(
|
|
||||||
`MATCH (user:User {id: $userId})
|
|
||||||
SET user.socialMedia = user.socialMedia + $url
|
|
||||||
RETURN user {.socialMedia}
|
|
||||||
`,
|
|
||||||
{
|
|
||||||
userId: id,
|
|
||||||
url
|
|
||||||
}
|
|
||||||
)
|
|
||||||
session.close()
|
|
||||||
const [currentUser] = result.records.map(record => {
|
|
||||||
return record.get('user')
|
|
||||||
})
|
|
||||||
|
|
||||||
return currentUser.socialMedia
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -310,12 +310,14 @@ describe('change password', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('addSocialMedia', () => {
|
describe('CreateSocialMedia', () => {
|
||||||
let client
|
let client
|
||||||
let headers
|
let headers
|
||||||
const mutation = `
|
const mutation = `
|
||||||
mutation($url: String!) {
|
mutation($input: SocialMediaInput) {
|
||||||
addSocialMedia(url: $url)
|
CreateSocialMedia(input: $input) {
|
||||||
|
url
|
||||||
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
@ -326,12 +328,12 @@ describe('addSocialMedia', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('rejects empty string', async () => {
|
it('rejects empty string', async () => {
|
||||||
const variables = { url: '' }
|
const variables = { input: { url: '' } }
|
||||||
await expect(client.request(mutation, variables)).rejects.toThrow('Input is not a URL')
|
await expect(client.request(mutation, variables)).rejects.toThrow('Input is not a URL')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('validates URLs', async () => {
|
it('validates URLs', async () => {
|
||||||
const variables = { url: 'not-a-url' }
|
const variables = { input: { url: 'not-a-url' } }
|
||||||
await expect(client.request(mutation, variables)).rejects.toThrow('Input is not a URL')
|
await expect(client.request(mutation, variables)).rejects.toThrow('Input is not a URL')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -26,7 +26,7 @@ type Mutation {
|
|||||||
disable(id: ID!): ID
|
disable(id: ID!): ID
|
||||||
enable(id: ID!): ID
|
enable(id: ID!): ID
|
||||||
reward(fromBadgeId: ID!, toUserId: ID!): ID
|
reward(fromBadgeId: ID!, toUserId: ID!): ID
|
||||||
addSocialMedia(url: urlInput): [String]!
|
createSocialMedia(input: SocialMediaInput): SocialMedia
|
||||||
unreward(fromBadgeId: ID!, toUserId: ID!): ID
|
unreward(fromBadgeId: ID!, toUserId: ID!): ID
|
||||||
"Shout the given Type and ID"
|
"Shout the given Type and ID"
|
||||||
shout(id: ID!, type: ShoutTypeEnum): Boolean! @cypher(statement: """
|
shout(id: ID!, type: ShoutTypeEnum): Boolean! @cypher(statement: """
|
||||||
@ -121,7 +121,7 @@ type User {
|
|||||||
location: Location @cypher(statement: "MATCH (this)-[:IS_IN]->(l:Location) RETURN l")
|
location: Location @cypher(statement: "MATCH (this)-[:IS_IN]->(l:Location) RETURN l")
|
||||||
locationName: String
|
locationName: String
|
||||||
about: String
|
about: String
|
||||||
socialMedia: [String]
|
socialMedia: [SocialMedia]! @relation(name: "OWNED", direction: "OUT")
|
||||||
|
|
||||||
createdAt: String
|
createdAt: String
|
||||||
updatedAt: String
|
updatedAt: String
|
||||||
@ -313,9 +313,14 @@ type SharedInboxEndpoint {
|
|||||||
|
|
||||||
type SocialMedia {
|
type SocialMedia {
|
||||||
id: ID!
|
id: ID!
|
||||||
uri: String
|
url: String
|
||||||
|
ownedBy: [User]! @relation(name: "OWNED", direction: "IN")
|
||||||
}
|
}
|
||||||
|
|
||||||
input urlInput {
|
input SocialMediaInput {
|
||||||
url: String! @constraint(format: "uri")
|
url: String! @constraint(format: "uri")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
directive @constraint(
|
||||||
|
format: String
|
||||||
|
) on INPUT_FIELD_DEFINITION
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import faker from 'faker'
|
import faker from 'faker'
|
||||||
import uuid from 'uuid/v4'
|
import uuid from 'uuid/v4'
|
||||||
|
|
||||||
export default function create (params) {
|
export default function create(params) {
|
||||||
const {
|
const {
|
||||||
id = uuid(),
|
id = uuid(),
|
||||||
name = faker.name.findName(),
|
name = faker.name.findName(),
|
||||||
@ -27,8 +27,7 @@ export default function create (params) {
|
|||||||
about: "${about}",
|
about: "${about}",
|
||||||
role: ${role},
|
role: ${role},
|
||||||
disabled: ${disabled},
|
disabled: ${disabled},
|
||||||
deleted: ${deleted},
|
deleted: ${deleted}
|
||||||
socialMedia: []
|
|
||||||
) {
|
) {
|
||||||
id
|
id
|
||||||
name
|
name
|
||||||
@ -38,7 +37,6 @@ export default function create (params) {
|
|||||||
role
|
role
|
||||||
deleted
|
deleted
|
||||||
disabled
|
disabled
|
||||||
socialMedia
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user