strawberry

A GraphQL library for Python that leverages type annotations 🍓

MIT License

Downloads
1.1M
Stars
3.8K
Committers
260

Bot releases are hidden (Show)

strawberry - 🍓 0.156.0

Published by botberry over 1 year ago

This release adds support for specialized generic types.
Before, the following code would give an error, saying that T was not
provided to the generic type:

@strawberry.type
class Foo(Generic[T]):
    some_var: T


@strawberry.type
class IntFoo(Foo[int]):
    ...


@strawberry.type
class Query:
    int_foo: IntFoo

Also, because the type is already specialized, Int won't get inserted to its name,
meaning it will be exported to the schema with a type name of IntFoo and not
IntIntFoo.

For example, this query:

@strawberry.type
class Query:
    int_foo: IntFoo
    str_foo: Foo[str]

Will generate a schema like this:

type IntFoo {
  someVar: Int!
}

type StrFoo {
  someVar: String!
}

type Query {
  intFoo: IntFoo!
  strfoo: StrFoo!
}

Releases contributed by @bellini666 via #2517

strawberry - 🍓 0.155.4

Published by botberry over 1 year ago

Fix file not found error when exporting schema with lazy types from CLI #2469

Releases contributed by @skilkis via #2512

strawberry - 🍓 0.155.3

Published by botberry over 1 year ago

Fix missing custom resolve_reference for using pydantic with federation

i.e:

import typing
from pydantic import BaseModel
import strawberry
from strawberry.federation.schema_directives import Key


class ProductInDb(BaseModel):
    upc: str
    name: str


@strawberry.experimental.pydantic.type(
    model=ProductInDb, directives=[Key(fields="upc", resolvable=True)]
)
class Product:
    upc: str
    name: str

    @classmethod
    def resolve_reference(cls, upc):
        return Product(upc=upc, name="")
strawberry - 🍓 0.155.2

Published by botberry over 1 year ago

This release fixes a bug in subscriptions using the graphql-transport-ws protocol
where the conversion of the NextMessage object to a dictionary took an unnecessary
amount of time leading to an increase in CPU usage.

Releases contributed by @rjwills28 via #2481

strawberry - 🍓 0.155.1

Published by botberry over 1 year ago

A link to the changelog has been added to the package metadata, so it shows up on PyPI.

Releases contributed by @twm via #2490

strawberry - 🍓 0.155.0

Published by botberry over 1 year ago

This release adds a new utility function to convert a Strawberry object to a
dictionary.

You can use strawberry.asdict(...) function to convert a Strawberry object to
a dictionary:

@strawberry.type
class User:
    name: str
    age: int


# should be {"name": "Lorem", "age": 25}
user_dict = strawberry.asdict(User(name="Lorem", age=25))

Note: This function uses the dataclasses.asdict function under the hood, so
you can safely replace dataclasses.asdict with strawberry.asdict in your
code. This will make it easier to update your code to newer versions of
Strawberry if we decide to change the implementation.

Releases contributed by @Hazealign via #2417

strawberry - 🍓 0.154.1

Published by botberry almost 2 years ago

Fix DuplicatedTypeName exception being raised on generics declared using
strawberry.lazy. Previously the following would raise:

# issue_2397.py
from typing import Annotated, Generic, TypeVar

import strawberry

T = TypeVar("T")


@strawberry.type
class Item:
    name: str


@strawberry.type
class Edge(Generic[T]):
    node: T


@strawberry.type
class Query:
    edges_normal: Edge[Item]
    edges_lazy: Edge[Annotated["Item", strawberry.lazy("issue_2397")]]


if __name__ == "__main__":
    schema = strawberry.Schema(query=Query)

Releases contributed by @pre-commit-ci[bot] via #2462

strawberry - 🍓 0.154.0

Published by botberry almost 2 years ago

Support constrained float field types in Pydantic models.

i.e.

import pydantic

class Model(pydantic.BaseModel):
    field: pydantic.confloat(le=100.0)
	equivalent_field: float = pydantic.Field(le=100.0)

Releases contributed by @airwoodix via #2455

strawberry - 🍓 0.153.0

Published by botberry almost 2 years ago

This change allows clients to define connectionParams when making Subscription requests similar to the way Apollo-Server does it.

With Apollo-Client (React) as an example, define a Websocket Link:

import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { createClient } from 'graphql-ws';

const wsLink = new GraphQLWsLink(createClient({
  url: 'ws://localhost:4000/subscriptions',
  connectionParams: {
    authToken: user.authToken,
  },
}));

and the JSON passed to connectionParams here will appear within Strawberry's context as the connection_params attribute when accessing info.context within a Subscription resolver.

Releases contributed by @tsmith023 via #2380

strawberry - 🍓 0.152.0

Published by botberry almost 2 years ago

This release adds support for updating (or adding) the query document inside an
extension's on_request_start method.

This can be useful for implementing persisted queries. The old behavior of
returning a 400 error if no query is present in the request is still supported.

