strawberry-django-phonenumber


Namestrawberry-django-phonenumber JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/UpliftAgency/strawberry-django-phonenumber
SummaryA strawberry + Django integration for phone numbers
upload_time2023-09-19 04:53:22
maintainer
docs_urlNone
authorPaul Craciunoiu
requires_python>=3.11,<4.0
licenseMIT
keywords strawberry django phonenumber phone number phonenumber-field graphql strawberry_django
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # strawberry-django-phonenumber

![build status](https://github.com/UpliftAgency/strawberry-django-phonenumber/actions/workflows/pythonpackage.yml/badge.svg)

## Introduction

GraphQL types for Phone Numbers with Strawberry Django. If you use `django`, `strawberry`, and `django-phonenumber-field`, this library is for you.

Supported on:

* Python 3.9+ (likely earlier versions too, needs tested)
* Django 3+
* strawberry-graphql-django 0.17+
* django-phonenumber-field 7+

Here's how it works. Automagically get this query:

```graphql
query User {
  phoneNumber {
    ...phoneNumberFragment
  }
}

fragment phoneNumberFragment on PhoneNumber {
    asInternational  # +1 415-418-3420
    asNational  # (415) 418-3420
    asE164
    asRfc3966
    countryCode  # 1
    nationalNumber
    extension
    rawInput
}
```

With this code:

```python
# yourapp/models.py
from django.contrib.auth.models import AbstractUser
from phonenumber_field.modelfields import PhoneNumberField


class User(AbstractUser):
    phone_number = PhoneNumberField(blank=True)

# yourapp/graphql/types.py
from typing import Optional, cast

import strawberry
import strawberry_django
from strawberry.types import Info
from strawberry_django_phonenumber import PhoneNumber

from yourapp import models



@strawberry_django.type(models.User)
class User(strawberry.relay.Node):
    """GraphQL type for the User model."""

    @strawberry_django.field
    async def phone_number(root, info: Info) -> Optional[PhoneNumber]:
        if not root.phone_number:
            return None
        return cast(PhoneNumber, root.phone_number)

# yourapp/graphql/__init__.py
from typing import Optional

import strawberry
import strawberry_django
from asgiref.sync import sync_to_async
from strawberry.types import Info

from .types import User


@sync_to_async
def aget_user_from_request(request):
    return request.user if bool(request.user) else None


@strawberry.type
class Queries:
    @strawberry_django.field
    async def me(self, info: Info) -> Optional[User]:
        user = await aget_user_from_request(info.context.request)
        return user


schema = strawberry.Schema(query=Queries)

# yourapp/urls.py

from django.urls import path
from django.views.decorators.csrf import csrf_exempt
from strawberry.django.views import AsyncGraphQLView

from .graphql import schema

urlpatterns = [
    path(
        "graphql/",
        csrf_exempt(
            AsyncGraphQLView.as_view(
                schema=schema,
                graphiql=True,
            )
        ),
    ),
]

```

## Installation

```bash
pip install strawberry-django-phonenumber
```

### Changelog

**0.1.0**

    - Initial release


## Contributing

Running tests:

```bash
poetry run pytest
```

That's it, lite process for now. Please open a pull request or issue.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/UpliftAgency/strawberry-django-phonenumber",
    "name": "strawberry-django-phonenumber",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.11,<4.0",
    "maintainer_email": "",
    "keywords": "strawberry,django,phonenumber,phone,number,phonenumber-field,graphql,strawberry_django",
    "author": "Paul Craciunoiu",
    "author_email": "paul@craciunoiu.net",
    "download_url": "https://files.pythonhosted.org/packages/0f/89/4bb393babec84e34d05b5de1858f95a430f3f064721a50293ffc8d036163/strawberry_django_phonenumber-0.1.0.tar.gz",
    "platform": null,
    "description": "# strawberry-django-phonenumber\n\n![build status](https://github.com/UpliftAgency/strawberry-django-phonenumber/actions/workflows/pythonpackage.yml/badge.svg)\n\n## Introduction\n\nGraphQL types for Phone Numbers with Strawberry Django. If you use `django`, `strawberry`, and `django-phonenumber-field`, this library is for you.\n\nSupported on:\n\n* Python 3.9+ (likely earlier versions too, needs tested)\n* Django 3+\n* strawberry-graphql-django 0.17+\n* django-phonenumber-field 7+\n\nHere's how it works. Automagically get this query:\n\n```graphql\nquery User {\n  phoneNumber {\n    ...phoneNumberFragment\n  }\n}\n\nfragment phoneNumberFragment on PhoneNumber {\n    asInternational  # +1 415-418-3420\n    asNational  # (415) 418-3420\n    asE164\n    asRfc3966\n    countryCode  # 1\n    nationalNumber\n    extension\n    rawInput\n}\n```\n\nWith this code:\n\n```python\n# yourapp/models.py\nfrom django.contrib.auth.models import AbstractUser\nfrom phonenumber_field.modelfields import PhoneNumberField\n\n\nclass User(AbstractUser):\n    phone_number = PhoneNumberField(blank=True)\n\n# yourapp/graphql/types.py\nfrom typing import Optional, cast\n\nimport strawberry\nimport strawberry_django\nfrom strawberry.types import Info\nfrom strawberry_django_phonenumber import PhoneNumber\n\nfrom yourapp import models\n\n\n\n@strawberry_django.type(models.User)\nclass User(strawberry.relay.Node):\n    \"\"\"GraphQL type for the User model.\"\"\"\n\n    @strawberry_django.field\n    async def phone_number(root, info: Info) -> Optional[PhoneNumber]:\n        if not root.phone_number:\n            return None\n        return cast(PhoneNumber, root.phone_number)\n\n# yourapp/graphql/__init__.py\nfrom typing import Optional\n\nimport strawberry\nimport strawberry_django\nfrom asgiref.sync import sync_to_async\nfrom strawberry.types import Info\n\nfrom .types import User\n\n\n@sync_to_async\ndef aget_user_from_request(request):\n    return request.user if bool(request.user) else None\n\n\n@strawberry.type\nclass Queries:\n    @strawberry_django.field\n    async def me(self, info: Info) -> Optional[User]:\n        user = await aget_user_from_request(info.context.request)\n        return user\n\n\nschema = strawberry.Schema(query=Queries)\n\n# yourapp/urls.py\n\nfrom django.urls import path\nfrom django.views.decorators.csrf import csrf_exempt\nfrom strawberry.django.views import AsyncGraphQLView\n\nfrom .graphql import schema\n\nurlpatterns = [\n    path(\n        \"graphql/\",\n        csrf_exempt(\n            AsyncGraphQLView.as_view(\n                schema=schema,\n                graphiql=True,\n            )\n        ),\n    ),\n]\n\n```\n\n## Installation\n\n```bash\npip install strawberry-django-phonenumber\n```\n\n### Changelog\n\n**0.1.0**\n\n    - Initial release\n\n\n## Contributing\n\nRunning tests:\n\n```bash\npoetry run pytest\n```\n\nThat's it, lite process for now. Please open a pull request or issue.\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A strawberry + Django integration for phone numbers",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/UpliftAgency/strawberry-django-phonenumber",
        "Repository": "https://github.com/UpliftAgency/strawberry-django-phonenumber"
    },
    "split_keywords": [
        "strawberry",
        "django",
        "phonenumber",
        "phone",
        "number",
        "phonenumber-field",
        "graphql",
        "strawberry_django"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ee7f8b002baffd319948f99c66230309b951d9aff3f3b5c7328c7cd3843f0c39",
                "md5": "e4fe87994d2d4b5a7b80320564bbe141",
                "sha256": "b89028adc14eb00a87864c6717b3d804f05e1bb84c4312a076b42fffc7e301c6"
            },
            "downloads": -1,
            "filename": "strawberry_django_phonenumber-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e4fe87994d2d4b5a7b80320564bbe141",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11,<4.0",
            "size": 4206,
            "upload_time": "2023-09-19T04:53:20",
            "upload_time_iso_8601": "2023-09-19T04:53:20.650011Z",
            "url": "https://files.pythonhosted.org/packages/ee/7f/8b002baffd319948f99c66230309b951d9aff3f3b5c7328c7cd3843f0c39/strawberry_django_phonenumber-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0f894bb393babec84e34d05b5de1858f95a430f3f064721a50293ffc8d036163",
                "md5": "c348c241405023b3c4aa785d092126c3",
                "sha256": "6d2c2742e676e22269a3d7dc5a15f134bb6ffb067ff4d4aec1a6f1e3d0447028"
            },
            "downloads": -1,
            "filename": "strawberry_django_phonenumber-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "c348c241405023b3c4aa785d092126c3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11,<4.0",
            "size": 3160,
            "upload_time": "2023-09-19T04:53:22",
            "upload_time_iso_8601": "2023-09-19T04:53:22.088951Z",
            "url": "https://files.pythonhosted.org/packages/0f/89/4bb393babec84e34d05b5de1858f95a430f3f064721a50293ffc8d036163/strawberry_django_phonenumber-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-19 04:53:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "UpliftAgency",
    "github_project": "strawberry-django-phonenumber",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "strawberry-django-phonenumber"
}
        
Elapsed time: 0.11866s