A strawberry + Django integration for phone numbers
Project description
strawberry-django-phonenumber
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:
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:
# 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
pip install strawberry-django-phonenumber
Changelog
0.1.0
- Initial release
Contributing
Running tests:
poetry run pytest
That's it, lite process for now. Please open a pull request or issue.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file strawberry_django_phonenumber-0.1.0.tar.gz
.
File metadata
- Download URL: strawberry_django_phonenumber-0.1.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d2c2742e676e22269a3d7dc5a15f134bb6ffb067ff4d4aec1a6f1e3d0447028 |
|
MD5 | c348c241405023b3c4aa785d092126c3 |
|
BLAKE2b-256 | 0f894bb393babec84e34d05b5de1858f95a430f3f064721a50293ffc8d036163 |
File details
Details for the file strawberry_django_phonenumber-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: strawberry_django_phonenumber-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b89028adc14eb00a87864c6717b3d804f05e1bb84c4312a076b42fffc7e301c6 |
|
MD5 | e4fe87994d2d4b5a7b80320564bbe141 |
|
BLAKE2b-256 | ee7f8b002baffd319948f99c66230309b951d9aff3f3b5c7328c7cd3843f0c39 |