mirumee / ariadne

Python library for implementing GraphQL servers using schema-first approach.

Home Page:https://ariadnegraphql.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom JSON implementation

iblislin opened this issue · comments

Hi,
Is there a way to change the JSON implementation? e.g. I want to use simplejson for better Decimal support.
I need the number with correct trailing zeros.

import simplejson
from decimal import Decimal

simplejson.dumps({'foo': Decimal('123.00000000000000000')})
# Out[20]: '{"foo": 123.00000000000000000}'

The JSON implementation in the standard library cannot support the decimal correctly.

For the record, GraphQL solution to this problem is a custom scalar that represents value as string.

Ariadne is not serializing response payloads directly, this is done by Starlette's JSONResponse, but if you need to customize this behavior, you can subclass ariadne.asgi.handlers.GraphQLHTTPHandler:

https://ariadnegraphql.org/docs/asgi-handlers-reference#create_json_response

Custom JSON response guide is now available on Ariadne's docs: Customizing JSON responses