Skip to content

Commit f8c5ac7

Browse files
committed
simplify missingFieldHandlers
1 parent d7edf3c commit f8c5ac7

File tree

2 files changed

+102
-112
lines changed

2 files changed

+102
-112
lines changed

packages/rescript-relay/src/RescriptRelay.res

Lines changed: 50 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -347,78 +347,65 @@ module ReadOnlyRecordSourceProxy = {
347347
}
348348

349349
module MissingFieldHandler = {
350-
@@warning("-30")
351-
type t
352-
353-
type normalizationArgumentWrapped = {kind: [#ListValue | #Literal | #ObjectValue | #Variable]}
354-
355-
type rec normalizationListValueArgument = {
356-
name: string,
357-
items: array<Nullable.t<normalizationArgumentWrapped>>,
358-
}
359-
and normalizationLiteralArgument = {
360-
name: string,
361-
@as("type") type_: Nullable.t<string>,
362-
value: JSON.t,
363-
}
364-
and normalizationObjectValueArgument = {
365-
name: string,
366-
fields: Nullable.t<array<normalizationArgumentWrapped>>,
367-
}
368-
and normalizationVariableArgument = {
369-
name: string,
370-
@as("type") type_: Nullable.t<string>,
371-
variableName: string,
372-
}
373-
374-
type normalizationArgument =
375-
| ListValue(normalizationListValueArgument)
376-
| Literal(normalizationLiteralArgument)
377-
| ObjectValue(normalizationObjectValueArgument)
378-
| Variable(normalizationVariableArgument)
379-
380-
let unwrapNormalizationArgument = wrapped =>
381-
switch wrapped.kind {
382-
| #ListValue => ListValue(Obj.magic(wrapped))
383-
| #Literal => Literal(Obj.magic(wrapped))
384-
| #ObjectValue => ObjectValue(Obj.magic(wrapped))
385-
| #Variable => Variable(Obj.magic(wrapped))
386-
}
350+
@tag("kind")
351+
type rec normalizationArgument =
352+
| ListValue({name: string, items: array<null<normalizationArgument>>})
353+
| Literal({name: string, @as("type") type_: nullable<string>, value: JSON.t})
354+
| ObjectValue({name: string, fields: array<normalizationArgument>})
355+
| Variable({name: string, @as("type") type_: nullable<string>, variableName: string})
387356

388357
type normalizationScalarField = {
389-
alias: Nullable.t<string>,
358+
alias: nullable<string>,
390359
name: string,
391-
args: Nullable.t<array<normalizationArgumentWrapped>>,
392-
storageKey: Nullable.t<string>,
360+
args: nullable<array<normalizationArgument>>,
361+
storageKey: nullable<string>,
393362
}
394363

395-
let makeScalarMissingFieldHandler = handle =>
396-
Obj.magic({
397-
"kind": #scalar,
398-
"handle": handle,
399-
})
400-
401364
type normalizationLinkedField = {
402-
alias: Nullable.t<string>,
365+
alias: nullable<string>,
403366
name: string,
404-
storageKey: Nullable.t<string>,
405-
args: Nullable.t<array<normalizationArgument>>,
406-
concreteType: Nullable.t<string>,
367+
storageKey: nullable<string>,
368+
args: nullable<array<normalizationArgument>>,
369+
concreteType: nullable<string>,
407370
plural: bool,
408371
selections: array<JSON.t>,
409372
}
410373

411-
let makeLinkedMissingFieldHandler = handle =>
412-
Obj.magic({
413-
"kind": #linked,
414-
"handle": handle,
415-
})
416-
417-
let makePluralLinkedMissingFieldHandler = handle =>
418-
Obj.magic({
419-
"kind": #pluralLinked,
420-
"handle": handle,
421-
})
374+
@tag("kind")
375+
type rec t =
376+
| @as("scalar")
377+
Scalar({
378+
handle: (
379+
normalizationScalarField,
380+
nullable<'record>,
381+
'args,
382+
ReadOnlyRecordSourceProxy.t,
383+
) => 'scalarValue,
384+
}): t
385+
| @as("linked")
386+
Linked({
387+
handle: (
388+
normalizationLinkedField,
389+
nullable<'record>,
390+
'args,
391+
ReadOnlyRecordSourceProxy.t,
392+
) => option<dataId>,
393+
}): t
394+
| @as("pluralLinked")
395+
PluralLinked({
396+
handle: (
397+
normalizationLinkedField,
398+
nullable<'record>,
399+
'args,
400+
ReadOnlyRecordSourceProxy.t,
401+
) => array<dataId>,
402+
}): t
403+
404+
let makeScalarMissingFieldHandler = handle => Scalar({handle: handle})
405+
406+
let makeLinkedMissingFieldHandler = handle => Linked({handle: handle})
407+
408+
let makePluralLinkedMissingFieldHandler = handle => PluralLinked({handle: handle})
422409
}
423410

424411
// This handler below enables automatic resolution of all cached items through the Node interface
@@ -428,8 +415,8 @@ let nodeInterfaceMissingFieldHandler = MissingFieldHandler.makeLinkedMissingFiel
428415
args,
429416
_store,
430417
) =>
431-
switch (Nullable.toOption(record), field["name"], Nullable.toOption(args["id"])) {
432-
| (Some(record), "node", argsId) if record->RecordProxy.getType == storeRootType => argsId
418+
switch (record, field.name, args["id"]) {
419+
| (Value(record), "node", argsId) if record->RecordProxy.getType == storeRootType => argsId
433420
| _ => None
434421
}
435422
)

packages/rescript-relay/src/RescriptRelay.resi

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -635,85 +635,88 @@ module ReadOnlyRecordSourceProxy: {
635635
636636
Feed a list of missing field handlers into `Environment.make` if you want to use them.*/
637637
module MissingFieldHandler: {
638-
@@warning("-30")
639-
640-
/**A missing field handler, which is a way of teaching Relay more about the relations in your schema, so it can fulfill more things from the cache. Read more [in this section of the Relay docs](https://relay.dev/docs/guided-tour/reusing-cached-data/filling-in-missing-data/).*/
641-
type t
642-
643-
type normalizationArgumentWrapped = {kind: [#ListValue | #Literal | #ObjectValue | #Variable]}
638+
@tag("kind")
639+
type rec normalizationArgument =
640+
| ListValue({name: string, items: array<null<normalizationArgument>>})
641+
| Literal({name: string, @as("type") type_: nullable<string>, value: JSON.t})
642+
| ObjectValue({name: string, fields: array<normalizationArgument>})
643+
| Variable({name: string, @as("type") type_: nullable<string>, variableName: string})
644644

645-
type rec normalizationListValueArgument = {
646-
name: string,
647-
items: array<Nullable.t<normalizationArgumentWrapped>>,
648-
}
649-
and normalizationLiteralArgument = {
650-
name: string,
651-
@as("type") type_: Nullable.t<string>,
652-
value: JSON.t,
653-
}
654-
and normalizationObjectValueArgument = {
655-
name: string,
656-
fields: Nullable.t<array<normalizationArgumentWrapped>>,
657-
}
658-
and normalizationVariableArgument = {
645+
type normalizationScalarField = {
646+
alias: nullable<string>,
659647
name: string,
660-
@as("type") type_: Nullable.t<string>,
661-
variableName: string,
648+
args: nullable<array<normalizationArgument>>,
649+
storageKey: nullable<string>,
662650
}
663651

664-
type normalizationArgument =
665-
| ListValue(normalizationListValueArgument)
666-
| Literal(normalizationLiteralArgument)
667-
| ObjectValue(normalizationObjectValueArgument)
668-
| Variable(normalizationVariableArgument)
669-
670-
let unwrapNormalizationArgument: normalizationArgumentWrapped => normalizationArgument
671-
672-
type normalizationScalarField = {
673-
alias: Nullable.t<string>,
652+
type normalizationLinkedField = {
653+
alias: nullable<string>,
674654
name: string,
675-
args: Nullable.t<array<normalizationArgumentWrapped>>,
676-
storageKey: Nullable.t<string>,
655+
storageKey: nullable<string>,
656+
args: nullable<array<normalizationArgument>>,
657+
concreteType: nullable<string>,
658+
plural: bool,
659+
selections: array<JSON.t>,
677660
}
678661

662+
@tag("kind")
663+
type rec t =
664+
| @as("scalar")
665+
Scalar({
666+
handle: (
667+
normalizationScalarField,
668+
nullable<'record>,
669+
'args,
670+
ReadOnlyRecordSourceProxy.t,
671+
) => 'scalarValue,
672+
}): t
673+
| @as("linked")
674+
Linked({
675+
handle: (
676+
normalizationLinkedField,
677+
nullable<'record>,
678+
'args,
679+
ReadOnlyRecordSourceProxy.t,
680+
) => option<dataId>,
681+
}): t
682+
| @as("pluralLinked")
683+
PluralLinked({
684+
handle: (
685+
normalizationLinkedField,
686+
nullable<'record>,
687+
'args,
688+
ReadOnlyRecordSourceProxy.t,
689+
) => array<dataId>,
690+
}): t
691+
679692
/**Make a `MissingFieldHandler.t` for scalar fields. Give this a handler function that returns `Js.null` (to indicate that data exists but is null), `Js.undefined` (to indicate data is still missing), or a scalar value (to indicate that the value exists even though it's not in the cache, and is the value you send back).*/
680693
let makeScalarMissingFieldHandler: (
681694
(
682695
normalizationScalarField,
683-
Nullable.t<'record>,
696+
nullable<'record>,
684697
'args,
685698
ReadOnlyRecordSourceProxy.t,
686699
) => 'scalarValue
687700
) => t
688701

689-
type normalizationLinkedField = {
690-
alias: Nullable.t<string>,
691-
name: string,
692-
storageKey: Nullable.t<string>,
693-
args: Nullable.t<array<normalizationArgument>>,
694-
concreteType: Nullable.t<string>,
695-
plural: bool,
696-
selections: array<JSON.t>,
697-
}
698-
699702
/**Make a `MissingFieldHandler.t` for linked fields (other objects/records). Give this a handler function that returns `Js.null` (to indicate that the link exists but the linked record is null), `Js.undefined` (to indicate data is still missing), or a `dataId` of the record that is linked at this field.*/
700703
let makeLinkedMissingFieldHandler: (
701704
(
702705
normalizationLinkedField,
703-
Nullable.t<RecordProxy.t>,
706+
nullable<RecordProxy.t>,
704707
'args,
705708
ReadOnlyRecordSourceProxy.t,
706-
) => Nullable.t<dataId>
709+
) => option<dataId>
707710
) => t
708711

709712
/**Make a `MissingFieldHandler.t` for lists of linked fields (other objects/records). Give this a handler function that returns `Js.null` (to indicate that the link exists but the linked record is null), `Js.undefined` (to indicate data is still missing), or an array of `Js.Nullable.t<dataId>` where the `dataId`'s are the linked records/objects.*/
710713
let makePluralLinkedMissingFieldHandler: (
711714
(
712715
normalizationLinkedField,
713-
Nullable.t<RecordProxy.t>,
716+
nullable<RecordProxy.t>,
714717
'args,
715718
ReadOnlyRecordSourceProxy.t,
716-
) => Nullable.t<array<Nullable.t<dataId>>>
719+
) => array<dataId>
717720
) => t
718721
}
719722

0 commit comments

Comments
 (0)