mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2026-01-20 20:01:25 +00:00
Separate middleware for ids
This commit is contained in:
parent
30bb54c6fa
commit
9c4e599ff1
32
src/middleware/idMiddleware.js
Normal file
32
src/middleware/idMiddleware.js
Normal file
@ -0,0 +1,32 @@
|
||||
import find from 'lodash/find'
|
||||
|
||||
const includeId = async (resolve, root, args, context, info) => {
|
||||
let isIdPresent
|
||||
let removeIdFromResult
|
||||
isIdPresent = find(info.fieldNodes[0].selectionSet.selections, item => item.name.value === 'id')
|
||||
if (!isIdPresent) {
|
||||
// add id to request as the user did not ask but we need it
|
||||
info.fieldNodes[0].selectionSet.selections.unshift({
|
||||
kind: 'Field',
|
||||
name: { kind: 'Name', value: 'id' }
|
||||
})
|
||||
removeIdFromResult = true
|
||||
}
|
||||
|
||||
const result = await resolve(root, args, context, info)
|
||||
|
||||
if (!isIdPresent && removeIdFromResult) {
|
||||
// remove id if the user did not ask for it
|
||||
info.fieldNodes[0].selectionSet.selections.shift()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export default {
|
||||
Query: (resolve, root, args, context, info) => {
|
||||
return includeId(resolve, root, args, context, info)
|
||||
},
|
||||
Mutation: (resolve, root, args, context, info) => {
|
||||
return includeId(resolve, root, args, context, info)
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,7 @@ import dateTimeMiddleware from './dateTimeMiddleware'
|
||||
import xssMiddleware from './xssMiddleware'
|
||||
import permissionsMiddleware from './permissionsMiddleware'
|
||||
import userMiddleware from './userMiddleware'
|
||||
import idMiddleware from './idMiddleware'
|
||||
|
||||
export default schema => {
|
||||
let middleware = [
|
||||
@ -17,7 +18,8 @@ export default schema => {
|
||||
xssMiddleware,
|
||||
fixImageUrlsMiddleware,
|
||||
softDeleteMiddleware,
|
||||
userMiddleware
|
||||
userMiddleware,
|
||||
idMiddleware
|
||||
]
|
||||
|
||||
// add permisions middleware at the first position (unless we're seeding)
|
||||
|
||||
@ -38,9 +38,9 @@ describe('authorization', () => {
|
||||
})
|
||||
|
||||
it('does not expose the owner\'s email address', async () => {
|
||||
try{
|
||||
try {
|
||||
await action(headers)
|
||||
} catch(error) {
|
||||
} catch (error) {
|
||||
expect(error.response.data).toEqual({ User: [ { email: null } ] })
|
||||
}
|
||||
})
|
||||
@ -69,9 +69,9 @@ describe('authorization', () => {
|
||||
})
|
||||
|
||||
it('does not expose the owner\'s email address', async () => {
|
||||
try{
|
||||
try {
|
||||
await action(headers)
|
||||
} catch(error) {
|
||||
} catch (error) {
|
||||
expect(error.response.data).toEqual({ User: [ { email: null } ] })
|
||||
}
|
||||
})
|
||||
|
||||
@ -5,8 +5,8 @@ import { GraphQLClient } from 'graphql-request'
|
||||
let client
|
||||
let headers
|
||||
beforeEach(async () => {
|
||||
await create('user', {email: 'user@example.org', password: '1234'})
|
||||
headers = await authenticatedHeaders({email: 'user@example.org', password: '1234'})
|
||||
await create('user', { email: 'user@example.org', password: '1234' })
|
||||
headers = await authenticatedHeaders({ email: 'user@example.org', password: '1234' })
|
||||
client = new GraphQLClient(host, { headers })
|
||||
})
|
||||
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import createOrUpdateLocations from './nodes/locations'
|
||||
import find from 'lodash/find'
|
||||
|
||||
export default {
|
||||
Mutation: {
|
||||
@ -26,29 +25,6 @@ export default {
|
||||
})
|
||||
session.close()
|
||||
|
||||
return result
|
||||
}
|
||||
},
|
||||
Query: {
|
||||
User: async (resolve, root, args, context, info) => {
|
||||
let isIdPresent
|
||||
let removeIdFromResult
|
||||
try {
|
||||
isIdPresent = find(info.fieldNodes[0].selectionSet.selections, item => item.name.value === 'id')
|
||||
if (!isIdPresent) {
|
||||
// add id to request as the user did not ask but we need it
|
||||
info.fieldNodes[0].selectionSet.selections.unshift({
|
||||
kind: 'Field',
|
||||
name: { kind: 'Name', value: 'id' }
|
||||
})
|
||||
removeIdFromResult = true
|
||||
}
|
||||
} catch (err) {}
|
||||
const result = await resolve(root, args, context, info)
|
||||
if (!isIdPresent && removeIdFromResult) {
|
||||
// remove id if the user did not ask for it
|
||||
info.fieldNodes[0].selectionSet.selections.shift()
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user