graphql-python / graphene

GraphQL framework for Python

Home Page:http://graphene-python.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Conversion to snake_case fails on nested inputs

advl opened this issue · comments

commented
  • What is the current behavior?
    I have double nested inputs, the first two levels are properly converted to snake_case, the third one (deepest) is not.

  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via
    a github repo, https://repl.it or similar.

import graphene
from django.contrib.auth import get_user_model

from ..models import Place, PostalAddress

User = get_user_model()

class OwnerInput(graphene.InputObjectType):
    first_name = graphene.String(required=True)
    last_name = graphene.String(required=True)

class PostalAddressInput(graphene.InputObjectType):
    street_address = graphene.String(required=True)
    address_locality = graphene.String(required=True)
    address_region = graphene.String(required=True)
    postal_code = graphene.String(required=True)
    address_country = graphene.String(required=True)


class PlaceInput(graphene.InputObjectType):
    latitude = graphene.Float(required=True)
    longitude = graphene.Float(required=True)
    postal_address = graphene.Field(PostalAddressInput, required=True)


class RegisterPlaceInput(graphene.InputObjectType):
    owner = graphene.Field(OwnerInput, required=True)
    place = graphene.Field(PlaceInput, required=True)

class RegisterPlaceMutation(graphene.Mutation):
    class Arguments:
        input = graphene.Argument(RegisterPlaceInput, required=True)

    success = graphene.Boolean(
        description="Indicates whether the operation was successful."
    )

    def mutate(root, info, input):
        print(input)
        owner_data = input['owner'] #Converted to snake
        place_data = input['place'] #converted to snake at the lower level
        postal_address_data = place_data['postal_address'] # remains camel

        postal_address = PostalAddress.objects.create(**postal_address_data) #fails as data not in the right shape

        place = Place.objects.create(
            latitude=place_data['latitude'],
            longitude=place_data['longitude'],
            postal_address=postal_address,
        )

        return RegisterPlaceMutation(success=True)


class Mutation(graphene.ObjectType):
    register_place = RegisterPlaceMutation.Field()

print gives :

>>  {'owner': {'first_name': 'JOHN', 'last_name
': 'DOE'}, 'place': {'latitude': 48.9230953, 'longitude': 2.0303184, 'postal_address': {'streetAddress': 'Skalitzer Strasse 99', 'addressLocality': 'Berlin', 'addressRegion': 'Berlin', 'post
alCode': '10997', 'addressCountry': 'DE'}}}
  • What is the expected behavior?
    All nested object keys are converted to snake_case.

  • What is the motivation / use case for changing the behavior?
    snake_case conversion should be consistent independently of the shape of the input

  • Please tell us about your environment:

    • Version: 3.3
    • Platform: with graphene_django 3.2 - (unsure if it's relevant)