A GraphQL library for Python that leverages type annotations 🍓
MIT License
Bot releases are hidden (Show)
Published by botberry 9 months ago
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],
)
Releases contributed by @gazorby via #3213
Published by botberry 9 months ago
This release fixes a small issue in the GraphQL Transport websocket
where the connection would fail when receiving extra parameters
in the payload sent from the client.
This would happen when using Apollo Sandbox.
Releases contributed by @patrick91 via #3356
Published by botberry 9 months ago
This release adds a new method get_fields
on the Schema
class.
You can use get_fields
to hide certain field based on some conditions,
for example:
@strawberry.type
class User:
name: str
email: str = strawberry.field(metadata={"tags": ["internal"]})
@strawberry.type
class Query:
user: User
def public_field_filter(field: StrawberryField) -> bool:
return "internal" not in field.metadata.get("tags", [])
class PublicSchema(strawberry.Schema):
def get_fields(
self, type_definition: StrawberryObjectDefinition
) -> List[StrawberryField]:
return list(filter(public_field_filter, type_definition.fields))
schema = PublicSchema(query=Query)
The schema here would only have the name
field on the User
type.
Releases contributed by @patrick91 via #3274
Published by botberry 10 months ago
This hotfix enables permission extensions to be used with AsyncGenerators.
Releases contributed by @erikwrede via #3318
Published by botberry 10 months ago
Permissions classes now use a FieldExtension
. The new preferred way to add permissions
is to use the PermissionsExtension
class:
import strawberry
from strawberry.permission import PermissionExtension, BasePermission
class IsAuthorized(BasePermission):
message = "User is not authorized"
error_extensions = {"code": "UNAUTHORIZED"}
def has_permission(self, source, info, **kwargs) -> bool:
return False
@strawberry.type
class Query:
@strawberry.field(extensions=[PermissionExtension(permissions=[IsAuthorized()])])
def name(self) -> str:
return "ABC"
The old way of adding permissions using permission_classes
is still
supported via the automatic addition of a PermissionExtension
on the field.
Using the new PermissionExtension
API, permissions support even more features:
To return None
or []
instead of raising an error, the fail_silently
keyword
argument on PermissionExtension
can be set to True
.
Permissions will now automatically add pre-defined error extensions to the error, and
can use a custom GraphQLError
class. This can be configured by modifying
the error_class
and error_extensions
attributes on the BasePermission
class.
To customize the error handling, the on_unauthorized
method on
the BasePermission
class can be used. Further changes can be implemented by
subclassing the PermissionExtension
class.
Permissions will automatically be added as schema directives to the schema. This
behavior can be altered by setting the add_directives
to False
on PermissionExtension
, or by setting the _schema_directive
class attribute of the
permission to a custom directive.
Releases contributed by @erikwrede via #2570
Published by botberry 10 months ago
Don't require NodeId
annotation if resolve_id is overwritten on Node
implemented types
Releases contributed by @devkral via #2844
Published by botberry 11 months ago
Override encode_json() method in Django BaseView to use DjangoJSONEncoder
Releases contributed by @noamsto via #3273
Published by botberry 11 months ago
Fixed the base view so it uses parse_json
when loading parameters from the query string instead of json.loads
.
Releases contributed by @thearchitector via #3272
Published by botberry 11 months ago
This release updates the Apollo Sandbox integration to all you to
pass cookies to the GraphQL endpoint by enabling the Include cookes
option in the Sandbox settings.
Releases contributed by @patrick91 via #3278
Published by botberry 11 months ago
Improved error message when supplying GlobalID format that relates to another type than the query itself.
Releases contributed by @sdobbelaere via #3194
Published by botberry 11 months ago
Adds an optional extensions
parameter to strawberry.federation.field
, with default value None
. The key is passed through to strawberry.field
, so the functionality is exactly as described here.
Example:
strawberry.federation.field(extensions=[InputMutationExtension()])
Releases contributed by @bricker via #3239
Published by botberry 11 months ago
This release updates the GraphiQL packages to their latest versions:
[email protected]
@graphiql/[email protected]
Releases contributed by @rodrigofeijao via #3227
Published by botberry 12 months ago
This release adds support in all all our HTTP integration for choosing between
different GraphQL IDEs. For now we support GraphiQL (the default),
Apollo Sandbox, and Pathfinder.
Deprecations: This release deprecates the graphiql
option in all HTTP integrations,
in favour of graphql_ide
, this allows us to only have one settings to change GraphQL ide,
or to disable it.
Here's a couple of examples of how you can use this:
import strawberry
from fastapi import FastAPI
from strawberry.fastapi import GraphQLRouter
from api.schema import schema
graphql_app = GraphQLRouter(schema, graphql_ide="apollo-sandbox")
app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")
from django.urls import path
from strawberry.django.views import GraphQLView
from api.schema import schema
urlpatterns = [
path("graphql/", GraphQLView.as_view(schema=schema, graphql_ide="pathfinder")),
]
Releases contributed by @patrick91 via #3209
Published by botberry 12 months ago
This release changes how we check for generic types. Previously, any type that
had a generic typevar would be considered generic for the GraphQL schema, this
would generate un-necessary types in some cases. Now, we only consider a type
generic if it has a typevar that is used as the type of a field or one of its arguments.
For example the following type:
@strawberry.type
class Edge[T]:
cursor: strawberry.ID
some_interna_value: strawberry.Private[T]
Will not generate a generic type in the schema, as the typevar T
is not used
as the type of a field or argument.
Releases contributed by @patrick91 via #3202
Published by botberry 12 months ago
This release removes unused graphiql
submodules for Flask, Quart and Sanic.
Releases contributed by @catwell via #3203
Published by botberry 12 months ago
This release fixes an issue that prevented the parser_cache
extension to be used in combination with
other extensions such as MaxTokensLimiter
.
The following should work as expected now:
schema = strawberry.Schema(
query=Query, extensions=[MaxTokensLimiter(max_token_count=20), ParserCache()]
)
Releases contributed by @Dazix via #3170
Published by botberry 12 months ago
This release adds a Quart view.
Releases contributed by @catwell via #3162
Published by botberry 12 months ago
This release deprecates our SentryTracingExtension
, as it is now incorporated directly into Sentry itself as of version 1.32.0. You can now directly instrument Strawberry with Sentry.
Below is the revised usage example:
import sentry_sdk
from sentry_sdk.integrations.strawberry import StrawberryIntegration
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
integrations=[
# make sure to set async_execution to False if you're executing
# GraphQL queries synchronously
StrawberryIntegration(async_execution=True),
],
traces_sample_rate=1.0,
)
Many thanks to @sentrivana for their work on this integration!
Releases contributed by @patrick91 via #3169
Published by botberry almost 1 year ago
Fix strawberry mypy plugin for pydantic v2
Releases contributed by @Corentin-Br via #3159
Published by botberry about 1 year ago
Remove stack_info from error log messages to not clutter error logging with unnecessary information.
Releases contributed by @finsterwalder via #3143