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 Litestar integration #3213

Merged
merged 7 commits into from
Jan 24, 2024

Conversation

gazorby
Copy link
Contributor

@gazorby gazorby commented Nov 9, 2023

Add support for Litestar.

Description

This adds a new integration and does not replace the Starlite one. It adds a bunch of duplicated code but allows a smoother transition from Starlite to Litestar.

We may decide to keep both for some time and consider dropping Starlite later on.

Usage is pretty much identical as for Starlite :

import strawberry
from litestar import Request, Litestar
from strawberry.litestar import make_graphql_controller, BaseContext
from strawberry.types.info import Info

@strawberry.type
class Query:
    @strawberry.field
    def hello(self, info: Info[object, None]) -> str:
        return info.context["custom"]


class CustomContext(BaseContext):
    session: AsyncSession


async def custom_context_getter():
    async with async_session_maker() as session:
        yield CustomContext(session=session)


schema = strawberry.Schema(Query)

GraphQLController = make_graphql_controller(
    schema,
    path="/graphql",
    context_getter=custom_context_getter
)

app = Litestar(route_handlers=[GraphQLController])

Types of Changes

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

Issues Fixed or Closed by This PR

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

@botberry
Copy link
Member

botberry commented Nov 9, 2023

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release adds support for litestar.

import strawberry
from litestar import Request, Litestar
from strawberry.litestar import make_graphql_controller
from strawberry.types.info import Info


def custom_context_getter(request: Request):
    return {"custom": "context"}


@strawberry.type
class Query:
    @strawberry.field
    def hello(self, info: Info[object, None]) -> str:
        return info.context["custom"]


schema = strawberry.Schema(Query)


GraphQLController = make_graphql_controller(
    schema,
    path="/graphql",
    context_getter=custom_context_getter,
)

app = Litestar(
    route_handlers=[GraphQLController],
)

Here's the tweet text:

🆕 Release (next) is out! Thanks to Matthieu MN for the PR 👏

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

Copy link

codecov bot commented Nov 9, 2023

Codecov Report

Merging #3213 (34ce3a1) into main (7830bd2) will decrease coverage by 0.18%.
The diff coverage is 89.26%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3213      +/-   ##
==========================================
- Coverage   96.58%   96.40%   -0.18%     
==========================================
  Files         486      498      +12     
  Lines       30334    31107     +773     
  Branches     3751     3811      +60     
==========================================
+ Hits        29298    29989     +691     
- Misses        838      912      +74     
- Partials      198      206       +8     

Copy link

codspeed-hq bot commented Nov 9, 2023

CodSpeed Performance Report

Merging #3213 will not alter performance

Comparing gazorby:feat/litestar-integration (34ce3a1) with main (7830bd2)

Summary

✅ 13 untouched benchmarks

@gazorby
Copy link
Contributor Author

gazorby commented Nov 9, 2023

Documentation is missing, and the one for Starlite does not show up in the index btw

@gazorby gazorby changed the title Add litestar integration Add Litestar integration Nov 9, 2023
@gazorby
Copy link
Contributor Author

gazorby commented Nov 9, 2023

Documentation added, should ready for review

@betaboon
Copy link

is there anything we can do to help out getting this in?

@patrick91
Copy link
Member

@gazorby thank you for this and sorry for the delay, I'll review and merge soon 😊

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.

Awesome! I think we'll have to deprecate starlite, but we can do that in another PR 😊

@patrick91 patrick91 merged commit fdd06a4 into strawberry-graphql:main Jan 24, 2024
62 of 64 checks passed
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.

Missing starlite docs
4 participants