Skip to main content

GraphQL Money types for Django using graphene and django-money (djmoney).

Project description

graphene-djmoney

python package

Build Status PyPI version

Introduction

GraphQL Money types for Django using graphene and django-money (djmoney). If you use django, graphene_django, and django-money, this library is for you.

Supported on:

  • Python 3.7+ (likely earlier versions too, needs tested)
  • Django 2+
  • graphene-django 2+
  • django-money 1+

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

query Products {
    products {
        id
        cost {
            ...moneyFragment
        }
    }
}

fragment moneyFragment on Money {
    asString  # "123.45 USD"
    amount    # 123.45
    amountStr # "123.45"
    currency {
        code  # "USD"
        name  # "US Dollar"
        # These are not as commonly used, see tests:
        numeric
        symbol
        prefix
    }
}

With this code:

# yourapp/models.py
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models
from djmoney.models.fields import MoneyField


class User(AbstractUser):
    pass


class Product(models.Model):
    creator = models.ForeignKey(User, related_name="products", on_delete=models.CASCADE)
    title = models.CharField(max_length=2000)
    cost = MoneyField(
        max_digits=settings.CURRENCY_MAX_DIGITS,
        decimal_places=settings.CURRENCY_DECIMAL_PLACES,
        default_currency=settings.BASE_CURRENCY,
        null=True,
        blank=True,
    )

# yourapp/schema/types.py

import graphene
from graphene_django import DjangoObjectType

from yourapp import models


class Product(DjangoObjectType):
    class Meta:
        model = models.Product
        interfaces = (graphene.relay.Node,)
        fields = ("id", "cost")

# yourapp/schema/__init__.py

import graphene

from .. import models
from .types import Product

class Queries(graphene.ObjectType):

    products = graphene.List(graphene.NonNull(types.Product), required=True)

    def resolve_products(self, info, **kwargs):
        return models.Product.objects.all()


schema = graphene.Schema(query=Queries, types=[Product])

# yourapp/settings.py

INSTALLED_APPS += [
    "graphene_djmoney",
]

GRAPHENE = {
    "SCHEMA": "yourapp.schema.schema",
}

Installation

pip install graphene-djmoney

Changelog

0.2.0

- #5, #6, #7 Upgrade to py-moneyed 2.0, add babel format support (new field, `formatted`)
- **Breaking change**: removes `suffix` from schema, since babel doesn't support out of the box.

0.1.3

Initial release, sort of.

Contributing

Running tests:

poetry run pytest

Still TODO. 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

graphene_djmoney-0.2.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

graphene_djmoney-0.2.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: graphene_djmoney-0.2.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.15.0-1024-azure

File hashes

Hashes for graphene_djmoney-0.2.1.tar.gz
Algorithm Hash digest
SHA256 6fef813afb0c76e19bc763e56a92f2a945b1f70e8f4b743fc43b0e107e319a37
MD5 6253c86ade254f16d3e8f6420f3093f3
BLAKE2b-256 d2cd419765808aea634e2d349a509fbf5b41af51c1be85ac5f4245099636d436

See more details on using hashes here.

File details

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

File metadata

  • Download URL: graphene_djmoney-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.15.0-1024-azure

File hashes

Hashes for graphene_djmoney-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c4ad7aa75e87b54c959fcc024e80007b4351e3a1c893183e7476498137e3f1f5
MD5 6cf4d63b7bf346ac9c50abdad3f13ce2
BLAKE2b-256 8ccec5fd8eb451e14abcb60b76197295a811e9073f0e0fcb912363d6751e11ce

See more details on using hashes here.

Supported by

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