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

Add ability to set a custom info class for a schema #3592

Merged
merged 14 commits into from
Sep 16, 2024

Conversation

parafoxia
Copy link
Contributor

@parafoxia parafoxia commented Aug 7, 2024

Description

This PR adds an option to StrawberryConfig that allows the user to provide a subclass of strawberry.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

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

Issues Fixed or Closed by This PR

  • None

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).

Summary by Sourcery

Introduce the ability to set a custom info class in StrawberryConfig, allowing users to provide a subclass of strawberry.Info for use in types and queries. Update documentation with an example of this new feature.

New Features:

  • Add option to StrawberryConfig to allow users to provide a custom subclass of strawberry.Info for use in types and queries.

Documentation:

  • Add example usage of the new info_class option in RELEASE.md.

Copy link
Contributor

sourcery-ai bot commented Aug 7, 2024

Reviewer's Guide by Sourcery

This pull request introduces a new feature that allows users to set a custom Info class in the StrawberryConfig. This is implemented by adding an info_class attribute to the StrawberryConfig class and updating the schema converter to use this custom class. The changes include validation to ensure the custom class is a subclass of strawberry.types.Info and an example usage in the RELEASE.md file.

File-Level Changes

Files Changes
strawberry/schema/config.py
strawberry/schema/schema_converter.py
Introduced a new info_class attribute in StrawberryConfig and updated the schema converter to utilize this custom class.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a 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 new info_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

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

RELEASE.md Outdated Show resolved Hide resolved
RELEASE.md Outdated Show resolved Hide resolved
RELEASE.md Outdated Show resolved Hide resolved
@botberry
Copy link
Member

botberry commented Aug 7, 2024

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


You can now configure your schemas to provide a custom subclass of
strawberry.types.Info to your types and queries.

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:

🆕 Release (next) is out! Thanks to Ethan Henderson for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

strawberry/schema/config.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Aug 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.76%. Comparing base (08cc1c2) to head (468155a).
Report is 5 commits behind head on main.

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           

Copy link

codspeed-hq bot commented Aug 7, 2024

CodSpeed Performance Report

Merging #3592 will not alter performance

Comparing parafoxia:task/custom-info-class (468155a) with main (08cc1c2)

Summary

✅ 15 untouched benchmarks

@parafoxia parafoxia force-pushed the task/custom-info-class branch from 2e38710 to 3c37550 Compare August 8, 2024 11:17
@parafoxia
Copy link
Contributor Author

I've just rebased this on master so it should now pass the Pyright test stuff.

@parafoxia
Copy link
Contributor Author

@patrick91 Would it be possible for you to look at this today?

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.

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 :)

RELEASE.md Outdated Show resolved Hide resolved
strawberry/schema/config.py Outdated Show resolved Hide resolved
@parafoxia
Copy link
Contributor Author

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 😅

@parafoxia parafoxia requested a review from patrick91 August 13, 2024 09:55
@parafoxia parafoxia force-pushed the task/custom-info-class branch from c3c93f0 to cd69805 Compare August 14, 2024 08:57
@parafoxia parafoxia requested a review from DoctorJohn August 14, 2024 15:53
Copy link
Member

@DoctorJohn DoctorJohn left a 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.

@parafoxia parafoxia force-pushed the task/custom-info-class branch from cd69805 to 74cdd58 Compare August 16, 2024 10:34
@parafoxia
Copy link
Contributor Author

Just rebased onto the changes in the doc system.

@parafoxia
Copy link
Contributor Author

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.

Thanks! I wasn't sure how full an example it needed to be lmao, so I just went hog on it 😆

@parafoxia
Copy link
Contributor Author

@patrick91 Just bumping this 😄

@parafoxia
Copy link
Contributor Author

Just updated this to custom info classes get injected into extensions as well.

@DoctorJohn
Copy link
Member

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? :)

@parafoxia
Copy link
Contributor Author

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!

@parafoxia
Copy link
Contributor Author

Hi @patrick91, just bumping this for review again.

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.

Thank you! And sorry for the late review, this looks great!

@patrick91 patrick91 merged commit 7287047 into strawberry-graphql:main Sep 16, 2024
117 checks passed
@botberry
Copy link
Member

Thanks for contributing to Strawberry! 🎉 You've been invited to join
the Strawberry GraphQL organisation 😊

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 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants