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

feat: automatically include implementers of interfaces in schema #3686

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

erikwrede
Copy link
Member

@erikwrede erikwrede commented Nov 1, 2024

This PR implements automatically adding interface subclasses to the schema.

However, it only acts as a proof of concept as we currently lazily evaluate fields during instantiation of the GraphQL Core Schema (since #1684).
This making it impossible to collect all extra types before instantiating the GraphQL core schema.

Copy link
Contributor

sourcery-ai bot commented Nov 1, 2024

Reviewer's Guide by Sourcery

This PR implements automatic inclusion of interface implementers in the GraphQL schema. The implementation modifies the schema conversion process to detect and include classes that inherit from interface types, even if they're not explicitly referenced in the schema. This is demonstrated through new test cases using a cheese-themed example.

Sequence diagram for schema creation with interface implementers

sequenceDiagram
    participant Developer
    participant SchemaConverter
    participant GraphQLSchema
    Developer->>SchemaConverter: Create schema
    SchemaConverter->>SchemaConverter: Detect interface implementers
    SchemaConverter->>GraphQLSchema: Add implementers to schema
    GraphQLSchema-->>Developer: Schema with implementers
    note right of SchemaConverter: Automatically includes implementers of interfaces
Loading

Class diagram for the updated schema conversion process

classDiagram
    class SchemaConverter {
        +Dict extra_interface_child_map
        +Dict type_map
        +Config config
        +Dict scalar_registry
        +Callable get_fields
    }
    class StrawberryObjectDefinition
    class GraphQLSchema
    class GraphQLCoreConverter
    SchemaConverter --> StrawberryObjectDefinition : extra_interface_child_map
    SchemaConverter --> GraphQLSchema
    SchemaConverter --> GraphQLCoreConverter
    note for SchemaConverter "New attribute extra_interface_child_map added to track interface implementers."
Loading

File-Level Changes

Change Details Files
Added functionality to automatically detect and include interface implementations
  • Added a new dictionary to track additional interface implementations
  • Implemented subclass detection for interface types during type resolution
  • Modified schema creation to include detected interface implementations
  • Added TODO note about preventing duplicate processing
strawberry/schema/schema_converter.py
strawberry/schema/schema.py
Added comprehensive test cases for interface implementation
  • Created test case for interface queries without explicitly adding types
  • Added test case to verify behavior with duplicate references
  • Implemented cheese-themed example hierarchy with Italian and Swiss cheese types
tests/schema/test_interface.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@botberry
Copy link
Member

botberry commented Nov 1, 2024

Hi, thanks for contributing to Strawberry 🍓!

We noticed that this PR is missing a RELEASE.md file. We use that to automatically do releases here on GitHub and, most importantly, to PyPI!

So as soon as this PR is merged, a release will be made 🚀.

Here's an example of RELEASE.md:

Release type: patch

Description of the changes, ideally with some examples, if adding a new feature.

Release type can be one of patch, minor or major. We use semver, so make sure to pick the appropriate type. If in doubt feel free to ask :)

Here's the tweet text:

🆕 Release (next) is out! Thanks to Erik Wrede for the PR 👏

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

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 @erikwrede - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Please fix the TODO regarding duplicate processing - while it doesn't cause errors, it could impact performance unnecessarily.
  • Consider adding documentation explaining why this specific implementation approach was chosen, given the comment about it being 'somewhat hacky'.
  • The PR template is incomplete - please check the appropriate boxes for the type of change and other checklist items.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

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 and I'll use the feedback to improve your reviews.

# We need to find a way to add the extra implementations to the schema after creating it.
# This is not officially supported by GraphQL core and would be somewhat hacky.

# TODO: prevent duplicates - no error, but duplicate processing is inefficient
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (performance): Consider using a set or bulk extend operation to prevent duplicate processing

Instead of appending types one by one, consider collecting them in a set first or using list.extend() to add them all at once. This would prevent duplicate processing and improve performance.

            types = set()  # Using a set to automatically prevent duplicates

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

codecov bot commented Nov 1, 2024

Codecov Report

Attention: Patch coverage is 96.29630% with 2 lines in your changes missing coverage. Please review.

Project coverage is 94.83%. Comparing base (8859ad3) to head (348c23e).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3686      +/-   ##
==========================================
- Coverage   97.02%   94.83%   -2.20%     
==========================================
  Files         503      499       -4     
  Lines       33520    32417    -1103     
  Branches     5632     1673    -3959     
==========================================
- Hits        32524    30743    -1781     
- Misses        790     1404     +614     
- Partials      206      270      +64     

Copy link

codspeed-hq bot commented Nov 1, 2024

CodSpeed Performance Report

Merging #3686 will not alter performance

Comparing erikwrede:feat/3684-include-implementers-of-interfaces (348c23e) with main (3e2b9bf)

Summary

✅ 15 untouched benchmarks

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.

2 participants