Example usage:

from strawberry.extensions import Extension

def get_doc_id(request) -> str:
    """Implement this to get the document ID using your framework's request object"""
    ...

def load_persisted_query(doc_id: str) -> str:
    """Implement this load a query by document ID. For example, from a database."""
    ...

class PersistedQuery(Extension):
    def on_request_start(self):
        request = self.execution_context.context.request

        doc_id = get_doc_id(request)

        self.execution_context.query = load_persisted_query(doc_id)

Releases contributed by @jthorniley via #2431

strawberry - 🍓 0.151.3

Published by botberry almost 2 years ago

This release adds support for FastAPI 0.89.0

Releases contributed by @patrick91 via #2440

strawberry - 🍓 0.151.2

Published by botberry almost 2 years ago

This release fixes @strawberry.experimental.pydantic.type and adds support for the metadata attribute on fields.

Example:

@strawberry.experimental.pydantic.type(model=User)
class UserType:
    private: strawberry.auto = strawberry.field(metadata={"admin_only": True})
    public: strawberry.auto

Releases contributed by @huyz via #2415

strawberry - 🍓 0.151.1

Published by botberry almost 2 years ago

This release fixes an issue that prevented using generic
that had a field of type enum. The following works now:

@strawberry.enum
class EstimatedValueEnum(Enum):
    test = "test"
    testtest = "testtest"


@strawberry.type
class EstimatedValue(Generic[T]):
    value: T
    type: EstimatedValueEnum


@strawberry.type
class Query:
    @strawberry.field
    def estimated_value(self) -> Optional[EstimatedValue[int]]:
        return EstimatedValue(value=1, type=EstimatedValueEnum.test)

Releases contributed by @patrick91 via #2411

strawberry - 🍓 0.151.0

Published by botberry almost 2 years ago

This PR adds a new graphql_type parameter to strawberry.field that allows you
to explicitly set the field type. This parameter will take preference over the
resolver return type and the class field type.

For example:

@strawberry.type
class Query:
    a: float = strawberry.field(graphql_type=str)
    b = strawberry.field(graphql_type=int)

    @strawberry.field(graphql_type=float)
    def c(self) -> str:
        return "3.4"

schema = strawberry.Schema(Query)

str(schema) == """
  type Query {
    a: String!
    b: Int!
    c: Float!
  }
"""

Releases contributed by @jkimbo via #2313

strawberry - 🍓 0.150.1

Published by botberry almost 2 years ago

Fixed field resolvers with nested generic return types
(e.g. list, Optional, Union etc) raising TypeErrors.
This means resolver factory methods can now be correctly type hinted.

For example the below would previously error unless you ommited all the
type hints on resolver_factory and actual_resolver functions.

from typing import Callable, Optional, Type, TypeVar

import strawberry


@strawberry.type
class Cat:
    name: str


T = TypeVar("T")


def resolver_factory(type_: Type[T]) -> Callable[[], Optional[T]]:
    def actual_resolver() -> Optional[T]:
        # load rows from database and cast to type etc
        ...

    return actual_resolver


@strawberry.type
class Query:
    cat: Cat = strawberry.field(resolver_factory(Cat))


schema = strawberry.Schema(query=Query)
strawberry - 🍓 0.150.0

Published by botberry almost 2 years ago

This release implements the ability to use custom caching for dataloaders.
It also allows to provide a cache_key_fn to the dataloader. This function
is used to generate the cache key for the dataloader. This is useful when
you want to use a custom hashing function for the cache key.

strawberry - 🍓 0.149.2

Published by botberry almost 2 years ago

This release fixes support for generics in arguments, see the following example:

T = TypeVar('T')

@strawberry.type
class Node(Generic[T]):
   @strawberry.field
   def data(self, arg: T) -> T:  # `arg` is also generic
       return arg
strawberry - 🍓 0.149.1

Published by botberry almost 2 years ago

This release improves the performance of rich exceptions on custom scalars
by changing how frames are fetched from the call stack.
Before the change, custom scalars were using a CPU intensive call to the
inspect module to fetch frame info which could lead to serious CPU spikes.

strawberry - 🍓 0.149.0

Published by botberry almost 2 years ago

This release does some internal refactoring of the HTTP views, hopefully it
doesn't affect anyone. It mostly changes the status codes returned in case of
errors (e.g. bad JSON, missing queries and so on).

It also improves the testing, and adds an entirely new test suite for the HTTP
views, this means in future we'll be able to keep all the HTTP views in sync
feature-wise.

strawberry - 🍓 0.148.0

Published by botberry almost 2 years ago

This release changes the get_context, get_root_value and process_result
methods of the Flask async view to be async functions. This allows you to use
async code in these methods.

Package Rankings
Top 1.15% on Pypi.org
Top 16.1% on Conda-forge.org
Badges
Extracted from project README
CircleCI Discord PyPI
Related Projects