Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
14cbbd6f
Commit
14cbbd6f
authored
Nov 27, 2020
by
Brandon Labuschagne
Committed by
Mark Florian
Nov 27, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add GraphQL ID conversion helpers
parent
2e5b2b42
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
1 deletion
+76
-1
app/assets/javascripts/graphql_shared/utils.js
app/assets/javascripts/graphql_shared/utils.js
+38
-0
spec/frontend/graphql_shared/utils_spec.js
spec/frontend/graphql_shared/utils_spec.js
+38
-1
No files found.
app/assets/javascripts/graphql_shared/utils.js
View file @
14cbbd6f
...
...
@@ -14,3 +14,41 @@ export const MutationOperationMode = {
Remove
:
'
REMOVE
'
,
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
));
spec/frontend/graphql_shared/utils_spec.js
View file @
14cbbd6f
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
'
,
()
=>
{
[
...
...
@@ -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
));
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment