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 Django Support #84

Closed
jaydenwindle opened this issue Jun 7, 2019 · 10 comments · Fixed by #123
Closed

Add Django Support #84

jaydenwindle opened this issue Jun 7, 2019 · 10 comments · Fixed by #123
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@jaydenwindle
Copy link
Member

I'd love to see strawberry support Django as a first class citizen via a contrib package. To start, it could be as simple as adding a Django view that exposes a GraphQL endpoint and the GraphQL Playground. From there, a lot of other neat features could be added, such as:

  • Django authentication integration
  • Out of the box subscription support using Django channels
  • Automatic type generation from Django models
  • Automatic CRUD query / mutation / subscription generation for Django models

Is this something that strawberry would be interested in supporting?

@patrick91 patrick91 added enhancement New feature or request help wanted Extra attention is needed labels Jun 8, 2019
@patrick91
Copy link
Member

Yes totally! I'd love to see this happen (I'd love to use strawberry for some Django side project I have).

I'm currently focused on finishing ASGI support (mainly adding tests and a better structure of the code), would you like to work on this? :)

@jaydenwindle
Copy link
Member Author

I'd love to work on this! I'll throw up some PRs in the next couple of days :)

@jaydenwindle
Copy link
Member Author

I've been giving this some thought, and I think that Django support would make more sense as it's own package (similar to how Graphene is set up). That way we don't have to worry about managing extra dependencies or releasing changes to strawberry that affect only the Django package.

I'll spin up a repo with the Django extensions and we can add it to the strawberry org when it's ready to release.

@patrick91
Copy link
Member

@jaydenwindle I'm a bit opposed to that, to be honest, there were some talks of merging some of the libraries back to the graphene repo. For example, if we do changes to strawberry "core" we need to update all the other packages probably.

For dependencies we could do something like:

pip install strawberry[django]

or even have a metapackage that does that for you:

pip install strawberry-django

:)

@jaydenwindle
Copy link
Member Author

Oh interesting - I didn't know there were discussions around that in Graphene. In that case I'll add it directly to this repo :)

@patrick91
Copy link
Member

@jaydenwindle :) thanks!

Hopefully it won't be a bad idea!

@jaydenwindle jaydenwindle self-assigned this Jul 8, 2019
@conatus
Copy link

conatus commented Aug 15, 2019

Would love to see this!

@Rocamonde
Copy link

Hi @patrick91! Does Django support at this stage also imply the automatic conversion of a QuerySet into the dataclass-like structure that Strawberry uses? So a resolver can return a model instance or a QuerySet, and this is converted to the appropriate type and handled accordingly by GraphQL.

@patrick91
Copy link
Member

@Rocamonde so, we don't do any implicit conversion, but duck typing works. If you return anything that looks like the GraphQL type then things work :)

So for example this works:

@strawberry.type
class Query:
    @strawberry.field
    def user(self) -> User:
         return models.User.objects.last()

we don't check what type is being returned in the resolvers. So this works, and works for lists too.

I think the only place where this doesn't work is Union types, because we need to understand what type we are returning (but we can find a work around for that).

We do this a lot in PyCon Italia's website: https://github.com/pythonitalia/pycon/blob/master/backend/api/conferences/types.py#L127-L155

Hope this helps :)

@la4de
Copy link
Contributor

la4de commented Nov 28, 2020

I have been missing django model integration so I started to prototype this feature. Please try and leave feedback. https://github.com/la4de/strawberry-graphql-django

There is sample projects which can be used for testing as well. https://github.com/la4de/strawberry-graphql-django/tree/main/examples/django

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants