# 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.2.0**
- Remove psycopg2-binary dependency, allow psycopg>=3
**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": null,
"docs_url": null,
"requires_python": "<4,>=3.10",
"maintainer_email": null,
"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/c7/b8/55c92f5221b7a52315901e4bc71185109b2b49cc64af895c52c3a7770930/strawberry_django_phonenumber-0.2.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.2.0**\n\n - Remove psycopg2-binary dependency, allow psycopg>=3\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.2.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": "86aecb590444d856768123571e239a0fc74c9915fe45813d791b7aa0af56e901",
"md5": "ceefcc32b832edf1c39deb65cae48654",
"sha256": "de6dc3f7d9740592218c20e17d0a0e1e637357462c8b484bdb5dad28c3d08e66"
},
"downloads": -1,
"filename": "strawberry_django_phonenumber-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ceefcc32b832edf1c39deb65cae48654",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.10",
"size": 4279,
"upload_time": "2024-07-18T22:06:10",
"upload_time_iso_8601": "2024-07-18T22:06:10.439824Z",
"url": "https://files.pythonhosted.org/packages/86/ae/cb590444d856768123571e239a0fc74c9915fe45813d791b7aa0af56e901/strawberry_django_phonenumber-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c7b855c92f5221b7a52315901e4bc71185109b2b49cc64af895c52c3a7770930",
"md5": "bcd81ea9af90723c667b4398cd046428",
"sha256": "82e993e54205801ecd3cca4812911cd1ab988ec2dbd076a7a265a3106a6ee537"
},
"downloads": -1,
"filename": "strawberry_django_phonenumber-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "bcd81ea9af90723c667b4398cd046428",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.10",
"size": 3277,
"upload_time": "2024-07-18T22:06:11",
"upload_time_iso_8601": "2024-07-18T22:06:11.726365Z",
"url": "https://files.pythonhosted.org/packages/c7/b8/55c92f5221b7a52315901e4bc71185109b2b49cc64af895c52c3a7770930/strawberry_django_phonenumber-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-18 22:06:11",
"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"
}