rescript-relay icon indicating copy to clipboard operation
rescript-relay copied to clipboard

Encoding doesn't always apply scalar encoders.

Open ValdemarGr opened this issue 1 month ago • 9 comments

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 https://github.com/zth/rescript-relay/issues/407

ValdemarGr avatar Oct 30 '25 16:10 ValdemarGr

Update. If I swap the order of of the fields in the schema so labels is after the LocalDate the encoding works. Could it be some statefulness in the encoding?

ValdemarGr avatar Oct 30 '25 16:10 ValdemarGr

Could you add a failing test to utils.js?

zth avatar Oct 30 '25 16:10 zth

I made a suggestion https://github.com/zth/rescript-relay/pull/583 It is difficult for me to get the project running since I don't have the ocaml infrastructure and esy doesn't work well with nix.

I'll try using the compiler from npm for the test.

ValdemarGr avatar Oct 30 '25 17:10 ValdemarGr

I think this shows what you requested

$ /home/valde/git/rescript-relay/packages/rescript-relay/node_modules/.bin/jest __tests__/utils-tests.js
 FAIL  __tests__/utils-tests.js
  ● Test suite failed to run

    expect(received).toEqual(expected) // deep equality

    - Expected  - 2
    + Received  + 8

      Object {
        "input": Object {
          "os1s": Array [
    -       "serialized1",
    +       Object {
    +         "a": "a",
    +         "b": "b",
    +       },
          ],
    -     "os2": "serialized2",
    +     "os2": Object {
    +       "a": "a",
    +       "c": "c",
    +     },
        },
      }

      908 |         undefined
      909 |       )
    > 910 |     ).toEqual({
          |       ^
      911 |       input: {
      912 |         os1s: ["serialized1"],
      913 |         os2: "serialized2",

      at __tests__/utils-tests.js:910:7
      at __tests__/utils-tests.js:882:3
      at Object.<anonymous> (__tests__/utils-tests.js:3:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.498 s
Ran all test suites matching /__tests__\/utils-tests.js/i.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

ValdemarGr avatar Oct 30 '25 17:10 ValdemarGr

I can see the instructions make a difference, do you have a reference for the instructions? I am unsure how to construct the correct instructions to test?

ValdemarGr avatar Oct 30 '25 17:10 ValdemarGr

I added a mutation to generate the correct instructions. Can you verify that my test case covers the issue?

 ✘ valde@nixos  ~/git/rescript-relay/packages/rescript-relay   master  yarn run jest __tests__/utils-tests.js
yarn run v1.22.22
$ /home/valde/git/rescript-relay/packages/rescript-relay/node_modules/.bin/jest __tests__/utils-tests.js
 FAIL  __tests__/utils-tests.js
  ● Test suite failed to run

    expect(received).toEqual(expected) // deep equality

    - Expected  - 1
    + Received  + 4

      Object {
        "os1s": Array [
          "serialized1",
        ],
    -   "os2": "serialized2",
    +   "os2": Object {
    +     "a": "a",
    +     "c": "c",
    +   },
      }

      907 |         undefined
      908 |       )
    > 909 |     ).toEqual({
          |       ^
      910 |       os1s: ["serialized1"],
      911 |       os2: "serialized2",
      912 |     });

      at __tests__/utils-tests.js:909:7
      at __tests__/utils-tests.js:882:3
      at Object.<anonymous> (__tests__/utils-tests.js:3:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.491 s
Ran all test suites matching /__tests__\/utils-tests.js/i.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

ValdemarGr avatar Oct 30 '25 18:10 ValdemarGr

I'll have a look soon. This whole mechanism is due for a rewrite anyway. It's by far the thing that has been causing the most problems in RescriptRelay. Maybe easier to get it right now using AI.

zth avatar Oct 30 '25 18:10 zth

Do you think emitting the encoders as part of the code generation could be an idea? Then it is typechecked?

ValdemarGr avatar Oct 30 '25 19:10 ValdemarGr

@ValdemarGr sorry for dropping the ball here. I'm not sure exactly how it should be structured to be optimal, but I do know this needs a full rewrite. This is what has been causing the most bugs in RescriptRelay by far. Hoping to get around to a rewrite soon. I'll try and make it so you can switch between versions (current and new) so we can roll this out to test easily.

zth avatar Dec 04 '25 07:12 zth