-
-
Notifications
You must be signed in to change notification settings - Fork 541
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
Add ability to set a custom info class for a schema #3592
Add ability to set a custom info class for a schema #3592
Conversation
Reviewer's Guide by SourceryThis pull request introduces a new feature that allows users to set a custom File-Level Changes
Tips
|
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.
Hey @parafoxia - I've reviewed your changes - here's some feedback:
Overall Comments:
- This is a well-implemented feature that adds useful flexibility to the library. Consider adding tests for the
config
module, including this newinfo_class
option, to ensure long-term reliability.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟡 Documentation: 3 issues found
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.
Thanks for adding the Here's a preview of the changelog: You can now configure your schemas to provide a custom subclass of import strawberry
from strawberry.schema.config import StrawberryConfig
from .models import ProductModel
class CustomInfo(strawberry.Info):
@property
def selected_group_id(self) -> int | None:
"""Get the ID of the group you're logged in as."""
return self.context["request"].headers.get("Group-ID")
@strawberry.type
class Group:
id: strawberry.ID
name: str
@strawberry.type
class User:
id: strawberry.ID
name: str
group: Group
@strawberry.type
class Query:
@strawberry.field
def user(self, id: strawberry.ID, info: CustomInfo) -> Product:
kwargs = {"id": id, "name": ...}
if info.selected_group_id is not None:
# Get information about the group you're a part of, if
# available.
kwargs["group"] = ...
return User(**kwargs)
schema = strawberry.Schema(
Query,
config=StrawberryConfig(info_class=CustomInfo),
) Here's the tweet text:
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3592 +/- ##
=======================================
Coverage 96.75% 96.76%
=======================================
Files 521 522 +1
Lines 33732 33797 +65
Branches 5627 5635 +8
=======================================
+ Hits 32639 32705 +66
+ Misses 861 860 -1
Partials 232 232 |
CodSpeed Performance ReportMerging #3592 will not alter performanceComparing Summary
|
2e38710
to
3c37550
Compare
I've just rebased this on master so it should now pass the Pyright test stuff. |
@patrick91 Would it be possible for you to look at this today? |
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.
Looks solid, I left a probably annoying comment on the release, but other than that I think it's ok to merge :)
Ah, maybe we want to include this in the docs too :)
Happy to put this in the docs, I did not realise the config system had docs 😅 |
c3c93f0
to
cd69805
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.
Excellent @parafoxia, thanks for the changes. This looks good to me now. Let's have @patrick91 take a final look at the example in the RELEASE file. IMHO just showing how to use a custom Info class would have been enough there, but I'm also not against the current example, especially given the extra afford you already put into it.
…s docs Co-authored-by: Jonathan Ehwald <[email protected]>
for more information, see https://pre-commit.ci
cd69805
to
74cdd58
Compare
Just rebased onto the changes in the doc system. |
for more information, see https://pre-commit.ci
Thanks! I wasn't sure how full an example it needed to be lmao, so I just went hog on it 😆 |
@patrick91 Just bumping this 😄 |
Just updated this to custom info classes get injected into extensions as well. |
Oh, great catch! It would be great to have a test case checking that the custom Info class is used in directives and extensions. What do you think? :) |
Done! |
Hi @patrick91, just bumping this for review again. |
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.
Thank you! And sorry for the late review, this looks great!
Thanks for contributing to Strawberry! 🎉 You've been invited to join You can also request a free sticker by filling this form: https://forms.gle/dmnfQUPoY5gZbVT67 And don't forget to join our discord server: https://strawberry.rocks/discord 🔥 |
Description
This PR adds an option to
StrawberryConfig
that allows the user to provide a subclass ofstrawberry.Info
to use in types and queries instead of the default.An example can be found in the RELEASE.md.
Rationale
Being able to extend the
Info
class allows for the introduction of special utilities that use information within the instance.These are too specialised to be contributed to Strawberry generally, but custom
Info
support would allow these use-cases, and others like them, to be used where individuals and organisations see fit.Types of Changes
Issues Fixed or Closed by This PR
Checklist
Summary by Sourcery
Introduce the ability to set a custom info class in
StrawberryConfig
, allowing users to provide a subclass ofstrawberry.Info
for use in types and queries. Update documentation with an example of this new feature.New Features:
StrawberryConfig
to allow users to provide a custom subclass ofstrawberry.Info
for use in types and queries.Documentation:
info_class
option inRELEASE.md
.