-
-
Notifications
You must be signed in to change notification settings - Fork 577
feat: add field extension support #2567
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
feat: add field extension support #2567
Conversation
566e9f7
to
9525070
Compare
9525070
to
c0c7bed
Compare
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #2567 +/- ##
==========================================
+ Coverage 96.28% 96.30% +0.01%
==========================================
Files 184 185 +1
Lines 7653 7708 +55
Branches 1404 1422 +18
==========================================
+ Hits 7369 7423 +54
Misses 183 183
- Partials 101 102 +1 |
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
Thanks for adding the Here's a preview of the changelog: Adds support for a custom field using the approach specified in issue #2168. from strawberry.extensions import FieldExtension
class UpperCaseExtension(FieldExtension):
async def resolve_async(
self, next: Callable[..., Awaitable[Any]], source: Any, info: Info, **kwargs
):
result = await next(source, info, **kwargs)
return str(result).upper()
@strawberry.type
class Query:
@strawberry.field(extensions=[UpperCaseExtension()])
async def string(self) -> str:
return "This is a test!!" query {
string
} {
"string": "THIS IS A TEST!!"
} Here's the tweet text:
|
2cb8d1c
to
f71430b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really great! 👏 Just added a couple of suggestions for the docs but otherwise I say we ship it.
Co-authored-by: Jonathan Kim <jkimbo@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍🏼
There are some comments that I made at the other PR, but they are mostly for the permissions integration.
Hi 👋 You can find a preview of the docs here: https://strawberry.rocks/docs/pr/2567/guides/custom-extensions |
docs/guides/field-extensions.md
Outdated
|
||
## Combining multiple field extensions | ||
|
||
When chaining multiple field extensions, the outermost extension is called first, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think we could replace outermost
with last?
or could we make it so that the order is the same as the one you pass? I think the current behaviour might be confusing 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to have a diagram here for nested extensions, that would clear things up a lot. Do we have a preferred tool for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's use mermaid, it should work on github too: https://mermaid.js.org
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, maybe vertical, as it might be easier to read on mobile 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I just realized that doesn't work because the content inside those tags (the diagram in this case) won't be parsed by markdown then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@erikwrede we could make a custom component for that if it helps
or add a directive, like
```mermaid (mobile:vertical)
if it is easy to change the setting 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok it actually looks just fine on the github preview. The strawberry preview doesn't seem to update though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's probably because we don't support mermaid yet on the site :D I'll see if I can add support to the current design in the next couple of days 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also add the graph later when the new website is ready
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome 😊
🎉 |
* feat: add field extension support * fix: revert imports broken by pycharm * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: match error message on test * chore: refactor & add RELEASE.md * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * chore: make mypy happy * chore: enable async-only extensions on sync * chore: adjust callable type hint for resolve_async * hopefully fix codecov... * hopefully fix codecov... #2 * fix: make field directives mutable * docs: add field extension documentation * chore: change next to next_ * chore: make sync-only extensions before async-only extensions possible * fix: test case match string adapted * feat(draft): add permission extension blocked by #2567 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: use permission.raise_error * chore: fix mypy, drop old PermissionError raise * fix: add permissions extension to field automatically * fix: fix failing tests * fix: recognize async permissions correctly * refactor: move permission error raising to extension * tests: fix test regex to support python <3.10 * refactor: enable silent permissions * fix?: move noqa comment * chore: refactor permissions directives and add RELEASE.md * fix: use correct type annotation for schema_directive * fix: remove permission classes from base resolver Co-authored-by: Thiago Bellini Ribeiro <hackedbellini@gmail.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * chore: address review comment * chore: add type alias type hint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * chore: add better error docs * chore: import type alias from extensions * chore: remove unused private method on strawberry field to improve code cov * chore: improve on review comments * chore: improve coverage * chore: improve coverage * docs: fix keyword name in Release.MD Co-authored-by: Etty <28976199+estyxx@users.noreply.github.com> * chore: rename handle_no_permission to on_unauthorized * docs: describe permission extensions * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: update cached property import * fix: update cached property import * chore: mypy fixes * Make sure we test the exception * Lint * "Improve" coverage --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Thiago Bellini Ribeiro <hackedbellini@gmail.com> Co-authored-by: Patrick Arminio <patrick.arminio@gmail.com> Co-authored-by: Etty <28976199+estyxx@users.noreply.github.com>
Description
Adds support for field extensions using the approach specified in PR #2168. Field extensions can support
sync
,async
or both.However,
sync
andasync
extensions cannot be mixed on the same field: The result of an async resolver would have to be awaited before calling the sync extension, making it impossible for the sync extension to modify any arguments before calling the resolvers.async-only
extensions can be applied tosync
resolvers, because we automatically wrap sync resolvers in aSyncToAsyncExtension
.Additional checks to verify no async extensions are used in a sync server (django, flask) may be added in a later PR.
Types of Changes
Issues Fixed or Closed by This PR
Checklist