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
3a3f2898
Commit
3a3f2898
authored
Mar 10, 2021
by
Peter Hegman
Committed by
Vitaly Slobodin
Mar 10, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `convertFromGraphQLIds` GraphQL util
And refactor `projects_token_selector` component to use it
parent
5e29c446
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
10 deletions
+72
-10
app/assets/javascripts/access_tokens/components/projects_token_selector.vue
...ipts/access_tokens/components/projects_token_selector.vue
+4
-10
app/assets/javascripts/graphql_shared/utils.js
app/assets/javascripts/graphql_shared/utils.js
+34
-0
spec/frontend/graphql_shared/utils_spec.js
spec/frontend/graphql_shared/utils_spec.js
+34
-0
No files found.
app/assets/javascripts/access_tokens/components/projects_token_selector.vue
View file @
3a3f2898
...
@@ -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
(
project
s
),
list
:
convertNodeIdsFromGraphQLIds
(
projects
.
node
s
),
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
(
project
s
));
this
.
$emit
(
'
input
'
,
convertNodeIdsFromGraphQLIds
(
projects
.
node
s
));
},
},
},
},
},
},
...
@@ -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
;
...
...
app/assets/javascripts/graphql_shared/utils.js
View file @
3a3f2898
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
));
};
spec/frontend/graphql_shared/utils_spec.js
View file @
3a3f2898
...
@@ -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
'
),
);
});
});
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