Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend query depth limiter allowing for more detailed rules #2505

Merged
merged 31 commits into from
May 22, 2023

Conversation

tsmith023
Copy link
Contributor

@tsmith023 tsmith023 commented Feb 1, 2023

Enhances the query depth limiter by providing options to specify additional rules for fields within the GraphQL query.
These can be the field name on its own or including the field arguments.

Description

Exposes the new should_ignore argument to QueryDepthLimiter that takes a single argument of type IgnoreContext allowing the user greater verbosity when defining their rules. This dataclass can be extended to provide more ignore options by future PRs in response to further feature requests driven by specific use cases.

This functionality does not replace previous functionality but simply extends it. So you can still use the QueryDepthLimiter extension as it was previously implemented but now you can also use the further verbose functionality. However, the previous implementation is deprecated and will be removed in future releases.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

@tsmith023 tsmith023 marked this pull request as draft February 1, 2023 18:00
@tsmith023 tsmith023 changed the title Draft: Extend query depth limiter allowing for more detailed rules Extend query depth limiter allowing for more detailed rules Feb 1, 2023
@botberry
Copy link
Member

botberry commented Feb 1, 2023

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release introduces the new should_ignore argument to the QueryDepthLimiter extension that provides
a more general and more verbose way of specifying the rules by which a query's depth should be limited.

The should_ignore argument should be a function that accepts a single argument of type IgnoreContext.
The IgnoreContext class has the following attributes:

  • field_name of type str: the name of the field to be compared against
  • field_args of type strawberry.extensions.query_depth_limiter.FieldArgumentsType: the arguments of the field to be compared against
  • query of type graphql.language.Node: the query string
  • context of type graphql.validation.ValidationContext: the context passed to the query
    and returns True if the field should be ignored and False otherwise.
    This argument is injected, regardless of name, by the QueryDepthLimiter class and should not be passed by the user.

Instead, the user should write business logic to determine whether a field should be ignored or not by
the attributes of the IgnoreContext class.

For example, the following query:

"""
    query {
      matt: user(name: "matt") {
        email
      }
      andy: user(name: "andy") {
        email
        address {
          city
        }
        pets {
          name
          owner {
            name
          }
        }
      }
    }
"""

can have its depth limited by the following should_ignore:

from strawberry.extensions import IgnoreContext


def should_ignore(ignore: IgnoreContext):
    return ignore.field_args.get("name") == "matt"


query_depth_limiter = QueryDepthLimiter(should_ignore=should_ignore)

so that it effectively becomes:

"""
    query {
      andy: user(name: "andy") {
        email
        pets {
          name
          owner {
            name
          }
        }
      }
    }
"""

Here's the tweet text:

🆕 Release (next) is out! Thanks to @t1mmysm0th for the PR 👏

Get it here 👉 https://github.com/strawberry-graphql/strawberry/releases/tag/(next)

@codecov
Copy link

codecov bot commented Feb 1, 2023

Codecov Report

Merging #2505 (71de85a) into main (a621868) will increase coverage by 2.85%.
The diff coverage is 97.22%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2505      +/-   ##
==========================================
+ Coverage   93.60%   96.45%   +2.85%     
==========================================
  Files         198      198              
  Lines        8177     8244      +67     
  Branches     1478     1500      +22     
==========================================
+ Hits         7654     7952     +298     
+ Misses        379      184     -195     
+ Partials      144      108      -36     

@tsmith023
Copy link
Contributor Author

tsmith023 commented Feb 1, 2023

Linking the previous review here: #2501 (comment) 😁

Edit: @patrick91, thanks for flagging that missing test! It identified a subtle bug within the validation logic!

@tsmith023 tsmith023 marked this pull request as ready for review February 1, 2023 21:47
@tsmith023 tsmith023 marked this pull request as draft February 1, 2023 21:47
@tsmith023 tsmith023 marked this pull request as ready for review February 4, 2023 12:30
@tsmith023
Copy link
Contributor Author

tsmith023 commented Feb 5, 2023

@patrick91, this PR is in a state to be reviewed :D

Edit: anybody? 😭 @DoctorJohn @BryceBeagle

Copy link
Member

@patrick91 patrick91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell me more about this feature? what's your use case?

Also in terms of API, I wonder if we could use callables instead of field rules. I think that might make our implementation shorter, and potentially the api could be easier to use 🤔

docs/extensions/query-depth-limiter.md Outdated Show resolved Hide resolved
@tsmith023
Copy link
Contributor Author

tsmith023 commented Mar 15, 2023

The use case is that described in #2338, which allows to limit the depth of queries depending on the arguments of specific fields in addition to the names of said fields.

Let's have a discussion in Discord on the implementation detail of callables instead of FieldRules as I'm not 100% clear on how that would look in the code 😁

I had also hoped to limit based on the sub-fields of a specific field, but this wasn't doable with my implementation. Perhaps callables would help here!

@tsmith023
Copy link
Contributor Author

@patrick91, this PR has been refactored to take into account the callables discussion we had previously. If you have time to review it then it should be ready to merge!

RELEASE.md Outdated Show resolved Hide resolved
RELEASE.md Outdated Show resolved Hide resolved
)
validator = create_validator(max_depth, should_ignore, callback)
else:
warnings.warn(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for adding the deprecation 😊

Comment on lines +67 to +70
FieldArgumentType = Union[
bool, int, float, str, List["FieldArgumentType"], Dict[str, "FieldArgumentType"]
]
FieldArgumentsType = Dict[str, FieldArgumentType]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might want to move these out at some point, or reuse something classes we already have

@patrick91 patrick91 merged commit c2d50c0 into strawberry-graphql:main May 22, 2023
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants