debian-mirror-gitlab/app/assets/javascripts/graphql_shared/utils.js

55 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-03-13 15:44:24 +05:30
/**
* Ids generated by GraphQL endpoints are usually in the format
* gid://gitlab/Environments/123. This method extracts Id number
* from the Id path
*
* @param {String} gid GraphQL global ID
* @returns {Number}
*/
export const getIdFromGraphQLId = (gid = '') =>
2021-01-29 00:20:46 +05:30
parseInt(`${gid}`.replace(/gid:\/\/gitlab\/.*\//g, ''), 10) || null;
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
export const MutationOperationMode = {
Append: 'APPEND',
Remove: 'REMOVE',
Replace: 'REPLACE',
};
2021-02-22 17:27:13 +05:30
/**
* Possible GraphQL entity types.
*/
export const TYPE_GROUP = 'Group';
/**
* Ids generated by GraphQL endpoints are usually in the format
* gid://gitlab/Groups/123. This method takes a type and an id
* and interpolates the 2 values into the expected GraphQL ID format.
*
* @param {String} type The entity type
* @param {String|Number} id The id value
* @returns {String}
*/
export const convertToGraphQLId = (type, id) => {
if (typeof type !== 'string') {
throw new TypeError(`type must be a string; got ${typeof type}`);
}
if (!['number', 'string'].includes(typeof id)) {
throw new TypeError(`id must be a number or string; got ${typeof id}`);
}
return `gid://gitlab/${type}/${id}`;
};
/**
* Ids generated by GraphQL endpoints are usually in the format
* gid://gitlab/Groups/123. This method takes a type and an
* array of ids and tranforms the array values into the expected
* GraphQL ID format.
*
* @param {String} type The entity type
* @param {Array} ids An array of id values
* @returns {Array}
*/
2021-03-08 18:12:59 +05:30
export const convertToGraphQLIds = (type, ids) => ids.map((id) => convertToGraphQLId(type, id));