diff options
Diffstat (limited to 'app/assets/javascripts/graphql_shared/utils.js')
-rw-r--r-- | app/assets/javascripts/graphql_shared/utils.js | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/app/assets/javascripts/graphql_shared/utils.js b/app/assets/javascripts/graphql_shared/utils.js index 198d9f980f0..6a64e8a2fa8 100644 --- a/app/assets/javascripts/graphql_shared/utils.js +++ b/app/assets/javascripts/graphql_shared/utils.js @@ -16,7 +16,10 @@ export const isGid = (id) => { return false; }; -const parseGid = (gid) => parseInt(`${gid}`.replace(/gid:\/\/gitlab\/.*\//g, ''), 10); +const parseGid = (gid) => { + const [type, id] = `${gid}`.replace(/gid:\/\/gitlab\//g, '').split('/'); + return { type, id }; +}; /** * Ids generated by GraphQL endpoints are usually in the format @@ -27,8 +30,24 @@ const parseGid = (gid) => parseInt(`${gid}`.replace(/gid:\/\/gitlab\/.*\//g, '') * @returns {Number} */ export const getIdFromGraphQLId = (gid = '') => { - const parsedGid = parseGid(gid); - return Number.isInteger(parsedGid) ? parsedGid : null; + const rawId = isGid(gid) ? parseGid(gid).id : gid; + const id = parseInt(rawId, 10); + return Number.isInteger(id) ? id : null; +}; + +/** + * Ids generated by GraphQL endpoints are usually in the format + * gid://gitlab/Environments/123. This method extracts Type string + * from the Id path + * + * @param {String} gid GraphQL global ID + * @returns {String} + */ +export const getTypeFromGraphQLId = (gid = '') => { + if (!isGid(gid)) return null; + + const { type } = parseGid(gid); + return type || null; }; export const MutationOperationMode = { |