Separate middleware for ids

This commit is contained in:
Robert Schäfer 2019-02-19 23:25:28 +01:00
parent 30bb54c6fa
commit 9c4e599ff1
5 changed files with 41 additions and 31 deletions

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

View File

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

View File

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

View File

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

View File

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