Skip to content

Encoding doesn't always apply scalar encoders. #582

@ValdemarGr

Description

@ValdemarGr

the following change was made to our schema

@live
and input_AssetInput = {
+ labels: array<Scalars.UUIDScalar.t>,
  mileage?: int,
  comment?: string,
  color?: string,
  hasRegTax: bool,
  hasVat: bool,
  assetNo?: int,
  firstRegDate?: Scalars.LocalDateScalar.t,
  regNo?: string,
  makeModel?: string,
  vin?: string,
}

with

module UUIDScalar = {
  type t = UUID.t
  let parse = json =>
    switch UUID.decode(json) {
    | Some(uuid) => uuid
    | None =>
      Js.Console.error(`uuid decoding failure`)
      Js.Exn.raiseError(`faild to parse uuid`)
    }
  let serialize = x => UUID.stringify(x)->Js.Json.string
}
module LocalDateScalar = {
  type t = LocalDate.t
  let parse = y => {
    switch Js.Json.decodeString(y) {
    | None => raise(InvalidScalarType(y, "type is not string"))
    | Some(x) =>
      switch LocalDate.parseIso8601(x) {
      | None =>
        raise(
          InvalidScalarType(y, `failed to parse local date, value ${x} is not valid iso8601 date`),
        )
      | Some(x) => x
      }
    }
  }
  let serialize = x =>
    LocalDate.toIso8601(x)->StringScalar.serialize
}

Adding the array of scalars broke encoding.
Before:

{
	"input": {
		"assetNo": 10240,
		"color": "Sort",
		"comment": "The quick brow fox\n\njumps over the\n\nlazy\ndog",
		"firstRegDate": "2013-04-09",
		"hasRegTax": false,
		"hasVat": true,
		"makeModel": "RENAULT Ny Clio dCi 90 5d!",
		"mileage": 44,
		"regNo": "AE13324",
		"vin": "VF15RRL0H48604439"
	}
}

After:

{
	"input": {
		"assetNo": 10240,
		"color": "Sort",
		"comment": "The quick brow fox\n\njumps over the\n\nlazy\ndog",
		"firstRegDate": {
			"day": 9,
			"month": 4,
			"year": 2013
		},
		"hasRegTax": false,
		"hasVat": true,
		"labels": [],
		"makeModel": "RENAULT Ny Clio dCi 90 5d!",
		"mileage": 44,
		"regNo": "AE13324",
		"vin": "VF15RRL0H48604439"
	}
}

It might be related to #407

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions