Skip to main content

A strawberry + Django integration for phone numbers

Project description

strawberry-django-phonenumber

build status

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.2.0

- Remove psycopg2-binary dependency, allow psycopg>=3

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strawberry_django_phonenumber-0.2.1.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file strawberry_django_phonenumber-0.2.1.tar.gz.

File metadata

File hashes

Hashes for strawberry_django_phonenumber-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b465d0aa17bffb403ce88f511d222d86109003790f3b8a07ef895d2bcc7c6ef6
MD5 0e21b1a928dd9ec64d1538728ccfa0c8
BLAKE2b-256 a5f474fec037056498e0c57c7ca503ac8dc70cecfc64fe75fbb93b7a226c355a

See more details on using hashes here.

Provenance

The following attestation bundles were made for strawberry_django_phonenumber-0.2.1.tar.gz:

Publisher: publish.yml on UpliftAgency/strawberry-django-phonenumber

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file strawberry_django_phonenumber-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for strawberry_django_phonenumber-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f784c65a90655dde3033167100819b40ad49358d15165bee59b0e33167498323
MD5 8b642307d6ad5e2f892756c1636cf095
BLAKE2b-256 3b19bb7c00f7336083d06c4f132aac0b7b86c3aa1f78761aa7dabd706d0ed951

See more details on using hashes here.

Provenance

The following attestation bundles were made for strawberry_django_phonenumber-0.2.1-py3-none-any.whl:

Publisher: publish.yml on UpliftAgency/strawberry-django-phonenumber

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page