# graphene-djmoney
![python package](https://github.com/UpliftAgency/graphene-djmoney/actions/workflows/pythonpackage.yml/badge.svg)
[![Build Status](https://travis-ci.org/UpliftAgency/graphene-djmoney.svg?branch=master)](https://travis-ci.org/UpliftAgency/graphene-djmoney) [![PyPI version](https://badge.fury.io/py/graphene-djmoney.svg)](https://badge.fury.io/py/graphene-djmoney)
## 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:
```graphql
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:
```python
# 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
```bash
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:
```bash
poetry run pytest
```
Still TODO. For now, please open a pull request or issue.
Raw data
{
"_id": null,
"home_page": "https://github.com/UpliftAgency/graphene-djmoney",
"name": "graphene-djmoney",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "",
"keywords": "graphene,djmoney,moneyed,graphql,money,currency",
"author": "Paul Craciunoiu",
"author_email": "paul@craciunoiu.net",
"download_url": "https://files.pythonhosted.org/packages/77/04/46abc9ab1d4a21c4c6e7f89267ad7aecad787107e17b0bb9d506c3b8619e/graphene_djmoney-0.2.3.tar.gz",
"platform": null,
"description": "# graphene-djmoney\n\n![python package](https://github.com/UpliftAgency/graphene-djmoney/actions/workflows/pythonpackage.yml/badge.svg)\n\n[![Build Status](https://travis-ci.org/UpliftAgency/graphene-djmoney.svg?branch=master)](https://travis-ci.org/UpliftAgency/graphene-djmoney) [![PyPI version](https://badge.fury.io/py/graphene-djmoney.svg)](https://badge.fury.io/py/graphene-djmoney)\n\n## Introduction\n\nGraphQL Money types for Django using graphene and django-money (djmoney). If you use `django`, `graphene_django`, and `django-money`, this library is for you.\n\nSupported on:\n\n* Python 3.7+ (likely earlier versions too, needs tested)\n* Django 2+\n* graphene-django 2+\n* django-money 1+\n\nHere's how it works. Automagically get this query:\n\n```graphql\nquery Products {\n products {\n id\n cost {\n ...moneyFragment\n }\n }\n}\n\nfragment moneyFragment on Money {\n asString # \"123.45 USD\"\n amount # 123.45\n amountStr # \"123.45\"\n currency {\n code # \"USD\"\n name # \"US Dollar\"\n # These are not as commonly used, see tests:\n numeric\n symbol\n prefix\n }\n}\n```\n\nWith this code:\n\n```python\n# yourapp/models.py\nfrom django.conf import settings\nfrom django.contrib.auth.models import AbstractUser\nfrom django.db import models\nfrom djmoney.models.fields import MoneyField\n\n\nclass User(AbstractUser):\n pass\n\n\nclass Product(models.Model):\n creator = models.ForeignKey(User, related_name=\"products\", on_delete=models.CASCADE)\n title = models.CharField(max_length=2000)\n cost = MoneyField(\n max_digits=settings.CURRENCY_MAX_DIGITS,\n decimal_places=settings.CURRENCY_DECIMAL_PLACES,\n default_currency=settings.BASE_CURRENCY,\n null=True,\n blank=True,\n )\n\n# yourapp/schema/types.py\n\nimport graphene\nfrom graphene_django import DjangoObjectType\n\nfrom yourapp import models\n\n\nclass Product(DjangoObjectType):\n class Meta:\n model = models.Product\n interfaces = (graphene.relay.Node,)\n fields = (\"id\", \"cost\")\n\n# yourapp/schema/__init__.py\n\nimport graphene\n\nfrom .. import models\nfrom .types import Product\n\nclass Queries(graphene.ObjectType):\n\n products = graphene.List(graphene.NonNull(types.Product), required=True)\n\n def resolve_products(self, info, **kwargs):\n return models.Product.objects.all()\n\n\nschema = graphene.Schema(query=Queries, types=[Product])\n\n# yourapp/settings.py\n\nINSTALLED_APPS += [\n \"graphene_djmoney\",\n]\n\nGRAPHENE = {\n \"SCHEMA\": \"yourapp.schema.schema\",\n}\n\n```\n\n## Installation\n\n```bash\npip install graphene-djmoney\n```\n\n### Changelog\n\n**0.2.0**\n\n - #5, #6, #7 Upgrade to py-moneyed 2.0, add babel format support (new field, `formatted`)\n - **Breaking change**: removes `suffix` from schema, since babel doesn't support out of the box.\n\n\n**0.1.3**\n\n Initial release, sort of.\n\n## Contributing\n\nRunning tests:\n\n```bash\npoetry run pytest\n```\n\nStill TODO. For now, please open a pull request or issue.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "GraphQL Money types for Django using graphene and django-money (djmoney).",
"version": "0.2.3",
"split_keywords": [
"graphene",
"djmoney",
"moneyed",
"graphql",
"money",
"currency"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2489c77b6495474b94555c09272562734933729cc7cf8ca6de697bb1c998c149",
"md5": "f9146a766da1cd5b64a9475dd03aee88",
"sha256": "bad2b75e3ba420f8d2543f030509bc8aff18052823ba15fcfb6796efd777878b"
},
"downloads": -1,
"filename": "graphene_djmoney-0.2.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f9146a766da1cd5b64a9475dd03aee88",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 5540,
"upload_time": "2023-01-05T00:50:39",
"upload_time_iso_8601": "2023-01-05T00:50:39.644521Z",
"url": "https://files.pythonhosted.org/packages/24/89/c77b6495474b94555c09272562734933729cc7cf8ca6de697bb1c998c149/graphene_djmoney-0.2.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "770446abc9ab1d4a21c4c6e7f89267ad7aecad787107e17b0bb9d506c3b8619e",
"md5": "ce5530f92b7f9f92b4a2d85e5df3209c",
"sha256": "113dd17e878d518fc488a14652f2fb045a4f2e84d8d3739a4809fcc3852404ed"
},
"downloads": -1,
"filename": "graphene_djmoney-0.2.3.tar.gz",
"has_sig": false,
"md5_digest": "ce5530f92b7f9f92b4a2d85e5df3209c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 5355,
"upload_time": "2023-01-05T00:50:40",
"upload_time_iso_8601": "2023-01-05T00:50:40.670942Z",
"url": "https://files.pythonhosted.org/packages/77/04/46abc9ab1d4a21c4c6e7f89267ad7aecad787107e17b0bb9d506c3b8619e/graphene_djmoney-0.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-05 00:50:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "UpliftAgency",
"github_project": "graphene-djmoney",
"travis_ci": true,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "graphene-djmoney"
}