patrick.wtf

📝

Open-source updates - 17 October 2021

Open-source updates - 17 October 2021

Last two weeks have been a bit hectic personally, that’s why I’m only posting my weekly update now. Highlights from the last week are: a new FastAPI integration, an update to our Pydantic integration, a merge_types utility and improvements to our tests.

FastAPI integration

Not long ago we became FastAPI’s recommended library for GraphQL:

And slightly after Jiří Bireš contributed a PR to add a proper integration for FastAPI.

We used to recommend using ASGI for integrating for FastAPI, but now we have a better integration based on API Routers and allows to use FastAPI’s dependency injection system.

Also super happy to see many people reviewing this PR and making it better 😊

Pydantic integration update

Matt Allen made a PR to update our Pydantic integration to match Strawberry’s Django API. The old API is now deprecated, but we will keep it for a while since it is being used by few people. The API looks like this:

import pydantic
import strawberry
from strawberry.experimental.pydantic import auto

class User(pydantic.BaseModel):
    age: int
    password: str


@strawberry.experimental.pydantic.type(User)
class UserType:
    age: auto
    password: auto

instead of passing a list of fields to the decorator, you can now use the same API you use for all our decorators, but you can also use the “magic” auto keyword to let Strawberry infer the type from the Pydantic model.

This API is much nice than fields since it allows to easily declared the exposed fields and allows to also edit description of a specific field without having to pass the type of the field manually.

merge_types utility

Alexandru Mărășteanu made a PR with a merge_types utility. This new function allows to merge multiple types into one. It looks like this:

@strawberry.type
class QueryA:
    ...

@strawberry.type
class QueryB:
    ...

ComboQuery = merge_types((QueryB, QueryA))

schema = strawberry.Schema(query=ComboQuery)

This is an alternative to create a class that inherits from both QueryA and QueryB, and might be preferred by some people.

Test improvements

Michael Ossareh made a PR to run tests on Windows. He also fixed a small issue when running the Strawberry debug server on windows.

Now that we have tests running on windows, I’ve made a PR to add caching to our tests. Not sure I didn’t do this earlier, but now our tests should run quite faster as they don’t install all the dependencies all the time.

I’ve also made a PR to separate the Django tests. So now we have less tests to run for each python version and we’ll also only run Django tests for the latest version of Django. There’s not much need for us to run Django tests for multiple python versions. This will make our test suite faster.

Other PRs

Ester made a PR to add some checks when using @strawberry.type and other decorators. This will make it easier to work with Strawberry as it should make it easier to identify potential mistakes.

Jon made a PR with a test page for our docs. This is useful for us to check and improve our docs layout 😊

Jon also made a PR to fix a bug that was swallowing errors in the on_parse extension hook.

Finally Jon made a PR to update our schema docs.

Luke Murray made a PR to resolve LazyTypes in arguments. LazyTypes are useful when you have circular dependencies and now they work for arguments too!

I made a PR to run tests on Python 3.10 stable.

I also had to make a fix to prevent Strawberry to install FastAPI by default. Turns out marking dependencies as optional doesn’t work if they are not in an “extra” group.

Fin

I’m falling a bit behind with these updates, as they were supposed to be weekly updates, but in the last months I skipped them twice. I’ll try to get back on track soon, I have some personal life changes happening now, but hopefully they won’t distract me too much 😊

Webmentions