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
4d517ac1
Commit
4d517ac1
authored
Dec 05, 2019
by
Alex Kalderimis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enhance Global-ID handling
This splits out GID parsing, and adds support for asserting expected types.
parent
7b2baef3
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
12 deletions
+32
-12
app/graphql/gitlab_schema.rb
app/graphql/gitlab_schema.rb
+18
-10
spec/graphql/gitlab_schema_spec.rb
spec/graphql/gitlab_schema_spec.rb
+14
-2
No files found.
app/graphql/gitlab_schema.rb
View file @
4d517ac1
...
...
@@ -58,19 +58,12 @@ class GitlabSchema < GraphQL::Schema
end
def
object_from_id
(
global_id
,
ctx
=
{})
expected_type
=
ctx
[
:expected_type
]
gid
=
GlobalID
.
parse
(
global_id
)
gid
=
parse_gid
(
global_id
,
ctx
)
unless
gid
raise
Gitlab
::
Graphql
::
Errors
::
ArgumentError
,
"
#{
global_id
}
is not a valid GitLab id."
end
if
expected_type
&&
!
gid
.
model_class
.
ancestors
.
include?
(
expected_type
)
vars
=
{
global_id:
global_id
,
expected_type:
expected_type
}
msg
=
_
(
'%{global_id} is not a valid id for %{expected_type}.'
)
%
vars
raise
Gitlab
::
Graphql
::
Errors
::
ArgumentError
,
msg
find_by_gid
(
gid
)
end
def
find_by_gid
(
gid
)
if
gid
.
model_class
<
ApplicationRecord
Gitlab
::
Graphql
::
Loaders
::
BatchModelLoader
.
new
(
gid
.
model_class
,
gid
.
model_id
).
find
elsif
gid
.
model_class
.
respond_to?
(
:lazy_find
)
...
...
@@ -80,6 +73,21 @@ class GitlabSchema < GraphQL::Schema
end
end
def
parse_gid
(
global_id
,
ctx
=
{})
expected_type
=
ctx
[
:expected_type
]
gid
=
GlobalID
.
parse
(
global_id
)
raise
Gitlab
::
Graphql
::
Errors
::
ArgumentError
,
"
#{
global_id
}
is not a valid GitLab id."
unless
gid
if
expected_type
&&
!
gid
.
model_class
.
ancestors
.
include?
(
expected_type
)
vars
=
{
global_id:
global_id
,
expected_type:
expected_type
}
msg
=
_
(
'%{global_id} is not a valid id for %{expected_type}.'
)
%
vars
raise
Gitlab
::
Graphql
::
Errors
::
ArgumentError
,
msg
end
gid
end
private
def
max_query_complexity
(
ctx
)
...
...
spec/graphql/gitlab_schema_spec.rb
View file @
4d517ac1
...
...
@@ -124,14 +124,26 @@ describe GitlabSchema do
describe
'.object_from_id'
do
context
'for subclasses of `ApplicationRecord`'
do
it
'returns the correct record'
do
user
=
create
(
:user
)
let_it_be
(
:user
)
{
create
(
:user
)
}
it
'returns the correct record'
do
result
=
described_class
.
object_from_id
(
user
.
to_global_id
.
to_s
)
expect
(
result
.
sync
).
to
eq
(
user
)
end
it
'returns the correct record, of the expected type'
do
result
=
described_class
.
object_from_id
(
user
.
to_global_id
.
to_s
,
expected_type:
::
User
)
expect
(
result
.
sync
).
to
eq
(
user
)
end
it
'fails if the type does not match'
do
expect
do
described_class
.
object_from_id
(
user
.
to_global_id
.
to_s
,
expected_type:
::
Project
)
end
.
to
raise_error
(
Gitlab
::
Graphql
::
Errors
::
ArgumentError
)
end
it
'batchloads the queries'
do
user1
=
create
(
:user
)
user2
=
create
(
:user
)
...
...
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