Skip to content

Commit 00b0e0d

Browse files
fix: API edit mutation could fail because of incorrect check of arguments
* fixed graphql endpoint not accept empty after edited * removed console logs * improve API configuration edit mutation --------- Co-authored-by: Antoine Hurard <antoine.reliefapps@gmail.com>
1 parent 296aab4 commit 00b0e0d

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

src/i18n/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
},
6868
"edit": {
6969
"errors": {
70-
"invalidArguments": "Either name, status, authType, endpoint, pingUrl, settings or permissions must be provided."
70+
"invalidArguments": "Either name, status, authType, endpoint, graphqlEndpoint, pingUrl, settings or permissions must be provided."
7171
}
7272
}
7373
},

src/schema/mutation/editApiConfiguration.mutation.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { buildTypes } from '@utils/schema';
1414
import { validateApi } from '@utils/validators/validateApi';
1515
import config from 'config';
1616
import { logger } from '@services/logger.service';
17+
import { cloneDeep, isEmpty, omit } from 'lodash';
1718

1819
/**
1920
* Edit the passed apiConfiguration if authorized.
@@ -41,42 +42,33 @@ export default {
4142
);
4243
}
4344
const ability: AppAbility = user.ability;
44-
if (
45-
!args.name &&
46-
!args.status &&
47-
!args.authType &&
48-
!args.endpoint &&
49-
!args.pingUrl &&
50-
!args.graphQLEndpoint &&
51-
!args.settings &&
52-
!args.permissions
53-
) {
45+
46+
// Check if any of required arguments for a valid update are provided.
47+
// Else, send error
48+
// cloneDeep coupled with omit allows to filter out the 'id' field
49+
if (isEmpty(cloneDeep(omit(args, ['id'])))) {
5450
throw new GraphQLError(
5551
context.i18next.t(
5652
'mutations.apiConfiguration.edit.errors.invalidArguments'
5753
)
5854
);
5955
}
60-
const update = {};
56+
// See if API name is usable
6157
if (args.name) {
6258
validateApi(args.name);
6359
}
64-
Object.assign(
65-
update,
66-
args.name && { name: args.name },
67-
args.status && { status: args.status },
68-
args.authType && { authType: args.authType },
69-
args.endpoint && { endpoint: args.endpoint },
70-
args.graphQLEndpoint && { graphQLEndpoint: args.graphQLEndpoint },
71-
args.pingUrl && { pingUrl: args.pingUrl },
72-
args.settings && {
60+
// Create the update document
61+
const update = {
62+
...cloneDeep(omit(args, ['id'])),
63+
...(args.settings && {
7364
settings: CryptoJS.AES.encrypt(
7465
JSON.stringify(args.settings),
7566
config.get('encryption.key')
7667
).toString(),
77-
},
78-
args.permissions && { permissions: args.permissions }
79-
);
68+
}),
69+
};
70+
71+
// Find API configuration and update it using User permissions
8072
const filters = ApiConfiguration.accessibleBy(ability, 'update')
8173
.where({ _id: args.id })
8274
.getFilter();

0 commit comments

Comments
 (0)