Skip to content
GitLab
Next
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Primary navigation
Search or go to…
Project
GitLab
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Privacy statement
Keyboard shortcuts
?
What's new
4
Snippets
Groups
Projects
Show more breadcrumbs
GitLab.org
GitLab
Commits
14cbbd6f
Commit
14cbbd6f
authored
4 years ago
by
Brandon Labuschagne
Committed by
Mark Florian
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add GraphQL ID conversion helpers
parent
2e5b2b42
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/assets/javascripts/graphql_shared/utils.js
+38
-0
38 additions, 0 deletions
app/assets/javascripts/graphql_shared/utils.js
spec/frontend/graphql_shared/utils_spec.js
+38
-1
38 additions, 1 deletion
spec/frontend/graphql_shared/utils_spec.js
with
76 additions
and
1 deletion
app/assets/javascripts/graphql_shared/utils.js
+
38
−
0
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
));
This diff is collapsed.
Click to expand it.
spec/frontend/graphql_shared/utils_spec.js
+
38
−
1
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
));
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment