joaovitorsilvestre / graphene-mongodb

Implementation of Graphene and Mongoengine
MIT License
14 stars 4 forks source link

Mutation on a DecimalField fails with "Expected type 'CustomDecimalField'" #40

Closed stratosgear closed 6 years ago

stratosgear commented 6 years ago

Any ideas why a mutation like:

mutation {
  createOrder(totalAmount: 40.5, orderType: "purchase") {
    order {
      totalAmount
      orderType
      id
    }
  }
}

on a model structure like (copied relevant parts only):

class Order(Document):
    """HT3 Order Model."""

    meta = {'collection': 'order'}

    order_type = StringField(choices=ORDER_TYPES, max_length=12, nullable=False)
    total_amount = DecimalField(min_value=0)

class OrderSchema(MongoSchema):
    """Graphql representation of the Order Model."""

    model = Order

    @require_any_jwt_role(['admin', 'editor'])
    def validator(model, fields, query, special_params):
        """Check if the current user can read this model. """
        print('Order Graphql validator hook', file=sys.stderr)

class Mutation(ObjectType):
    """Graphql representation for saving project Models."""

    create_order = OrderSchema.mutate

schema = Schema(query=Query, mutation=Mutation)

will respond with a:

{
  "errors": [
    {
      "message": "Argument \"totalAmount\" has invalid value \"40.5\".\nExpected type \"CustomDecimalField\", found \"40.5\".",
      "locations": [
        {
          "line": 2,
          "column": 27
        }
      ]
    }
  ]
}

Tried with the amount as 40.5, "40.5" to no avail. Same error.

Your tests/examples are not using a DecimalField with a mutation anywhere... :(

Thanks!

joaovitorsilvestre commented 6 years ago

CustomDecimal field was missing the parse_value method. It was fixed in #41.