# -*- coding: utf-8 -*-
from setuptools import setup

packages = \
['strawberry_django',
 'strawberry_django.mutations',
 'strawberry_django.queries']

package_data = \
{'': ['*']}

install_requires = \
['Django>=3.0', 'strawberry-graphql>=0.53']

setup_kwargs = {
    'name': 'strawberry-graphql-django',
    'version': '0.1.4',
    'description': 'Strawberry GraphQL Django extension',
    'long_description': '# Strawberry GraphQL Django extension\n\n[![CI](https://github.com/la4de/strawberry-graphql-django/actions/workflows/main.yml/badge.svg)](https://github.com/la4de/strawberry-graphql-django/actions/workflows/main.yml)\n[![PyPI](https://img.shields.io/pypi/v/strawberry-graphql-django)](https://pypi.org/project/strawberry-graphql-django/)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/strawberry-graphql-django)](https://pypi.org/project/strawberry-graphql-django/)\n\nThis library provides helpers to generate types, mutations and resolvers for Django models.\n\nInstalling strawberry-graphql-django packet from the python package repository.\n```shell\npip install strawberry-graphql-django\n```\n\n\n## Example project files\n\nSee example Django project [examples/django](https://github.com/strawberry-graphql/strawberry-graphql-django/tree/main/examples/django).\n\nmodels.py\n```python\nfrom django.db import models\n\nclass User(models.Model):\n    name = models.CharField(max_length=50)\n    groups = models.ManyToManyField(\'Group\', related_name=\'users\')\n\nclass Group(models.Model):\n    name = models.CharField(max_length=50)\n```\n\ntypes.py\n```python\nimport strawberry\nimport strawberry_django\nfrom . import models\n\n# model types are collected into register. type converters use\n# register to resolve types of relation fields\ntypes = strawberry_django.TypeRegister()\n\n@types.register\n@strawberry_django.type(models.User, types=types)\nclass User:\n    # types can be extended with own fields and resolvers\n    @strawberry.field\n    def name_upper(root) -> str:\n        return root.name.upper()\n\n@types.register\n@strawberry_django.type(models.Group, fields=[\'id\'], types=types)\nclass Group:\n    # fields can be remapped\n    group_name: str = strawberry_django.field(field_name=\'name\')\n\n@types.register\n@strawberry_django.input(models.User)\nclass UserInput:\n    pass\n```\n\nschema.py\n```python\nimport strawberry, strawberry_django\nfrom . import models\nfrom .types import types\n\nQuery = strawberry_django.queries(models.User, models.Group, types=types)\nMutation = strawberry_django.mutations(models.User, types=types)\nschema = strawberry.Schema(query=Query, mutation=Mutation)\n```\n\nurls.py\n```python\nfrom django.urls import include, path\nfrom strawberry.django.views import AsyncGraphQLView\nfrom .schema import schema\n\nurlpatterns = [\n    path(\'graphql\', AsyncGraphQLView.as_view(schema=schema)),\n]\n```\n\nNow we have models, types, schema and graphql view. It is time to crete database and start development server.\n```shell\nmanage.py makemigrations\nmanage.py migrate\nmanage.py runserver\n```\n\n## Mutations and Queries\n\nOnce the server is running you can open your browser to http://localhost:8000/graphql and start testing auto generated queries and mutations.\n\nCreate new user.\n```\nmutation {\n  createUsers(data: { name: "my user" }) {\n    id\n  }\n}\n```\n\nMake first queries.\n```\nquery {\n  user(id: 1) {\n    name\n    groups {\n      groupName\n    }\n  }\n  users(filters: ["name__contains=\'my\'"]) {\n    id\n    name\n    nameUpper\n  }\n}\n```\n\nUpdate user data.\n```\nmutation {\n  updateUsers(data: {name: "new name"}, filters: ["id=1"]) {\n    id\n    name\n  }\n}\n```\n\nFinally delete user.\n```\nmutation {\n  deleteUsers(filters: ["id=1"])\n}\n```\n\n## Typed filtering with Django Filter\n\nInstall django-filters `pip install django-filter`\n\nThen add `django_filters` to your INSTALLED_APPS.\n\n```python\nINSTALLED_APPS = [\n    ...\n    \'django_filters\',\n]\n```\n\nWrite a `FilterSet` and decorate it with `strawberry_django.filter`:\n\n```python\nimport strawberry_django\nimport django_filters\n\n\n@strawberry_django.filter\nclass UserFilter(django_filters.FilterSet):\n    # These fields definitions are optional, \n    # they can be automatically generated.\n\n    name = django_filters.CharFilter(lookup_expr="icontains")\n    search = django_filters.CharFilter(method="filter_search")\n\n    def filter_search(self, queryset, name, value):\n        return queryset.filter(\n            Q(name__icontains=value) |\n            Q(group__name__icontains=value) |\n            Q(tag__name__icontains=value)\n        )\n\n    class Meta:\n        model = models.User\n        fields = ["name", "search"]\n```\n\nCreate the input type with `UserFilter`, and filter a queryset with `strawberry_django.filters.apply`\n\n```python\n@strawberry.type\nclass Query:\n    @strawberry.field\n    def user_ids(self, filters: Optional[UserFilter] = UNSET) -> List[int]:\n        queryset = models.User.objects.all()\n        queryset = strawberry_django.filters.apply(filters, queryset)\n        return queryset.order_by("pk").values_list("pk", flat=True)\n```\n\nThe schema will be:\n```\ntype Query {\n  userIds(filters: UserFilter): [Int!]!\n}\n\ninput UserFilter {\n  name: String\n  search: String\n}\n```\n\nAnd can be queried with:\n```\nquery getUserIds {\n    userIds (filters: {"name": "user1", "search": "tag2"})\n}\n```\n\nSee django-filter docs for details on writing FilterSets: https://django-filter.readthedocs.io\n\n## Django authentication examples\n\n`strawberry_django` provides mutations for authentications.\n\nschema.py:\n```\nclass IsAuthenticated(strawberry.BasePermission):\n    def has_permission(self, source: Any, info: Info, **kwargs) -> bool:\n        self.message = "Not authenticated"\n        return info.context.request.user.is_authenticated\n\n@strawberry.type\nclass Query:\n    @strawberry.field(permission_classes=[IsAuthenticated])\n    def current_user(self, info: Info) -> types.User:\n        return info.context.request.user\n\nschema = strawberry.Schema(query=Query, mutation=strawberry_django.AuthMutation)\n```\n\nLogin and logout with:\n```\nmutation {\n  login(username:"myuser", password:"mypassword")\n  logout()\n}\n```\n\nGet current user with:\n```\nquery {\n  currentUser {\n    id\n    firstName\n    lastName\n  }\n}\n```\n\n## Running unit tests\n```\npoetry install\npoetry run pytest\n```\n\n## Contributing\n\nI would be more than happy to get pull requests, improvement ideas and feedback from you.\n',
    'author': 'Lauri Hintsala',
    'author_email': 'lauri.hintsala@verkkopaja.fi',
    'maintainer': None,
    'maintainer_email': None,
    'url': 'https://github.com/strawberry-graphql/strawberry-graphql-django',
    'packages': packages,
    'package_data': package_data,
    'install_requires': install_requires,
    'python_requires': '>=3.7,<4.0',
}


setup(**setup_kwargs)
