Commit 14cbbd6f authored by Brandon Labuschagne's avatar Brandon Labuschagne Committed by Mark Florian

Add GraphQL ID conversion helpers

parent 2e5b2b42
...@@ -14,3 +14,41 @@ export const MutationOperationMode = { ...@@ -14,3 +14,41 @@ export const MutationOperationMode = {
Remove: 'REMOVE', Remove: 'REMOVE',
Replace: 'REPLACE', Replace: 'REPLACE',
}; };
/**
* 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}
*/
export const convertToGraphQLIds = (type, ids) => ids.map(id => convertToGraphQLId(type, id));
import { getIdFromGraphQLId } from '~/graphql_shared/utils'; import {
getIdFromGraphQLId,
convertToGraphQLId,
convertToGraphQLIds,
} from '~/graphql_shared/utils';
const mockType = 'Group';
const mockId = 12;
const mockGid = `gid://gitlab/Group/12`;
describe('getIdFromGraphQLId', () => { describe('getIdFromGraphQLId', () => {
[ [
...@@ -44,3 +52,32 @@ describe('getIdFromGraphQLId', () => { ...@@ -44,3 +52,32 @@ describe('getIdFromGraphQLId', () => {
}); });
}); });
}); });
describe('convertToGraphQLId', () => {
it('combines $type and $id into $result', () => {
expect(convertToGraphQLId(mockType, mockId)).toBe(mockGid);
});
it.each`
type | id | message
${mockType} | ${null} | ${'id must be a number or string; got object'}
${null} | ${mockId} | ${'type must be a string; got object'}
`('throws TypeError with "$message" if a param is missing', ({ type, id, message }) => {
expect(() => convertToGraphQLId(type, id)).toThrow(new TypeError(message));
});
});
describe('convertToGraphQLIds', () => {
it('combines $type and $id into $result', () => {
expect(convertToGraphQLIds(mockType, [mockId])).toStrictEqual([mockGid]);
});
it.each`
type | ids | message
${mockType} | ${null} | ${"Cannot read property 'map' of null"}
${mockType} | ${[mockId, null]} | ${'id must be a number or string; got object'}
${null} | ${[mockId]} | ${'type must be a string; got object'}
`('throws TypeError with "$message" if a param is missing', ({ type, ids, message }) => {
expect(() => convertToGraphQLIds(type, ids)).toThrow(new TypeError(message));
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment