Skip to content

Commit

Permalink
refactor: uncurries internal functions so that the package can compil…
Browse files Browse the repository at this point in the history
…e on either curried/uncurried
  • Loading branch information
illusionalsagacity committed Oct 20, 2024
1 parent fbf33cb commit b0f2f25
Show file tree
Hide file tree
Showing 29 changed files with 284 additions and 265 deletions.
2 changes: 2 additions & 0 deletions EXAMPLES/bsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "examples",
"uncurried": false,
"graphql": {
"apolloMode": true,
"uncurried": false,
"extendMutation": "ApolloClient.GraphQL_PPX.ExtendMutation",
"extendQuery": "ApolloClient.GraphQL_PPX.ExtendQuery",
"extendSubscription": "ApolloClient.GraphQL_PPX.ExtendSubscription",
Expand Down
6 changes: 3 additions & 3 deletions EXAMPLES/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"start": "rescript build -with-deps -w"
},
"devDependencies": {
"@reasonml-community/graphql-ppx": "1.2.4-1345e061.0",
"@reasonml-community/graphql-ppx": "1.2.4-79d140a5.0",
"graphql-client-example-server": "1.5.2",
"html-webpack-plugin": "5.5.0",
"rescript": "10.1.2",
"rescript": "11.1.4",
"webpack": "5.75.0",
"webpack-cli": "5.0.1",
"webpack-dev-server": "^4.11.1"
Expand All @@ -26,7 +26,7 @@
"graphql": "^15.7.2",
"react": "18.3.1",
"react-dom": "18.3.1",
"rescript-apollo-client": "3.2.0",
"rescript-apollo-client": "*",
"subscriptions-transport-ws": "0.11.0"
}
}
1 change: 1 addition & 0 deletions EXAMPLES/src/WebpackEntry.res
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@@warning("-3")
switch ReactDOM.querySelector("#root") {
| Some(el) =>
ReactDOM.render(
Expand Down
2 changes: 1 addition & 1 deletion EXAMPLES/src/caching/LocalStateManagement.res
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let cache = {
"Query",
TypePolicy.make(
~fields=[
("someLocalStateField", FieldReadFunction((_existing, _options) => someLocalState())),
("someLocalStateField", FieldReadFunction((. _existing, _options) => someLocalState())),
],
(),
),
Expand Down
1 change: 1 addition & 0 deletions bsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "rescript-apollo-client",
"uncurried": false,
"package-specs": [
{
"module": "commonjs",
Expand Down
17 changes: 8 additions & 9 deletions src/@apollo/client/cache/core/ApolloClient__Cache_Core_Cache.res
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ module ApolloCache = {
~fragmentName=?,
(),
) => {
let safeParse = Utils.safeParse(Fragment.parse)

let safeParse = Utils.safeParse(. Fragment.parse)
js
->Js_.readFragment(
~options={
Expand All @@ -196,7 +195,7 @@ module ApolloCache = {
(),
)
->Js.toOption
->Belt.Option.map(safeParse)
->Belt.Option.mapU(safeParse)
}

let readQuery = (
Expand All @@ -212,7 +211,7 @@ module ApolloCache = {
~canonizeResults=?,
variables,
) => {
let safeParse = Utils.safeParse(Operation.parse)
let safeParse = Utils.safeParse(. Operation.parse)

js
->Js_.readQuery(
Expand All @@ -230,7 +229,7 @@ module ApolloCache = {
~optimistic,
)
->Js.toOption
->Belt.Option.map(safeParse)
->Belt.Option.mapU(safeParse)
}

let writeFragment = (
Expand Down Expand Up @@ -304,7 +303,7 @@ module ApolloCache = {
~update,
variables,
) => {
let safeParse = Utils.safeParse(Operation.parse)
let safeParse = Utils.safeParse(. Operation.parse)

js
->Js_.updateQuery(
Expand All @@ -330,7 +329,7 @@ module ApolloCache = {
->Js.Nullable.fromOption,
)
->Js.toOption
->Belt.Option.map(safeParse)
->Belt.Option.mapU(safeParse)
}

let updateFragment = (
Expand All @@ -345,7 +344,7 @@ module ApolloCache = {
~update,
(),
) => {
let safeParse = Utils.safeParse(Fragment.parse)
let safeParse = Utils.safeParse(. Fragment.parse)

js
->Js_.updateFragment(
Expand All @@ -367,7 +366,7 @@ module ApolloCache = {
->Js.Nullable.fromOption,
)
->Js.toOption
->Belt.Option.map(safeParse)
->Belt.Option.mapU(safeParse)
}

preserveJsPropsAndContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ let make: (
?dataIdFromObject,
?possibleTypes,
?resultCaching,
typePolicies: ?typePolicies->Belt.Option.map(TypePolicies.toJs),
typePolicies: ?typePolicies->Belt.Option.mapU(TypePolicies.toJs),
})->ApolloCache.fromJs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ module TypePolicy = {
fields?: t_fields,
}

let toJs: t => Js_.t = t => {
keyFields: ?t.keyFields->Belt.Option.map(KeyArgs.toJs),
let toJs: (. t) => Js_.t = (. t) => {
keyFields: ?t.keyFields->Belt.Option.mapU(KeyArgs.toJs),
queryType: ?t.queryType,
mutationType: ?t.mutationType,
subscriptionType: ?t.subscriptionType,
fields: ?t.fields->Belt.Option.map(fields =>
fields: ?t.fields->Belt.Option.mapU((. fields) =>
fields
->Belt.Array.map(((fieldKey, t_field)) => (
->Belt.Array.mapU((. (fieldKey, t_field)) => (
fieldKey,
switch t_field {
| ConcatPagination(keyArgs) =>
Expand All @@ -97,7 +97,7 @@ module TypePolicy = {

| FieldPolicy(fieldPolicy) => fieldPolicy->FieldPolicy.toJs->Js_.FieldsUnion.fieldPolicy
| FieldReadFunction(fieldReadFunction) =>
fieldReadFunction->FieldReadFunction.toJs->Js_.FieldsUnion.fieldReadFunction
FieldReadFunction.toJs(. fieldReadFunction)->Js_.FieldsUnion.fieldReadFunction
},
))
->Js.Dict.fromArray
Expand Down Expand Up @@ -133,8 +133,8 @@ module TypePolicies = {

type t = array<(typename, TypePolicy.t)>

let toJs: t => Js_.t = t =>
t->Belt.Array.map(((key, policy)) => (key, TypePolicy.toJs(policy)))->Js.Dict.fromArray
let toJs: (. t) => Js_.t = (. t) =>
t->Belt.Array.mapU((. (key, policy)) => (key, TypePolicy.toJs(. policy)))->Js.Dict.fromArray
}

module PossibleTypesMap = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ module FieldMergeFunction = {
// incoming: SafeReadonly<TIncoming>,
// options: TOptions,
// ) => SafeReadonly<TExisting>;
type t<'existing> = ('existing, 'existing, FieldFunctionOptions.Js_.t) => 'existing
type t<'existing> = (. 'existing, 'existing, FieldFunctionOptions.Js_.t) => 'existing
}

let toJs: t<'existing> => Js_.t<'existing> = (t, existing, incoming, jsFieldFunctionOptions) =>
t(existing, incoming, jsFieldFunctionOptions->FieldFunctionOptions.fromJs)
let toJs: (. t<'existing>) => Js_.t<'existing> = (. t) =>
(. existing, incoming, jsFieldFunctionOptions) =>
t(existing, incoming, jsFieldFunctionOptions->FieldFunctionOptions.fromJs)
}

module FieldMerge = {
Expand All @@ -116,36 +117,37 @@ module FieldMerge = {
// FieldMergeFunction<TExisting, TIncoming, TOptions> | boolean;
module FieldMergeUnion: {
type t<'existing>
let mergeFunction: FieldMergeFunction.Js_.t<'existing> => t<'existing>
let mergeFunction: (. FieldMergeFunction.Js_.t<'existing>) => t<'existing>
let true_: t<'existing>
} = {
@unboxed
type rec t<'existing> = Any('a): t<'existing>
let mergeFunction = (v: FieldMergeFunction.Js_.t<'existing>) => Any(v)
let mergeFunction = (. v: FieldMergeFunction.Js_.t<'existing>) => Any(v)
let true_ = Any(true)
}

type t<'existing> = FieldMergeUnion.t<'existing>
}

let toJs: t<'existing> => Js_.t<'existing> = x =>
let toJs: (. t<'existing>) => Js_.t<'existing> = (. x) =>
switch x {
| MergeFunction(mergeFunction) =>
mergeFunction->FieldMergeFunction.toJs->Js_.FieldMergeUnion.mergeFunction
Js_.FieldMergeUnion.mergeFunction(. FieldMergeFunction.toJs(. mergeFunction))
| True => Js_.FieldMergeUnion.true_
}
}

module FieldReadFunction = {
type t<'existing> = (option<'existing>, FieldFunctionOptions.t) => 'existing
type t<'existing> = (. option<'existing>, FieldFunctionOptions.t) => 'existing

module Js_ = {
// export declare type FieldReadFunction<TExisting = any, TReadResult = TExisting> = (existing: SafeReadonly<TExisting> | undefined, options: FieldFunctionOptions) => TReadResult | undefined;
type t<'existing> = (option<'existing>, FieldFunctionOptions.Js_.t) => 'existing
type t<'existing> = (. option<'existing>, FieldFunctionOptions.Js_.t) => 'existing
}

let toJs: t<'existing> => Js_.t<'existing> = (t, existing, jsFieldFunctionOptions) =>
t(existing, jsFieldFunctionOptions->FieldFunctionOptions.fromJs)
let toJs: (. t<'existing>) => Js_.t<'existing> = (. t) =>
(. existing, jsFieldFunctionOptions) =>
t(. existing, jsFieldFunctionOptions->FieldFunctionOptions.fromJs)
}

module KeySpecifier = {
Expand Down Expand Up @@ -200,7 +202,7 @@ module FieldPolicy_KeyArgs = {
type t = KeyArgsUnion.t
}

let toJs: t => Js_.t = x =>
let toJs: (. t) => Js_.t = (. x) =>
switch x {
| KeySpecifier(keySpecifier) => keySpecifier->Js_.KeyArgsUnion.keySpecifier
| KeyArgsFunction(keyArgsFunction) => keyArgsFunction->Js_.KeyArgsUnion.keyArgsFunction
Expand Down Expand Up @@ -229,8 +231,8 @@ module FieldPolicy = {
}

let toJs: t<'existing> => Js_.t<'existing> = t => {
keyArgs: ?t.keyArgs->Belt.Option.map(FieldPolicy_KeyArgs.toJs),
read: ?t.read->Belt.Option.map(FieldReadFunction.toJs),
merge: ?t.merge->Belt.Option.map(FieldMerge.toJs),
keyArgs: ?t.keyArgs->Belt.Option.mapU(FieldPolicy_KeyArgs.toJs),
read: ?t.read->Belt.Option.mapU(FieldReadFunction.toJs),
merge: ?t.merge->Belt.Option.mapU(FieldMerge.toJs),
}
}
Loading

0 comments on commit b0f2f25

Please sign in to comment.