2021-03-10 10:09:11 -05:00
|
|
|
import { isArray } from 'lodash';
|
|
|
|
|
2021-08-02 08:09:59 -04:00
|
|
|
/**
|
|
|
|
* Ids generated by GraphQL endpoints are usually in the format
|
|
|
|
* gid://gitlab/Environments/123. This method checks if the passed id follows that format
|
|
|
|
*
|
|
|
|
* @param {String|Number} id The id value
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
export const isGid = (id) => {
|
|
|
|
if (typeof id === 'string' && id.startsWith('gid://gitlab/')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2021-11-08 22:42:22 -05:00
|
|
|
const parseGid = (gid) => parseInt(`${gid}`.replace(/gid:\/\/gitlab\/.*\//g, ''), 10);
|
|
|
|
|
2020-01-24 10:09:00 -05:00
|
|
|
/**
|
|
|
|
* 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}
|
|
|
|
*/
|
2021-11-09 22:10:14 -05:00
|
|
|
export const getIdFromGraphQLId = (gid = '') => {
|
2021-11-08 22:42:22 -05:00
|
|
|
const parsedGid = parseGid(gid);
|
|
|
|
return Number.isInteger(parsedGid) ? parsedGid : null;
|
|
|
|
};
|
2020-01-24 10:09:00 -05:00
|
|
|
|
2020-11-05 22:09:19 -05:00
|
|
|
export const MutationOperationMode = {
|
|
|
|
Append: 'APPEND',
|
|
|
|
Remove: 'REMOVE',
|
|
|
|
Replace: 'REPLACE',
|
|
|
|
};
|
2020-11-27 10:09:34 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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}`);
|
|
|
|
}
|
|
|
|
|
2021-08-02 08:09:59 -04:00
|
|
|
if (isGid(id)) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2020-11-27 10:09:34 -05:00
|
|
|
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}
|
|
|
|
*/
|
2020-12-23 19:10:25 -05:00
|
|
|
export const convertToGraphQLIds = (type, ids) => ids.map((id) => convertToGraphQLId(type, id));
|
2021-03-10 10:09:11 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ids generated by GraphQL endpoints are usually in the format
|
|
|
|
* gid://gitlab/Groups/123. This method takes an array of
|
|
|
|
* GraphQL Ids and converts them to a number.
|
|
|
|
*
|
|
|
|
* @param {Array} ids An array of GraphQL IDs
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
export const convertFromGraphQLIds = (ids) => {
|
|
|
|
if (!isArray(ids)) {
|
|
|
|
throw new TypeError(`ids must be an array; got ${typeof ids}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids.map((id) => getIdFromGraphQLId(id));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ids generated by GraphQL endpoints are usually in the format
|
|
|
|
* gid://gitlab/Groups/123. This method takes an array of nodes
|
|
|
|
* and converts the `id` properties from a GraphQL Id to a number.
|
|
|
|
*
|
|
|
|
* @param {Array} nodes An array of nodes with an `id` property
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
export const convertNodeIdsFromGraphQLIds = (nodes) => {
|
|
|
|
if (!isArray(nodes)) {
|
|
|
|
throw new TypeError(`nodes must be an array; got ${typeof nodes}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes.map((node) => (node.id ? { ...node, id: getIdFromGraphQLId(node.id) } : node));
|
|
|
|
};
|