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

WIP defer support #3753

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

WIP defer support #3753

wants to merge 1 commit into from

Conversation

patrick91
Copy link
Member

@patrick91 patrick91 commented Jan 14, 2025

Worked on this with @bellini666

Still need to check:

  • extensions
  • commented out code
  • stream (Streamable type)
  • multipart subscriptions are probably broken

Summary by Sourcery

Implement support for @defer directive. This allows deferring the execution of specific fields in a query, improving initial response times.

New Features:

  • Added support for the @defer directive, enabling deferred execution of selected fields within a query.

Tests:

  • Added tests for the @defer directive, covering basic functionality and multipart subscriptions.

Copy link
Contributor

sourcery-ai bot commented Jan 14, 2025

Reviewer's Guide by Sourcery

This PR implements support for @defer and @stream directives. It introduces handling for multipart responses in subscriptions and modifies the GraphiQL HTML to use a newer version.

Sequence diagram for incremental execution with @defer

sequenceDiagram
    participant Client
    participant Server
    participant GraphQL

    Client->>Server: Send GraphQL query with @defer
    Server->>GraphQL: Execute query incrementally
    GraphQL-->>Server: Return initial result
    Server-->>Client: Send initial response

    loop Subsequent Results
        GraphQL-->>Server: Return deferred result
        Server-->>Client: Send multipart response
    end

    Server-->>Client: Send end boundary
Loading

Class diagram for incremental execution results

classDiagram
    class ExecutionResult
    class ExperimentalIncrementalExecutionResults
    class InitialIncrementalExecutionResult
    class SubsequentIncrementalExecutionResult
    class IncrementalResult
    class IncrementalDeferResult
    class IncrementalStreamResult

    ExperimentalIncrementalExecutionResults --> InitialIncrementalExecutionResult
    ExperimentalIncrementalExecutionResults --> SubsequentIncrementalExecutionResult

    IncrementalResult <|-- IncrementalDeferResult
    IncrementalResult <|-- IncrementalStreamResult

    class IncrementalDeferResult {
        +data
        +errors
        +path
        +label
        +extensions
    }

    class IncrementalStreamResult {
        +items
        +errors
        +path
        +label
        +extensions
    }
Loading

Flow diagram for multipart response handling

flowchart TD
    A[Receive Query] --> B{Is Incremental?}
    B -->|Yes| C[Process Initial Result]
    C --> D[Send Initial Response]
    D --> E[Process Subsequent Results]
    E --> F{Has Next?}
    F -->|Yes| E
    F -->|No| G[Send End Boundary]

    B -->|No| H[Process Normal Result]
    H --> I[Send Response]

    style B decision
    style F decision
Loading

File-Level Changes

Change Details Files
Implement @defer directive
  • Added handling for ExperimentalIncrementalExecutionResults in run method to support @defer.
  • Modified execute to use experimental_execute_incrementally.
  • Added process_incremental_result and process_subsequent_result methods to handle incremental results.
  • Added tests for basic defer functionality.
  • Updated process_result to handle InitialIncrementalExecutionResult objects.
strawberry/http/async_base_view.py
strawberry/schema/execute.py
tests/http/incremental/test_defer.py
Implement @stream directive
  • Added GraphQLStreamDirective to the schema.
  • Updated release notes to mention @defer.
strawberry/schema/schema.py
RELEASE.md
Update GraphiQL version
  • Updated GraphiQL library from version 3.0.9 to 3.8.3.
strawberry/static/graphiql.html
Implement multipart subscription support
  • Added encode_multipart_data method to format multipart responses.
  • Added tests for multipart subscriptions.
  • Added fixtures for testing multipart subscriptions.
  • Removed old tests for multipart subscriptions.
strawberry/http/async_base_view.py
tests/http/incremental/test_multipart_subscription.py
tests/http/incremental/conftest.py
tests/http/test_multipart_subscription.py
Update graphql-core dependency
  • Updated graphql-core dependency to version 3.3.0a6.
  • Updated poetry.lock file to reflect the dependency change.
pyproject.toml
poetry.lock

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

@patrick91 patrick91 force-pushed the feature/defer-with-thiago branch from 9e7d000 to 573fa72 Compare January 14, 2025 18:45
Copy link

codspeed-hq bot commented Jan 14, 2025

CodSpeed Performance Report

Merging #3753 will improve performances by ×3.2

Comparing feature/defer-with-thiago (573fa72) with main (fa5c2d0)

Summary

⚡ 3 improvements
✅ 18 untouched benchmarks

Benchmarks breakdown

Benchmark main feature/defer-with-thiago Change
test_execute_generic_input 410.6 ms 369.7 ms +11.07%
test_subscription_long_run[1000] 179 ms 59.3 ms ×3
test_subscription_long_run[20000] 3.5 s 1.1 s ×3.2

Copy link

codecov bot commented Jan 15, 2025

Codecov Report

Attention: Patch coverage is 2.38095% with 82 lines in your changes missing coverage. Please review.

Project coverage is 9.89%. Comparing base (fa5c2d0) to head (573fa72).

❗ There is a different number of reports uploaded between BASE (fa5c2d0) and HEAD (573fa72). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (fa5c2d0) HEAD (573fa72)
2 1
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #3753       +/-   ##
==========================================
- Coverage   97.27%   9.89%   -87.38%     
==========================================
  Files         504     502        -2     
  Lines       33481   32353     -1128     
  Branches     5503    1671     -3832     
==========================================
- Hits        32567    3202    -29365     
- Misses        703   28995    +28292     
+ Partials      211     156       -55     

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.

1 participant