Commit 3a3f2898 authored by Peter Hegman's avatar Peter Hegman Committed by Vitaly Slobodin

Add `convertFromGraphQLIds` GraphQL util

And refactor `projects_token_selector` component to use it
parent 5e29c446
...@@ -8,7 +8,7 @@ import { ...@@ -8,7 +8,7 @@ import {
} from '@gitlab/ui'; } from '@gitlab/ui';
import produce from 'immer'; import produce from 'immer';
import { getIdFromGraphQLId, convertToGraphQLId } from '~/graphql_shared/utils'; import { convertToGraphQLIds, convertNodeIdsFromGraphQLIds } from '~/graphql_shared/utils';
import getProjectsQuery from '../graphql/queries/get_projects.query.graphql'; import getProjectsQuery from '../graphql/queries/get_projects.query.graphql';
...@@ -51,7 +51,7 @@ export default { ...@@ -51,7 +51,7 @@ export default {
}, },
update({ projects }) { update({ projects }) {
return { return {
list: this.formatProjectNodes(projects), list: convertNodeIdsFromGraphQLIds(projects.nodes),
pageInfo: projects.pageInfo, pageInfo: projects.pageInfo,
}; };
}, },
...@@ -64,7 +64,7 @@ export default { ...@@ -64,7 +64,7 @@ export default {
query: getProjectsQuery, query: getProjectsQuery,
variables() { variables() {
return { return {
ids: this.initialProjectIds.map((id) => convertToGraphQLId(GRAPHQL_ENTITY_TYPE, id)), ids: convertToGraphQLIds(GRAPHQL_ENTITY_TYPE, this.initialProjectIds),
}; };
}, },
manual: true, manual: true,
...@@ -72,7 +72,7 @@ export default { ...@@ -72,7 +72,7 @@ export default {
return !this.initialProjectIds.length; return !this.initialProjectIds.length;
}, },
result({ data: { projects } }) { result({ data: { projects } }) {
this.$emit('input', this.formatProjectNodes(projects)); this.$emit('input', convertNodeIdsFromGraphQLIds(projects.nodes));
}, },
}, },
}, },
...@@ -88,12 +88,6 @@ export default { ...@@ -88,12 +88,6 @@ export default {
}; };
}, },
methods: { methods: {
formatProjectNodes(projects) {
return projects.nodes.map((project) => ({
...project,
id: getIdFromGraphQLId(project.id),
}));
},
handleSearch(query) { handleSearch(query) {
this.isSearching = true; this.isSearching = true;
this.searchQuery = query; this.searchQuery = query;
......
import { isArray } from 'lodash';
/** /**
* Ids generated by GraphQL endpoints are usually in the format * Ids generated by GraphQL endpoints are usually in the format
* gid://gitlab/Environments/123. This method extracts Id number * gid://gitlab/Environments/123. This method extracts Id number
...@@ -52,3 +54,35 @@ export const convertToGraphQLId = (type, id) => { ...@@ -52,3 +54,35 @@ export const convertToGraphQLId = (type, id) => {
* @returns {Array} * @returns {Array}
*/ */
export const convertToGraphQLIds = (type, ids) => ids.map((id) => convertToGraphQLId(type, id)); export const convertToGraphQLIds = (type, ids) => ids.map((id) => convertToGraphQLId(type, id));
/**
* 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));
};
...@@ -2,6 +2,8 @@ import { ...@@ -2,6 +2,8 @@ import {
getIdFromGraphQLId, getIdFromGraphQLId,
convertToGraphQLId, convertToGraphQLId,
convertToGraphQLIds, convertToGraphQLIds,
convertFromGraphQLIds,
convertNodeIdsFromGraphQLIds,
} from '~/graphql_shared/utils'; } from '~/graphql_shared/utils';
const mockType = 'Group'; const mockType = 'Group';
...@@ -81,3 +83,35 @@ describe('convertToGraphQLIds', () => { ...@@ -81,3 +83,35 @@ describe('convertToGraphQLIds', () => {
expect(() => convertToGraphQLIds(type, ids)).toThrow(new TypeError(message)); expect(() => convertToGraphQLIds(type, ids)).toThrow(new TypeError(message));
}); });
}); });
describe('convertFromGraphQLIds', () => {
it.each`
ids | expected
${[mockGid]} | ${[mockId]}
${[mockGid, 'invalid id']} | ${[mockId, null]}
`('converts $ids from GraphQL Ids', ({ ids, expected }) => {
expect(convertFromGraphQLIds(ids)).toEqual(expected);
});
it("throws TypeError if `ids` parameter isn't an array", () => {
expect(() => convertFromGraphQLIds('invalid')).toThrow(
new TypeError('ids must be an array; got string'),
);
});
});
describe('convertNodeIdsFromGraphQLIds', () => {
it.each`
nodes | expected
${[{ id: mockGid, name: 'foo bar' }, { id: mockGid, name: 'baz' }]} | ${[{ id: mockId, name: 'foo bar' }, { id: mockId, name: 'baz' }]}
${[{ name: 'foo bar' }]} | ${[{ name: 'foo bar' }]}
`('converts `id` properties in $nodes from GraphQL Id', ({ nodes, expected }) => {
expect(convertNodeIdsFromGraphQLIds(nodes)).toEqual(expected);
});
it("throws TypeError if `nodes` parameter isn't an array", () => {
expect(() => convertNodeIdsFromGraphQLIds('invalid')).toThrow(
new TypeError('nodes must be an array; got string'),
);
});
});
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