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
95b3fe28
Commit
95b3fe28
authored
Jun 04, 2019
by
Brett Walker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use :complexity_multiplier only with connections
This helps reduce complexity for non-connections
parent
75e11922
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
41 additions
and
27 deletions
+41
-27
app/graphql/types/base_field.rb
app/graphql/types/base_field.rb
+7
-6
spec/graphql/resolvers/base_resolver_spec.rb
spec/graphql/resolvers/base_resolver_spec.rb
+12
-10
spec/graphql/resolvers/issues_resolver_spec.rb
spec/graphql/resolvers/issues_resolver_spec.rb
+1
-1
spec/graphql/resolvers/namespace_projects_resolver_spec.rb
spec/graphql/resolvers/namespace_projects_resolver_spec.rb
+1
-1
spec/graphql/types/base_field_spec.rb
spec/graphql/types/base_field_spec.rb
+20
-9
No files found.
app/graphql/types/base_field.rb
View file @
95b3fe28
...
...
@@ -29,15 +29,16 @@ module Types
# proc because we set complexity depending on arguments and number of
# items which can be loaded.
proc
do
|
ctx
,
args
,
child_complexity
|
page_size
=
@max_page_size
||
ctx
.
schema
.
default_max_page_size
limit_value
=
[
args
[
:first
],
args
[
:last
],
page_size
].
compact
.
min
# Resolvers may add extra complexity depending on used arguments
complexity
=
child_complexity
+
self
.
resolver
&
.
try
(
:resolver_complexity
,
args
,
child_complexity:
child_complexity
).
to_i
# Resolvers may add extra complexity depending on number of items being loaded.
multiplier
=
self
.
resolver
&
.
try
(
:complexity_multiplier
,
args
).
to_f
complexity
+=
complexity
*
limit_value
*
multiplier
if
@connection
# Resolvers may add extra complexity depending on number of items being loaded.
page_size
=
@max_page_size
||
ctx
.
schema
.
default_max_page_size
limit_value
=
[
args
[
:first
],
args
[
:last
],
page_size
].
compact
.
min
multiplier
=
self
.
resolver
&
.
try
(
:complexity_multiplier
,
args
).
to_f
complexity
+=
complexity
*
limit_value
*
multiplier
end
complexity
.
to_i
end
...
...
spec/graphql/resolvers/base_resolver_spec.rb
View file @
95b3fe28
...
...
@@ -29,18 +29,20 @@ describe Resolvers::BaseResolver do
end
end
it
'increases complexity based on arguments'
do
field
=
Types
::
BaseField
.
new
(
name:
'test'
,
type:
GraphQL
::
STRING_TYPE
,
resolver_class:
described_class
,
null:
false
,
max_page_size:
1
)
context
'when field is a connection'
do
it
'increases complexity based on arguments'
do
field
=
Types
::
BaseField
.
new
(
name:
'test'
,
type:
GraphQL
::
STRING_TYPE
.
connection_type
,
resolver_class:
described_class
,
null:
false
,
max_page_size:
1
)
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
sort:
'foo'
},
1
)).
to
eq
3
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
search:
'foo'
},
1
)).
to
eq
7
end
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
sort:
'foo'
},
1
)).
to
eq
3
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
search:
'foo'
},
1
)).
to
eq
7
end
it
'does not increase complexity when filtering by iids'
do
field
=
Types
::
BaseField
.
new
(
name:
'test'
,
type:
GraphQL
::
STRING_TYPE
,
resolver_class:
described_class
,
null:
false
,
max_page_size:
100
)
it
'does not increase complexity when filtering by iids'
do
field
=
Types
::
BaseField
.
new
(
name:
'test'
,
type:
GraphQL
::
STRING_TYPE
.
connection_type
,
resolver_class:
described_class
,
null:
false
,
max_page_size:
100
)
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
sort:
'foo'
},
1
)).
to
eq
6
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
sort:
'foo'
,
iid:
1
},
1
)).
to
eq
3
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
sort:
'foo'
,
iids:
[
1
,
2
,
3
]
},
1
)).
to
eq
3
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
sort:
'foo'
},
1
)).
to
eq
6
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
sort:
'foo'
,
iid:
1
},
1
)).
to
eq
3
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
sort:
'foo'
,
iids:
[
1
,
2
,
3
]
},
1
)).
to
eq
3
end
end
end
spec/graphql/resolvers/issues_resolver_spec.rb
View file @
95b3fe28
...
...
@@ -121,7 +121,7 @@ describe Resolvers::IssuesResolver do
end
it
'increases field complexity based on arguments'
do
field
=
Types
::
BaseField
.
new
(
name:
'test'
,
type:
GraphQL
::
STRING_TYPE
,
resolver_class:
described_class
,
null:
false
,
max_page_size:
100
)
field
=
Types
::
BaseField
.
new
(
name:
'test'
,
type:
GraphQL
::
STRING_TYPE
.
connection_type
,
resolver_class:
described_class
,
null:
false
,
max_page_size:
100
)
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{},
1
)).
to
eq
4
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
labelName:
'foo'
},
1
)).
to
eq
8
...
...
spec/graphql/resolvers/namespace_projects_resolver_spec.rb
View file @
95b3fe28
...
...
@@ -57,7 +57,7 @@ describe Resolvers::NamespaceProjectsResolver, :nested_groups do
end
it
'has an high complexity regardless of arguments'
do
field
=
Types
::
BaseField
.
new
(
name:
'test'
,
type:
GraphQL
::
STRING_TYPE
,
resolver_class:
described_class
,
null:
false
,
max_page_size:
100
)
field
=
Types
::
BaseField
.
new
(
name:
'test'
,
type:
GraphQL
::
STRING_TYPE
.
connection_type
,
resolver_class:
described_class
,
null:
false
,
max_page_size:
100
)
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{},
1
)).
to
eq
24
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
include_subgroups:
true
},
1
)).
to
eq
24
...
...
spec/graphql/types/base_field_spec.rb
View file @
95b3fe28
...
...
@@ -28,18 +28,29 @@ describe Types::BaseField do
expect
(
field
.
to_graphql
.
complexity
).
to
eq
12
end
it
'sets complexity depending on arguments for resolvers'
do
field
=
described_class
.
new
(
name:
'test'
,
type:
GraphQL
::
STRING_TYPE
,
resolver_class:
resolver
,
max_page_size:
100
,
null:
true
)
context
'when field has a resolver proc'
do
context
'and is a connection'
do
let
(
:field
)
{
described_class
.
new
(
name:
'test'
,
type:
GraphQL
::
STRING_TYPE
.
connection_type
,
resolver_class:
resolver
,
max_page_size:
100
,
null:
true
)
}
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{},
2
)).
to
eq
4
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
first:
50
},
2
)).
to
eq
3
end
it
'sets complexity depending on arguments for resolvers'
do
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{},
2
)).
to
eq
4
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
first:
50
},
2
)).
to
eq
3
end
it
'sets complexity depending on number load limits for resolvers'
do
field
=
described_class
.
new
(
name:
'test'
,
type:
GraphQL
::
STRING_TYPE
,
resolver_class:
resolver
,
max_page_size:
100
,
null:
true
)
it
'sets complexity depending on number load limits for resolvers'
do
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
first:
1
},
2
)).
to
eq
2
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
first:
1
,
foo:
true
},
2
)).
to
eq
4
end
end
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
first:
1
},
2
)).
to
eq
2
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
first:
1
,
foo:
true
},
2
)).
to
eq
4
context
'and is not a connection'
do
it
'sets complexity as normal'
do
field
=
described_class
.
new
(
name:
'test'
,
type:
GraphQL
::
STRING_TYPE
,
resolver_class:
resolver
,
max_page_size:
100
,
null:
true
)
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{},
2
)).
to
eq
2
expect
(
field
.
to_graphql
.
complexity
.
call
({},
{
first:
50
},
2
)).
to
eq
2
end
end
end
end
end
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